Silverlight项目如何实现多语言呢?微软提供了标准的解决方案
整个解决方案分以下几步:
1.创建语言文件
如上图,创建以resx结尾的资源文件MultiLanguage。并创建需要的语言文件,语言文件的命名规范是资源文件名加点,加标准语言名称(如简体中文是zh-CN,可查)。
如上图,将语言文件设置为没用生成代码。
在语言文件中添加需要翻译的语言文字。
zh-CN
en-US
2.为项目生成添加语言支持
在silverlight项目的.csproj文件中添加多语言支持 <SupportedCultures>zh-CN,en-US</SupportedCultures>
3.创建用于多语言的类LocalizedStrings
namespace TMS
{
public class LocalizedStrings
{
public ObservableResources LanguageResource { get; set; }
public LocalizedStrings()
{
LanguageResource = new ObservableResources();
}
public void ChangeLanguage()
{
LanguageResource.UpdateBindings();
}
}
public class ObservableResources : INotifyPropertyChanged {
public string this[string resourceName] {
get {
return Resources.MultiLanguage.ResourceManager.GetString(resourceName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void UpdateBindings() {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Item[]"));
}
}
}
该类已封装了语言动态切换时通知界面文件刷新的功能
其中用于语言转换的类名称来自资源文件MultiLanguage.Designer.cs
namespace TMS.Resources {
using System;
/// <summary>
/// 一个强类型的资源类,用于查找本地化的字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class MultiLanguage {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal MultiLanguage() {
}
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TMS.Resources.MultiLanguage", typeof(MultiLanguage).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 使用此强类型资源类,为所有资源查找
/// 重写当前线程的 CurrentUICulture 属性。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// 查找类似 BASE 的本地化字符串。
/// </summary>
public static string SubmitTxt {
get {
return ResourceManager.GetString("SubmitTxt", resourceCulture);
}
}
}
}
为了在全局使用此转换类,我们在App.xaml中添加一个静态资源Generic.xaml
<Application
x:Class="TMS.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles.xaml"/>
<ResourceDictionary Source="Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Generic.xaml内容如下
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<local:LocalizedStrings xmlns:local ="clr-namespace:TMS"
x:Key="LocalizedStrings" />
</ResourceDictionary>
4.在silverlight页面中使用多语言切换
<TextBlock Text="{Binding Path=LanguageResource[CurrFaultInfo], Source={StaticResource LocalizedStrings }}"/>
特殊情况是对datagrid字段头名称的动态绑定,需要用到
<sdk:DataGridTextColumn.HeaderStyle>
<sdk:DataGridTextColumn Header="设备号" Binding="{Binding ATMId}" >
<sdk:DataGridTextColumn.HeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=LanguageResource[EquipNum], Source={StaticResource LocalizedStrings }}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</sdk:DataGridTextColumn.HeaderStyle>
</sdk:DataGridTextColumn>
创建对象
LocalizedStrings languageSetter = (LocalizedStrings)App.Current.Resources["LocalizedStrings"];
使用对象
DT = languageSetter.LanguageResource["Support"];
6.实现语言更换动态通知
两个radiobox的点击事件中处理
private void zhCN_Click(object sender, RoutedEventArgs e)
{
CultureInfo ci = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
MultiLanguage.Culture = ci;
//LocalizedStrings languageSetter = (LocalizedStrings)App.Current.Resources["LocalizedStrings"];
languageSetter.ChangeLanguage();
}
private void enUS_Click(object sender, RoutedEventArgs e)
{
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
MultiLanguage.Culture = ci;
//LocalizedStrings languageSetter = (LocalizedStrings)App.Current.Resources["LocalizedStrings"];
languageSetter.ChangeLanguage();
}
<sdk:DataGridTextColumn Header="设备号" Binding="{Binding ATMId}" >
的方式实现