WPF中的多语言

Jon*_*han 22 globalization wpf multilingual xaml

你能推荐一种为WPF应用程序实现多语言系统的好方法吗?我现在使用的方法涉及XML,类和xaml扩展.它在大多数情况下工作正常,但是当我不得不处理动态标签或动态文本时,它需要一些额外的努力.我想让程序员只在主要问题上工作并忘记lang问题.

And*_*rde 34

跟着这些步骤:

1)将所有String片段放在单独的资源文件中.

示例StringResources.xaml::

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">

    <!-- String resource that can be localized -->
    <system:String x:Key="All_Vehicles">All Vehicles</system:String>

</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

2)为每种语言制作副本并将它们(已翻译)添加到合并的词典中.不要忘记添加国家/地区的ISO代码以简化操作.

示例App.xaml:

<Application x:Class="WpfStringTables.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="StringResources.de-DE.xaml" />
                <ResourceDictionary Source="StringResources.nl-NL.xaml" />
                <ResourceDictionary Source="StringResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

带字符串的最后一个资源文件将用于替换代码中的文本部分.

3a)使用String表格中的文字部分:

示例Window1.xaml:

<Window x:Class="WpfStringTables.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>
        <Button Margin="51,82,108,129" Name="AllVehiclesButton" 
                Content="{StaticResource All_Vehicles}"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

3b)从代码加载资源(如果你不想设置,只使用这个代码XAML):

void PageLoad()
{
  string str = FindResource("All_Vehicles").ToString();
}
Run Code Online (Sandbox Code Playgroud)

4)在应用开始时切换到新的文化:

Codesnippet来自App.xaml.cs:

public static void SelectCulture(string culture)    
{      
    if (String.IsNullOrEmpty(culture))
        return;

    //Copy all MergedDictionarys into a auxiliar list.
    var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();

    //Search for the specified culture.     
    string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
    var resourceDictionary = dictionaryList.
        FirstOrDefault(d => d.Source.OriginalString == requestedCulture);

    if (resourceDictionary == null)
    {
        //If not found, select our default language.             
        requestedCulture = "StringResources.xaml";
        resourceDictionary = dictionaryList.
            FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
    }

    //If we have the requested resource, remove it from the list and place at the end.     
    //Then this language will be our string table to use.      
    if (resourceDictionary != null)
    {
        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
    }

    //Inform the threads of the new culture.     
    Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

}
Run Code Online (Sandbox Code Playgroud)

  • 实际上,只要在任何时候使用资源指定{DynamicResource resKey},然后在运行时随时调用上面的SelectCulture(culture)方法,它将动态更新所有字符串到新文化.而不是:string str = FindResource("All_Vehicles").ToString(); 使用:Application.Current.Resources ["All_Vehicles"]作为字符串 (4认同)

CSh*_*per 17

我正在使用WPF本地化扩展.它是本地化的任何类型的非常简单的方法DependencyPropertyDependencyObject秒.

  • 处于一个真正稳定的状态
  • 支持像绑定一样的写作风格 Text = {LocText ResAssembly:ResFile:ResKey}
  • 适用于.resx-fallback机制(例如en-us - > en - >独立文化)
  • 支持文化强迫(例如"这必须始终是英语")
  • 适用于普通的依赖属性
  • 适用于控件模板
  • 可以在XAML(实际上是:P)中使用,而无需任何其他命名空间
  • 可以在后面的代码中使用,以将本地化值绑定到动态生成的控件
  • 实现INotifyPropertyChanged高级用途
  • 支持字符串格式化 "this is the '{0}' value"
  • 支持前缀和后缀值(目前有LocText扩展名)
  • 在生产系统中使用(如我的公共关系产品)
  • 将语言切换到运行时会影响时间片
  • 可以与.resx所有程序集中的任何资源文件()一起使用(也可以在运行时加载动态文件)
  • 不需要任何初始化过程(比如"调用xyz来注册一个特殊的本地化字典")
  • 可在设计时使用(MS Expression Blend,MS Visual Studio 2008(普通版和SP1版)
  • 在设计时可以更改所选语言
  • 可以本地化任何类型的数据类型,只要它的转换器(TypeConverter)存在(扩展LocalizeExtension)
  • 内置支持Text,上Text,下Text,Images,Brushes DoubleThickness
  • 不会影响任何内存泄漏
  • 保持UID不动产
  • 提供SpecificCulture用作IFormatProvider(例如(123.20).ToString(LocalizeDictionary.SpecificCulture) = "123.20""123,20")
  • 提供了一些功能来检查和获取代码中的资源值
  • 不会改变文化Thread.CurrentCultureThread.CurrentUICulture(可以轻易改变)

  • 没有关于如何使用它的文档或教程? (6认同)