WPF 中的多语言[关闭]
-
03-07-2019 - |
题
您能推荐一种为 WPF 应用程序实现多语言系统的好方法吗?我现在使用的方法涉及 XML、类和 xaml 扩展。它在大多数情况下工作得很好,但是当我必须处理动态标签或动态文本时,它需要一些额外的努力。我想让程序员只处理主要问题,而忘记语言问题。
解决方案
我正在使用 WPF 本地化扩展. 。这是本地化任何类型的非常简单的方法 DependencyProperty
在 DependencyObject
s。
- 处于真正的稳定状态
- 支持类似绑定的书写风格
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
,Image
s,Brush
是的,Double
和Thickness
- 不影响任何内存泄漏
- 留下
UID
财产未受影响 - 提供
SpecificCulture
用作IFormatProvider
(例如。(123.20).ToString(LocalizeDictionary.SpecificCulture) = "123.20"
或者"123,20"
) - 提供一些功能来检查和获取后面代码中的资源值
- 不会改变文化
Thread.CurrentCulture
或者Thread.CurrentUICulture
(可以轻松更改)
其他提示
请按照以下步骤操作:
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);
}
Josh Smith写了一篇关于他首选方法的深入教程:创建一个WPF中的国际化向导。
它可能会指向一个重大的重新设计(这是一个 MVVM解决方案),但是出于其他原因,使用MVVM似乎也值得。
使用本文,我设法轻松使用资源文件来处理多语言WPF窗口。 http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx 你应该检查一下,因为它非常简单有效。
不隶属于 StackOverflow