Saturday, January 28, 2012

Make a XAML content localizable

  • Create a new resource file (see How to: Add Resources to a Silverlight-based Application) or reuse an existing one. You will later create additional resource files for each language/culture to which you are localizing your application, so you may want to create a folder for the resource files.
  • Open the resource file. At the top of the resource file window, make sure that AccessModifier is set to Public.
  • Add a Class item to your project, and give it an appropriate name (such as DisplayText in this example). Define a class with a property that points to the resources, as in the following example. In this example, SilverlightApp is the namespace that contains the resource file, and Resource1 is the name of the resource file
    public class DisplayText
    {
        /// <summary>
        /// Resources Dictionary
        /// </summary>
        public DisplayTextResources Resources { get; private set; }
    
    
        /// <summary>
        /// Constructor
        /// </summary>
        public DisplayText()
        {
            Resources = new DisplayTextResources();
        }
    }
  • Make sure that the class, its class constructor, and the property that returns the resource are public; otherwise, you won't be able to use it in XAML.
  • If you have multiple resource files in your project (for example, Resource1 and Resource2), you can have a single LocalizedStrings class with multiple properties, each of which returns a particular resource.
  • Open your App.xaml file and add the following to the <Application.Resources> section:
    <Application.Resources>
       <loc:DisplayText xmlns:loc="clr-namespace:appNamespace" 
                        x:Key="LocalizedStrings" />
    </Application.Resources>
  • where DisplayText is the name of the class with the property that returns your resource file, and appNamespace is the namespace that contains the LocalizedStrings class. The x:Key attribute defines the name by which you refer to a DisplayText instance in code.
  • Open a XAML file (such as MainPage.xaml) and find the individual strings in it that you want to localize.
  • To make a string that is assigned to an attribute localizable, replace the string value with the following:
    "{Binding Path=Resources.resourceName, Source={StaticResource LocalizedStrings}}"
    
    
  • where resourceName is the name of the localizable resource, Resources is the name of the resource file that contains resourceName, and LocalizedStrings is the name assigned to an instance of the class that returns the resource. For example, if the original XAML is the following:
    <Button Content="Submit" />
    you might change it to:
    <Button Content="{Binding Path=Resources.Submit, Source={StaticResource LocalizedStrings }}"/>
  • Repeat the previous step for all the strings that you want to make localizable in each XAML file in your application that contains localizable content.

No comments:

Post a Comment