Silverlight中实现多语言(本地化)

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> 


5.对后台代码,如弹出提示框等动态绑定

创建对象

 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();
        }



OK,大功告成!!另外可能面临的问题是如果datagrid的列名绑定如果在后台用代码写,将不能实现语言的动态切换,建议采用在前台

<sdk:DataGridTextColumn Header="设备号" Binding="{Binding ATMId}" >
的方式实现




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
/// <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()] internal class Resources { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// <summary> /// 返回此使用的缓存的 ResourceManager 实例。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("让屏幕开满玫瑰把妹必备.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// 使用此类型资源,为所有资源查找 /// 重写当前线程的 CurrentUICulture 属性。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } internal static System.Drawing.Bitmap 稀饭你 { get { object obj = ResourceManager.GetObject("稀饭你", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值