22

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

4

4 回答 4

37

跟着这些步骤:

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>

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>

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

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>

3b)从代码加载资源(如果您不想通过设置,请仅使用此代码XAML):

void PageLoad()
{
  string str = FindResource("All_Vehicles").ToString();
}

4)申请开始时切换到新文化:

代码片段来自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);

}
于 2009-05-02T10:49:40.743 回答
17

我正在使用WPF 本地化扩展。这是本地化任何类型的DependencyPropertyon DependencyObjects 的一种非常简单的方法。

  • 处于真正稳定的状态
  • 支持类似绑定的写作风格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(Normal 和 SP1)
  • 在设计时可以更改所选语言
  • 可以本地化任何类型的数据类型,只要它的转换器(TypeConverter)存在(扩展LocalizeExtension
  • 内置了对Text, upper Text, lower Text, Images, BrushesDoubleThickness
  • 不影响任何内存泄漏
  • 保持UID不动产
  • 提供SpecificCulture用作IFormatProvider(例如(123.20).ToString(LocalizeDictionary.SpecificCulture) = "123.20""123,20"
  • 提供一些功能来检查和获取后台代码中的资源值
  • 不会改变文化Thread.CurrentCultureThread.CurrentUICulture(可以很容易地改变)
于 2009-05-02T13:01:59.053 回答
1

Josh Smith 写了一篇关于他的首选方法的深入教程:在 WPF 中创建国际化向导

它可能会引导您进行大的重新设计(这是一个MVVM 解决方案),但出于其他原因,使用 MVVM 似乎也是值得的。

于 2009-05-02T10:57:16.937 回答
1

使用这篇文章,我设法轻松地使用资源文件来处理多语言 WPF 窗口。 http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx 您应该检查一下,因为它非常简单有效。

于 2011-10-31T14:06:39.613 回答