Share via

How to: Make XAML Content Localizable

Microsoft Silverlight will reach end of support after October 2021. Learn more.

In your Silverlight-based application, you may want to make your XAML code localizable by dynamically supplying resource strings for XML attributes, or by modifying XAML content. XAML content refers to the string value between an opening and a closing XAML tag. For example, the following <TextBlock> tag makes use of XAML content:

<TextBlock>This is a block of text.</TextBlock>

To make XML attributes localizable

  1. 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.

  2. Open the resource file. At the top of the resource file window, make sure that AccessModifier is set to Public.

  3. Add a Class item to your project, and give it an appropriate name (such as LocalizedStrings 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 LocalizedStrings
       Public Sub New()
       End Sub
       Shared rm As New ResourceManager("SilverlightApp.Resource1", System.Reflection.Assembly.GetExecutingAssembly())
       Private Shared pResource1 As My.Resources.Resource1 = New My.Resources.Resource1()
       Public ReadOnly Property Resource1() As My.Resources.Resource1
             Return pResource1
          End Get
       End Property
    End Class
    public class LocalizedStrings
       public LocalizedStrings()
       private static SilverlightApp.Resource1 resource1 = new SilverlightApp.Resource1();
       public SilverlightApp.Resource1 Resource1 { get { return resource1; } }

    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.

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

       <local:LocalizedStrings xmlns:local ="clr-namespace:appNamespace"
                               x:Key="LocalizedStrings" />

    where LocalizedStrings 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 LocalizedStrings instance in code.

  5. Open a XAML file (such as MainPage.xaml) and find the individual strings in it that you want to localize.

  6. To make a string that is assigned to an attribute localizable, replace the string value with the following:

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

    where resourceName is the name of the localizable resource, resourceFile 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="press me!" />

    you might change it to:

    <Button Content="{Binding Path=Resource1.PressText, Source={StaticResource LocalizedStrings }}"/>
  7. 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.

To make XAML content localizable

  1. Create the necessary resource files and the class that returns the resources, as described in steps 1-3 in the previous procedure. In this example, the resource file is named Resource1, and the class instance that returns the resources is named LocalizedStrings.

  2. Identify the XAML content that you would like to localize. For example, the following string is defined as XAML content rather than as an XAML attribute.

    <TextBlock>"Hello world!"</TextBlock>
  3. Change the XAML to its attribute form.

    <TextBlock Text="Hello world!" />

    The following table lists some Silverlight controls and the name of the content property to which their XAML content can be assigned. In general, the ContentPropertyAttribute attribute that is applied to a control class defines the property to which its XAML content is mapped.











  4. Place the localizable text in the resource file for your default culture. For example, the text "Hello world!" might be assigned to a string resource named HelloText.

  5. Use the {Binding} markup extension to dynamically modify the XAML based on the value of the string resource.

    <TextBlock Text="{Binding Path=Resource1.HelloText, Source={StaticResource LocalizedStrings }}"/>