最近發現資源文件是個好東西, 用的好了能夠給開發人員節約很多的時間. 例如作一個多語言的網站, 資源文件就有不小的用處.app
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"); /// <summary> /// 取得實做語系 /// </summary> public static List<CultureItem> All { get { return _cultures; } } // 實做語系 private static readonly List<CultureItem> _cultures = new List<CultureItem> { ENU, CHS, CHT, //"en-GB", //"en-CA", //"de-DE", //"es-ES", //"it-IT", //"ja-JP", //"fr-FR", }; /// <summary> /// 驗證culture資料是否超出範圍 /// </summary> /// <param name="cultureName"></param> /// <returns></returns> public static string GetValidBigTrackerCulture(string cultureName) { //不確定預設是否為en - US, or cultureName的值超出預期的範圍 if (_cultures.Where(w => w.LanguageCultureName == cultureName).Count() == 0) { cultureName = ENU.LanguageCultureName; } return cultureName; } /// <summary> /// 依照「name」參數回傳有效並已實做之語系名稱。 /// 若無合適語系名稱,則回傳預設語系名稱。 /// 本專案的預設語系名稱為「en-US」 /// </summary> /// <param name="name">語系名稱</param> 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(); } /// <summary> /// 回傳預設的語系名稱 /// </summary> public static string GetDefaultCulture() { return ENU.LanguageCultureName; } /// <summary> /// 取得目前語系名稱 /// </summary> 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; } /// <summary> /// 取得目前的中性語系名稱 /// </summary> 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; } } /// <summary> /// 取得中性語系名稱 /// </summary> 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 }