8 out of 18 rated this helpful Rate this topic

How to: Build a Localized Application for Windows Phone

Windows Phone

December 15, 2011

By following the best practices in the Localization Best Practices for Windows Phone topic, you can ensure that your application is localizable.

This topic describes how to separate localizable resources from code by creating language-specific resource files. Visual Studio uses these files to create assemblies that allow your application to support many languages. For more information about this process, see the Packaging and Deploying Resources topic.

Important noteImportant Note:

If you plan to localize your application for East Asian languages, you will also need to follow the steps listed at Displaying Chinese, Japanese, and Korean Text for Silverlight.

To view a full, working code sample that contains the below code examples, download the Globalization Sample from the Code Samples for Windows Phone topic.

As described in the Localization Best Practices for Windows Phone topic, all localizable resources should be moved to a resource file.

  1. Add a resource file for the default language of your application.

    1. Open the project in Visual Studio.

    2. Add a resource file. In Solution Explorer, right-click on the project name, click Add and click New Item.

    3. In the Add New Item dialog box, choose Resources File and re-name the file if desired. For example, you could re-name the file as AppResources.resx. This file will contain the resources for the default language for the application.

    4. Identify strings in your application and add them to the resource file. You can input a name, a value, and an optional comment for each string.

      • The name must be unique. Make it as descriptive as possible.

      • The value is the string that will be displayed to the user in the application.

      • The comment is optional, but it is helpful for translators, especially in large resource files with many strings.

  2. Add a resource file for each additional language that the application will support. Each resource file must contain the correct culture/language name, as described in Culture and Language Support for Windows Phone.

    For example:

    • For the culture Spanish (Spain), use AppResources.es-ES.resx.

    • For the culture German (Germany), use AppResources.de-DE.resx.

    Note

    You need to complete this step only for additional languages. For example, if your default language is English (United States) and you have added the strings for that language into the default language resource file, you should not create an additional resource file for "en-US."

  3. Define the default culture that the application will support:

    Important note Important Note:

    By default, the Neutral Language is set to the default culture in Visual Studio. If your application targets a different culture than the default culture in Visual Studio, you will need to complete the following steps. If you are not targeting a different culture, verify with the following steps that the correct Neutral Language is set.

    1. In Solution Explorer, right-click the project name, and click Properties.

    2. Under the Application tab, click the Assembly Information button.

    3. In the Neutral Language list, select the default culture. This identifies the language of the strings in the default resources file. For example, if the default resources file is named AppResources.resx, and the strings in that file are English (United States) language strings, you would select English (United States) as the Neutral Language for the project.

  4. Close the project and open the project file (<project name>.csproj) in a text editor. Locate the <SupportedCultures> tag and add the names of each additional culture (language) your application needs to support. You should have an entry for each .resx file added to the project).

    Separate the language names using a semicolon, and only add the additional culture names. For example, if an application uses English (United States) for its default culture, but the application also supports German (Germany) and Spanish (Spain), you would update this tag as follows:

    <SupportedCultures>de-DE;es-ES;</SupportedCultures>

Re-open the project to replace the hard-coded strings with strings in a resource file.

  1. In Solution Explorer, open a resource file and select Public from the AccessModifier list box at the top of the pane. Repeat this step for each resource file in your project.

  2. Define a class with a property that points to the resources. In the following example, the LocalizedStrings class contains a property that points to the AppResources resource file in the sdkGlobalizationCS namespace.

    public class LocalizedStrings
    {
        public LocalizedStrings()
        {
        }
    
        private static sdkGlobalizationCS.AppResources localizedResources = new sdkGlobalizationCS.AppResources();
    
        public sdkGlobalizationCS.AppResources LocalizedResources { get { return localizedResources; } }
    }
    
    

    If there are additional resource files in your project, you can have a single LocalizedStrings class with multiple properties, each of which returns a particular resource.

  3. Open your App.xaml file and add the following XAML code to the <Application.Resources> section:

    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:sdkGlobalizationCS" x:Key="LocalizedStrings" />
    </Application.Resources>
    
    

    where LocalizedStrings is the name of the class with the property that returns your resource file, and sdkGlobalizationCS is the namespace that contains the LocalizedStrings class. The x:Key attribute defines the name by which you refer to a LocalizedStrings instance in code.

  4. Replace each hard-coded string with the following XAML code:

    "{Binding Path=resourceFile.resourceName, Source={StaticResource LocalizedStrings}}"

    Where resourceName is the name of the localizable resource, resourceFile is the name of the pointer that contains resourceName, and LocalizedStrings is the name assigned to an instance of the class that returns the resource.

    The following XAML code example from the Globalization Sample binds the ListBoxItem control with the LangRegionNameFrFR string returned from the LocalizedStrings class instance:

    <ListBoxItem Content="{Binding Path=LocalizedResources.LangRegionNameFrFR, Source={StaticResource LocalizedStrings}}" />
    
  5. Repeat the previous step for each string that you want to make localizable, in each XAML file in your application that contains localizable content.

  6. Build your application – it will now build as a multilingual application and display a user interface according to the language settings of the phone.

You can add an Application Bar to a page in your application either in the page’s XAML or using C# in the page’s code-behind. However, because the Application Bar is not a Silverlight control, it does not support dynamic data binding for the text label of the ApplicationBarIconButton or for the text of the ApplicationBarMenuItem. For this reason, if you want to localize these strings, you must create or update the Application Bar at run time. The following code example shows a helper function that builds an Application Bar using localized resources to provide the string values. This example assumes that you have created a localized resource DLL with string table entries called “ButtonText” and “MenuItemText”. For information on creating an application bar, see Application Bar Overview for Windows Phone.

Caution note Caution:

The below code is not included in the Globalization Sample.

// Helper function to build a localized ApplicationBar
private void BuildApplicationBar()
{
    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    ApplicationBar = new ApplicationBar();

    // Create a new button and set the text value to the localized string from AppResources.
    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("ApplicationIcon.png",UriKind.Relative));
    appBarButton.Text = AppResources.ButtonText;
    ApplicationBar.Buttons.Add(appBarButton);

    // Create a new menu item with the localized string from AppResources.
    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.MenuItemText);
    ApplicationBar.MenuItems.Add(appBarMenuItem);

}

Did you find this helpful?
(1500 characters remaining)