.NET:國際化和本地化


背景

國際化(i18n)和本地化(l10n)是高端程序的必備技術,可惜從業五年從沒有嘗試過,下一步准備做一個多用戶的博客系統,想支持多語言,今天就學習了一下,寫出來,希望大家批評。

收集的資料

一些知識點

  • CultureInfo有兩部分組成:語言和區域,如en是語言,US是美國區域,標准的區域格式是:語言-區域(en-US)。
  • ResourceManager默認會根據當前線程的CurrentUICulture來返回資源,查找的路徑依次是:bin\語言-區域\類庫或應用程序.resources.dll、bin\語言\類庫或應用程序.resources.dll,如果沒有找到就去“資源名稱.resx”中查找。
  • 當前線程的CurrentCulture只對數據的格式化和排序有影響。

使用資源文件和衛星程序集

代碼下載:http://yunpan.cn/QnJezZmVV8LL5

感謝Visual Studio,它幫我們做了很多工作。

項目結構

測試代碼

 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 }

輸出結果

 

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的多語言就需要另外的技術了(原型覆蓋)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM