国际化(i18n)和本地化(l10n)是高端程序的必备技术,可惜从业五年从没有尝试过,下一步准备做一个多用户的博客系统,想支持多语言,今天就学习了一下,写出来,希望大家批评。
代码下载:http://yunpan.cn/QnJezZmVV8LL5。
感谢Visual Studio,它帮我们做了很多工作。
项目结构
测试代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Globalization;
-
- namespace I18nStudy
- {
- class Program
- {
- static void Main(string[] args)
- {
- Display();
-
- Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
- Display();
- }
-
- private static void Display()
- {
- Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
-
- Console.WriteLine(Resources.Messages.Name);
- }
- }
- }
输出结果
原来内部使用了ResourceManager。
- //------------------------------------------------------------------------------
- // <auto-generated>
- // 此代码由工具生成。
- // 运行时版本:4.0.30319.17929
- //
- // 对此文件的更改可能会导致不正确的行为,并且如果
- // 重新生成代码,这些更改将会丢失。
- // </auto-generated>
- //------------------------------------------------------------------------------
-
- namespace I18nStudy.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()]
- internal class Messages {
-
- private static global::System.Resources.ResourceManager resourceMan;
-
- private static global::System.Globalization.CultureInfo resourceCulture;
-
- [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
- internal Messages() {
- }
-
- /// <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("I18nStudy.Resources.Messages", typeof(Messages).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;
- }
- }
-
- /// <summary>
- /// 查找类似 默认 的本地化字符串。
- /// </summary>
- internal static string Name {
- get {
- return ResourceManager.GetString("Name", resourceCulture);
- }
- }
- }
- }
如果产品做大了,就要使Resgen和AI来生成卫星资源程序集了,多数情况VS就够用了。
每种UI技术都会在此之上进一步封装,另外客户端JS的多语言就需要另外的技术了(原型覆盖)。