最近发现资源文件是个好东西, 用的好了可以给开发人员节约不少的时间. 例如做一个多语言的网站, 资源文件就有不小的用处.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | public class Culture { public static CultureItem ENU = new CultureItem("en-US", "English", "EN"); public static CultureItem CHS = new CultureItem("zh-CN", "簡体中文", "CN"); public static CultureItem CHT = new CultureItem("zh-TW", "繁體中文", "TW"); public static CultureItem JPN = new CultureItem("ja-JP", "日本語", "JP"); public static CultureItem DE = new CultureItem("de-DE", "Deutsch", "DE"); public static CultureItem FR = new CultureItem("fr-FR", "French", "FR"); public static CultureItem ES = new CultureItem("es-ES", "Spanish", "ES"); public static CultureItem IT = new CultureItem("it-IT", "Italian", "IT"); /// /// 取得實作語系 /// public static List { get { return _cultures; } } // 實作語系 private static readonly List { ENU, CHS, CHT, //"en-GB", //"en-CA", //"de-DE", //"es-ES", //"it-IT", //"ja-JP", //"fr-FR", }; /// /// 驗證culture資料是否超出範圍 /// /// /// public static string GetValidBigTrackerCulture(string cultureName) { //不確定預設是否為en - US, or cultureName的值超出預期的範圍 if (_cultures.Where(w => w.LanguageCultureName == cultureName).Count() == 0) { cultureName = ENU.LanguageCultureName; } return cultureName; } /// /// 依照「name」參數回傳有效並已實作之語系名稱。 /// 若無合適語系名稱,則回傳預設語系名稱。 /// 本專案的預設語系名稱為「en-US」 /// /// 語系名稱 public static string GetImplementedCulture(string name) { // 確認是否為空字串 if (string.IsNullOrEmpty(name)) return GetDefaultCulture(); // 若是空字串則回傳預設語系 // 如果該語系名稱已被實作,則接受使用該語系名稱 var selectedCulture = _cultures.FirstOrDefault(c => c.LanguageCultureName.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (selectedCulture != null) { return selectedCulture.LanguageCultureName; // 接受這個語系 } // 取得最接近之語系名稱。例如,如果已經實作了「en-US」而使用者的請求是「en-GB」, // 則回傳最接近的「en-US」因為這樣至少是相同的語言(例如:英文) var n = GetNeutralCulture(name); foreach (var c in _cultures) if (c.LanguageCultureName.StartsWith(n)) return c.LanguageCultureName; // 如果沒有合適的,就回傳預設語系名稱 return GetDefaultCulture(); } /// /// 回傳預設的語系名稱 /// public static string GetDefaultCulture() { return ENU.LanguageCultureName; } /// /// 取得目前語系名稱 /// public static string GetCurrentCulture() { return Thread.CurrentThread.CurrentCulture.Name; } public static bool IsTW { get { return GetCurrentCulture().Equals(CHT.LanguageCultureName, StringComparison.OrdinalIgnoreCase); } } public static CultureInfo GetCurrentCultureInfo() { return Thread.CurrentThread.CurrentCulture; } /// /// 取得目前的中性語系名稱 /// public static string GetCurrentNeutralCulture() { return GetNeutralCulture(Thread.CurrentThread.CurrentCulture.Name); } public static void SetCurrentCulture(string userCulture) { //驗證culture是否超出系統支援範圍 userCulture = GetValidBigTrackerCulture(userCulture); if (Thread.CurrentThread.CurrentCulture.Name != userCulture) { Thread.CurrentThread.CurrentCulture = new CultureInfo(userCulture); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } } /// /// 取得中性語系名稱 /// public static string GetNeutralCulture(string name) { if (name.Length < 2) return name; return name.Substring(0, 2); // 回傳前兩個字元,例如:"en", "es" } public static string cookieName { get { return Models.Enum.cookieName.BulidcookieKey(Models.Enum.cookieName.Culture); } } } |
1 using BigZata.Common;
2 using BigZata.Resources.Mapping.TitleMapping;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Threading;
8 using System.Threading.Tasks;
9
10 namespace Resources
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 Console.Write(Thread.CurrentThread.CurrentCulture);
17
18 // System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-us", true);
19 // Thread.CurrentThread.CurrentCulture = culture;
20 Culture.SetCurrentCulture("en-us");
21
22 Console.Write(Thread.CurrentThread.CurrentCulture);
23 // System.Globalization.CultureInfo Culture=new System.Globalization.CultureInfo
24 Console.Write(TitleMapping.LandingPage);
25 Console.ReadKey();
26 }
27 }
28 }


