MAUI Supporting Multiple Languages – Localization


Localization in .NET MAUI can enable the application to handle multiple languages. It will be much easier for users from all over the world to get more information about the app in their native language.

Create Resource Files for Localization

Localization in .NET MAUI uses resource files with the .resx file extension to store key, localized strings applicable to particular languages. For example, you might have one resource file for English, one for Spanish, one for French, and so on.

  1. Add a Resources Folder:
    • Create a folder in your project called Resources.
    • Create a subfolder called Strings under it to host the resource files for localization.
  2. Create the .resx Files:
    • Add a .resx file for each language you want to support. For instance:
      • Resources/Strings/AppResources.resx (for default language, e.g., English)
      • Resources/Strings/AppResources.es.resx (for Spanish)
      • Resources/Strings/AppResources.fr.resx (for French)

Set Resource File Build Action

Make sure the Build Action for each .resx file is set to EmbeddedResource so that resource files compile properly into your application.

Define a Static Class for Localization Access

Here’s a static class that will provide ready access to the localized strings. This class uses ResourceManager to fetch the strings based upon the current culture.

LocalizationResourceManager.cs:

using System.Globalization;
using Resources.Strings;

namespace MauiAppExample.Helpers
{
    public static class LocalizationResourceManager
    {
        private static readonly ResourceManager resourceManager = new ResourceManager(typeof(AppResources));

        public static string GetString(string key)
        {
            return resourceManager.GetString(key, CultureInfo.CurrentUICulture);
        }
    }
}

ResourceManager: Makes it easy to access the .resx resources.

CultureInfo.CurrentUICulture: Returns the culture currently being used for retrieving of localized string.

Set Culture Based on User Preferences

There should be an optimization of set culture according to the user preference.

If you want your app to be able to display text in the language of your user’s choice then adjust the current culture.

using System.Globalization;
using System.Threading;

namespace MauiAppExample
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            // Set the desired culture
            SetCulture("es"); // Set to Spanish, change as per the user's choice

            MainPage = new MainPage();
        }

        private void SetCulture(string cultureCode)
        {
            var cultureInfo = new CultureInfo(cultureCode);
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }
    }
}

SetCulture: This method accepts a language code and sets the CurrentCulture and CurrentUICulture for the application, For example es is Spanish and fr is French.

Use Localized Strings in XAML

It is also possible to use data binding to retrieve localized strings into XAML views.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiAppExample.Helpers"
             x:Class="MauiAppExample.MainPage">

    <StackLayout Padding="20" Spacing="20" VerticalOptions="Center">
        <!-- Localized Label -->
        <Label Text="{x:Static local:LocalizationResourceManager.WelcomeMessage}" 
               FontSize="24"
               HorizontalOptions="Center" />

        <!-- Localized Button -->
        <Button Text="{x:Static local:LocalizationResourceManager.LoginButton}" 
                FontSize="18"
                HorizontalOptions="Center"
                VerticalOptions="Center" />
    </StackLayout>
</ContentPage>

Changing Language at Runtime

If you would like to provide a feature for users to switch language at run time you could implement a picker for language selection and invoke SetCulture.

<StackLayout Padding="20" Spacing="20" VerticalOptions="Center">
    <!-- Language Picker -->
    <Picker x:Name="LanguagePicker"
            Title="Choose Language"
            SelectedIndexChanged="LanguagePicker_SelectedIndexChanged">
        <Picker.Items>
            <x:String>English</x:String>
            <x:String>Spanish</x:String>
            <x:String>French</x:String>
        </Picker.Items>
    </Picker>

    <!-- Localized Label -->
    <Label Text="{x:Static local:LocalizationResourceManager.WelcomeMessage}" 
           FontSize="24"
           HorizontalOptions="Center" />

    <!-- Localized Button -->
    <Button Text="{x:Static local:LocalizationResourceManager.LoginButton}" 
            FontSize="18"
            HorizontalOptions="Center"
            VerticalOptions="Center" />
</StackLayout>
namespace MauiAppExample
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LanguagePicker_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedLanguage = LanguagePicker.SelectedItem.ToString();
            string cultureCode = "en"; // Default to English

            switch (selectedLanguage)
            {
                case "Spanish":
                    cultureCode = "es";
                    break;
                case "French":
                    cultureCode = "fr";
                    break;
            }

            // Set the culture and refresh the page
            App app = (App)Application.Current;
            app.SetCulture(cultureCode);

            // Refresh the UI
            Navigation.PushAsync(new MainPage());
        }
    }
}
,