doc/articles/guides/localization.md
This guide will walk you through the necessary steps to localize an Uno Platform application.
[!TIP] The complete source code that goes along with this guide is available in the unoplatform/Uno.Samples GitHub repository - Localization
[!TIP] For a step-by-step guide to installing the prerequisites for your preferred IDE and environment, consult the Get Started guide.
Create a new Uno Platform application, following the instructions in Get Started guide.
Modify the content of MainPage:
In MainPage.xaml, replace the content of the page:
<Page x:Class="UnoLocalization.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="using:Uno.UI.Toolkit">
<StackPanel toolkit:VisibleBoundsPadding.PaddingMask="Top">
<TextBlock x:Uid="MainPage_IntroText" Text="Hello, world!" Margin="20" FontSize="30" />
<TextBlock x:Name="CodeBehindText" Text="This text will be replaced" />
</StackPanel>
</Page>
Note:
- The
x:Nameis used to make the element accessible from the code-behind with that same name.- The
x:Uidis used for localization. To localize a property, you need to add a string resource in each resource file using itsx:Uidfollowed by a dot (.) and then the property name. eg:MainPage_IntroText.TextMore on this in the resource steps that follow.
In MainPage.xaml.cs, add an Page.Loaded handler to change the text for CodeBehindText:
public MainPage()
{
this.InitializeComponent();
this.Loaded += (s, e) => CodeBehindText.Text = Windows.ApplicationModel.Resources.ResourceLoader
.GetForViewIndependentUse()
.GetString("MainPage_CodeBehindString");
}
Create a new resource file for localization in French in the UnoLocalization.Shared project:
fr under the Strings folder by:
Right-click on Strings > Add > New FolderResources.resw under the fr folder by:
Right-click on fr > Add > New Item ... > Visual C# > Xaml > Resources FileAdd the localization strings for both English and French:
Open both Strings\en\Resources.resw and Strings\fr\Resources.resw, and add these:
| Name | Value in en\Resources.resw | Value in fr\Resources.resw |
|---|---|---|
| MainPage_IntroText.Text | Hello Uno | Bonjour Uno |
| MainPage_CodeBehindString | String from code-behind | Texte provenant du code-behind |
[!NOTE] Make sure to hit Ctrl+S for both files, to save the changes.
[!IMPORTANT] The
ResourceLoaderwill search for resources in the current language, then the default language. The default language is defined by the MSBuild propertyDefaultLanguage, which defaults toen.
You can now try to run the app.
The "Hello World" text should be replaced with "Hello Uno", or "Bonjour Uno" if the targeted environment is on French culture. Now, if you change the language of the targeted PC or the mobile device AND restart the app, that text should also change accordingly.
You can also set the starting culture to see the result, without having to modify the system language:
App.cs or App.xaml.cs:
public App()
{
InitializeLogging();
ChangeStartingLanguage();
this.InitializeComponent();
this.Suspending += OnSuspending;
}
private void ChangeStartingLanguage()
{
var culture = new System.Globalization.CultureInfo("fr");
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = culture.TwoLetterISOLanguageName;
}
For WinUI 3 projects
package.appxmanifest in a text editor and locate the following section:<Resources>
<Resource Language="x-generate"/>
</Resources>
Replace it with elements for each of your supported languages. For example:
<Resources>
<Resource Language="en-US"/>
<Resource Language="es-ES"/>
<!-- Add more languages as needed -->
</Resources>
See the completed sample on GitHub: LocalizationSamples/Localization
Globalization and localization
[!includegetting-help]