User Tools

Site Tools


notes:uwp:localization

Localization in UWP

Namespaces in UWP related to cultures and regions:

  • Windows.Globalization
  • Windows.ApplicationModel.Resources

In UWP resource files have an extension .resw instead of .resx although both types of files use the same XML format, except that .resw files may contain only strings and file paths.

You can use the Visual Studio resource editors to create and edit resources.

Example: Localizing an app:

Step #1: Add resource files to the project:

  • Messages/en-us/Errors.resw
  • Messages/en-us/Messages.resw
  • Strings/en-us/Resources.resw

Step #2: Add resource keys and values:

Step #3: Create the UI and the code-behind. Note that we are using the attribute x:Uid to point to the resource:

MainPage.xaml

<Page
    x:Class="LocalizationTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LocalizationTest"
    FontSize="40" Foreground="Black">
 
    <Grid Background="White">
        <StackPanel Margin="20" Width="400" Background="Beige">
            <TextBlock x:Name="TextBlock1" />
            <TextBlock x:Name="TextBlock2" />
 
            <!-- 
            Use the resource named Text1 in the file Resources.resw.
            This resource defines values for properties Text and Width.
            -->
            <TextBox x:Uid="Text1" />
        </StackPanel>
    </Grid>
</Page>

MainPage.xaml.cs

using System;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
 
namespace LocalizationTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
 
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Assign a date with the current format.
            // An equivalent is to use ToString("D")
            TextBlock1.Text = DateTime.Today.ToString("D");
 
            // Access resources programmatically.
            var loader = new ResourceLoader("Messages"); // use the Messages.resw file
            TextBlock2.Text = loader.GetString("Hello");
        }
    }
}

Step #4: Install the Multilingual App Toolkit for Visual Studio and translate the resources:

  • Enable the tool: Tools > Enable Multilingual Toolkit
  • Right click the project in the Project Explorer and select “Add Translation Language…” option
  • Choose which languages should be translated
  • A MultilingualResources subfolder is created that contains .xlf files (in XLIFF format) for the selected languages.
  • Rebuild the project. The XLIFF files are filled with content from all the resources.
  • Click an .xlf file. This opens the Multilingual Editor.
  • Select the Translate command from the toolbar. This translates the resources from the selected file.

This is directory structure in the Visual Studio Solution Explorer after translating resources using the Multilingual App Toolkit:

Step #5: Run the app:

Step #6: Change the language preference in the Control Panel:

Step #7: Re-launch your app:

notes/uwp/localization.txt · Last modified: 2017/06/16 by leszek