Localize your .NET MAUI application

Introduction

In this article, you’ll learn how to localize your .NET MAUI application. Localization refers to the translation of application resources into localized versions for the specific languages that the application supports. In other words, this is the process of customizing your app to make it work in different languages.

Let’s get started.

How to Create RESX files?

To get started, under the Resources folder of your .NET MAUI project, create a new folder called Strings. Inside that folder, add 3 new resource files,

  1. AppResources.resx (English — default)
  2. AppResources.fr.resx(French)
  3. AppResources.ar.resx (Arabic)

AppResources.resx

To support different languages, you need to create a default resource file with all strings used in the application, as well as resource files for each supported language with the name: AppResources.[culture code].resx. You can find a list of culture codes here.

Once the files are added, rows can be added for each text resource,

AppResources.resx

Using RESX files through XAML

First, you need to import the XML namespace of your resources.

xmlns:resx="clr-namespace:MauiMultiLangDemo.Resources.Strings"

Then, you can access your strings as [x:Static](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions#the-xstatic-markup-extension) properties like,

Text="{x:Static resx:AppResources.HelloWorld}"

Here’s a reference for how your XAML page could look like.

<?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"
             x:Class="MauiMultiLangDemo.MainPage"
             xmlns:resx="clr-namespace:MauiMultiLangDemo.Resources.Strings">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25"
            VerticalOptions="Center">

            <Image
                HeightRequest="200"
                HorizontalOptions="Center"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                Source="dotnet_bot.png" />

            <Label
                FontSize="32"
                HorizontalOptions="Center"
                SemanticProperties.HeadingLevel="Level1"
                Text="{x:Static resx:AppResources.HelloWorld}" />

            <Label
                FontSize="18"
                HorizontalTextAlignment="Center"
                HorizontalOptions="Center"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                SemanticProperties.HeadingLevel="Level2"
                Text="{x:Static resx:AppResources.Welcome}" />

            <Button
                x:Name="CounterBtn"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center"
                SemanticProperties.Hint="Counts the number of times you click"
                Text="{x:Static resx:AppResources.ClickMe}" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Using RESX files through C#

Localized text can also be retrieved in code,

Label label = new Label
{
Text = AppResources.HelloWorld,
};

Finally, to set the app language, we need to add this line on the file AppShell.xaml.cs.

AppResources.Culture = new CultureInfo("ar");

You can download the source code from this link.


Similar Articles