问题描述:

There are a number of RESX markup extension samples (and products like Globalizer) that provide XAML markup extensions to allow you to dynamically reference resources in a RESX file to enable localization. Has anybody looked at all these to pick out the wheat from the chaff? That is, anybody have a recommendation on a good RESX markup extension for XAML?

Here are some of the available options:

  • http://wpflocalizeextension.codeplex.com/
  • http://wpflocalization.codeplex.com/
  • http://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files
  • http://www.wpftutorial.net/LocalizeMarkupExtension.html
  • http://www.infralution.com/globalizer.html

网友答案:

You don't need any markup extensions to accomplish this. You can do it with a simple ValueConverter.

  1. Add .resx file to your project (for this example we will call it Resource1.resx, with a string value named "String1" value "Hello world!")

  2. Create a converter that will convert a string to a localized string.

    namespace WpfApplication1.Converters
    {
        public class ResxLocalizationConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string result = Resource1.ResourceManager.GetString(value.ToString());
                if (result == null)
                {
                    result = value.ToString();
                }
                return result;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    
  3. Add a converter instance to App.xaml

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml" 
                 xmlns:converters="clr-namespace:WpfApplication1.Converters">
        <Application.Resources>
            <converters:ResxLocalizationConverter x:Key="ResxLocalizationConverter" />
      </Application.Resources>
    </Application>
    
  4. Put the non-localized string values (or keys) in your XAML.

    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid>        
            <TextBlock Text="{Binding Source='String1', Converter={StaticResource ResxLocalizationConverter}}"/>
        </Grid>
    </Window>
    

This will display "Hello world!" in the TextBlock.

To localize for French, for example, you would just need to add Resource1.fr-FR.resx to the Visual Studio project (with a string value named "String1" value "Salut tout le monde!"). This will display the French version on French localized PCs, or you can use the ResourceManager.GetString(...) overload that lets you explicitly select the language.

相关阅读:
Top