How to: Combine Localizable Strings at Run Time

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

Your XAML code may include a localizable string that uses the .NET Framework's composite formatting feature (see Composite Formatting) to combine with another string at run time. For example, a text box might display a greeting along with the user's name. The localized string can include a format item, such as "Hello, {0}.".

You can combine localizable strings at run time by performing the following steps. This procedure assumes that you have already created the necessary resource files and created the class that returns the resources. (For more information, see Localizing Silverlight-based Applications.) In the case of this example, the resource file is named Resource1, and the class instance that returns the resources is named LocalizedStrings.

To combine localizable strings at run time

  1. Create a string resource that contains the composite format string. For example, you might add a string resource named FormatText whose value is "Hello, {0}." to the Resource1 resource file.

  2. Define a value converter (an System.Windows.Data.IValueConverter implementation) that is capable of processing the composite format string that is retrieved from the resource file. For example, the following code defines an IValueConverter implementation named SubstitutionConverter that is capable of handling a composite format string such as "Hello, {0}.".

    Public Class SubstitutionConverter : Implements System.Windows.Data.IValueConverter
    
       Public Sub New()
       End Sub
    
       Public Function Convert(ByVal value As Object, ByVal targetType As Type, _
                             ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
                             Implements System.Windows.Data.IValueConverter.Convert
          Return String.Format(value.ToString(), parameter.ToString())
       End Function
    
       Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, _
                                 ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
                                 Implements System.Windows.Data.IValueConverter.ConvertBack
          Throw New NotSupportedException("The SubstitutionConverter.ConvertBack method is not supported.")
       End Function
    End Class
    
    public class SubstitutionConverter : System.Windows.Data.IValueConverter
    {
       public SubstitutionConverter()
       {
       }
    
       public object Convert(object value, Type targetType,
                             object parameter, System.Globalization.CultureInfo culture)
       {
          return String.Format(value.ToString(), parameter.ToString());
       }
    
       public object ConvertBack(object value, Type targetType,
                                 object parameter, System.Globalization.CultureInfo culture)
       {
          throw new NotSupportedException("The SubstitutionConverter.ConvertBack method is not supported.");
       }
    }
    
  3. Define an instance of the value converter in an accessible resource dictionary, such as the <Application.Resources> section of App.xaml. This enables Silverlight to locate your value converter, and also maps your implementation to a class instance. For example, the definition for the SubstitutionConverter class is:

    <Application.Resources>
       <local:SubstitutionConverter xmlns:local="clr-namespace:SilverlightApp" 
        x:Key="substitutionConverter" /> 
    </Application.Resources>
    

    SubstitutionConverter is the name of the class that implements IValueConverter, SilverlightApp is the namespace in which SubstitutionConverter resides, and substitutionConverter is the name of an instance of SubstitutionConverter.

  4. Use the {Binding} markup extension to bind the XAML property to the composite format string that is retrieved from the resource file. For example, the following {Binding} markup extension replaces the {0} format item in the string "Hello, {0}." with the string "John" to produce the string "Hello, John".

    "{Binding Path=Resource1.FormatText, ConverterParameter=John,  
              Source={StaticResource LocalizedStrings}, 
              Converter={StaticResource substitutionConverter}}"
    

    Resource1 is the name of the resource file and FormatText is the name of the string resource that contains the composite format string, John is the string to be combined with FormatText, LocalizedStrings is the instance of the class that is used to retrieve string resources, and substitutionConverter is the name of the object that provides your IValueConverter implementation.