.NET:国际化和本地化

背景

国际化(i18n)和本地化(l10n)是高端程序的必备技术,可惜从业五年从没有尝试过,下一步准备做一个多用户的博客系统,想支持多语言,今天就学习了一下,写出来,希望大家批评。

收集的资料

一些知识点

  • CultureInfo有两部分组成:语言和区域,如en是语言,US是美国区域,标准的区域格式是:语言-区域(en-US)。
  • ResourceManager默认会根据当前线程的CurrentUICulture来返回资源,查找的路径依次是:bin\语言-区域\类库或应用程序.resources.dll、bin\语言\类库或应用程序.resources.dll,如果没有找到就去“资源名称.resx”中查找。
  • 当前线程的CurrentCulture只对数据的格式化和排序有影响。

使用资源文件和卫星程序集

代码下载:http://yunpan.cn/QnJezZmVV8LL5

感谢Visual Studio,它帮我们做了很多工作。

项目结构

.NET:国际化和本地化

测试代码

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Threading.Tasks;

 6 using System.Threading;

 7 using System.Globalization;

 8 

 9 namespace I18nStudy

10 {

11     class Program

12     {

13         static void Main(string[] args)

14         {

15             Display();

16 

17             Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

18             Display();

19         }

20 

21         private static void Display()

22         {

23             Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);

24 

25             Console.WriteLine(Resources.Messages.Name);

26         }

27     }

28 }

输出结果

.NET:国际化和本地化

 

Visual Studio帮我们做了什么?

原来内部使用了ResourceManager。

 1 //------------------------------------------------------------------------------

 2 // <auto-generated>

 3 //     此代码由工具生成。

 4 //     运行时版本:4.0.30319.17929

 5 //

 6 //     对此文件的更改可能会导致不正确的行为,并且如果

 7 //     重新生成代码,这些更改将会丢失。

 8 // </auto-generated>

 9 //------------------------------------------------------------------------------

10 

11 namespace I18nStudy.Resources {

12     using System;

13     

14     

15     /// <summary>

16     ///   一个强类型的资源类,用于查找本地化的字符串等。

17     /// </summary>

18     // 此类是由 StronglyTypedResourceBuilder

19     // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。

20     // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen

21     // (以 /str 作为命令选项),或重新生成 VS 项目。

22     [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]

23     [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

24     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]

25     internal class Messages {

26         

27         private static global::System.Resources.ResourceManager resourceMan;

28         

29         private static global::System.Globalization.CultureInfo resourceCulture;

30         

31         [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]

32         internal Messages() {

33         }

34         

35         /// <summary>

36         ///   返回此类使用的缓存的 ResourceManager 实例。

37         /// </summary>

38         [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]

39         internal static global::System.Resources.ResourceManager ResourceManager {

40             get {

41                 if (object.ReferenceEquals(resourceMan, null)) {

42                     global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("I18nStudy.Resources.Messages", typeof(Messages).Assembly);

43                     resourceMan = temp;

44                 }

45                 return resourceMan;

46             }

47         }

48         

49         /// <summary>

50         ///   使用此强类型资源类,为所有资源查找

51         ///   重写当前线程的 CurrentUICulture 属性。

52         /// </summary>

53         [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]

54         internal static global::System.Globalization.CultureInfo Culture {

55             get {

56                 return resourceCulture;

57             }

58             set {

59                 resourceCulture = value;

60             }

61         }

62         

63         /// <summary>

64         ///   查找类似 默认 的本地化字符串。

65         /// </summary>

66         internal static string Name {

67             get {

68                 return ResourceManager.GetString("Name", resourceCulture);

69             }

70         }

71     }

72 }

备注

如果产品做大了,就要使Resgen和AI来生成卫星资源程序集了,多数情况VS就够用了。 

每种UI技术都会在此之上进一步封装,另外客户端JS的多语言就需要另外的技术了(原型覆盖)。

 

你可能感兴趣的:(.net)