using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace Chruch.Net { public static class LanHelper { private const string KEY_LAN = "_Lan"; private const string COOKIE_LAN = "lan"; public static string LanCode { get { if (System.Web.HttpContext.Current.Session[KEY_LAN] != null) { return (string)System.Web.HttpContext.Current.Session[KEY_LAN]; } else { return "en-us"; return "zh-tw"; //if (BLL.LanguageBLL.CheckEnable(BrowserLan.Name.ToLower())) //{ // LanCode = BrowserLan.Name.ToLower(); //} //else //{ // LanCode = BLL.LanguageBLL.GetDefaultLanguage(); //} } return System.Web.HttpContext.Current.Session[KEY_LAN].ToString(); } set { System.Web.HttpContext.Current.Session[KEY_LAN] = value.ToLower(); } } private static Dictionary> _lanCache; private static Dictionary GetLanCache() { _lanCache = _lanCache ?? new Dictionary>(); if (!_lanCache.ContainsKey(LanCode)) { _lanCache.Add(LanCode, new Dictionary()); } return _lanCache[LanCode]; } private static Dictionary GetDicFromFile(string file) { var dict = new Dictionary(); if (File.Exists(file)) { using (var sr = new FileInfo(file).OpenText()) { string s = sr.ReadLine(); while ((s != null)) { if (s.IndexOf("=") > 0) { s = s.Replace("\t", " "); while (s.IndexOf(" ") > 0) { s = s.Replace(" ", " "); } //清除掉TAB string[] ss = new string[2]; ss[0] = s.Remove(s.IndexOf("=")).ToUpper().Trim(); ss[1] = s.Substring(s.IndexOf("=") + 1); if (!dict.ContainsKey(ss[0])) { dict.Add(ss[0], ss[1].Trim()); } //key轉小寫 , 去除空白 } s = sr.ReadLine(); } } } return dict; } public static Dictionary> GetLanguageSettingsFromFile(string file) { var result = new Dictionary>(); var dict = new Dictionary(); if (File.Exists(file)) { using (var sr = new FileInfo(file).OpenText()) { string s = sr.ReadLine(); while ((s != null)) { if (s.IndexOf("=") > 0) { s = s.Replace("\t", " "); while (s.IndexOf(" ") > 0) { s = s.Replace(" ", " "); } //清除掉TAB string[] ss = new string[2]; ss[0] = s.Remove(s.IndexOf("=")).ToUpper().Trim(); ss[1] = s.Substring(s.IndexOf("=") + 1); if (!dict.ContainsKey(ss[0])) { dict.Add(ss[0], ss[1].Trim()); } //key轉小寫 , 去除空白 } else if (s.IndexOf("#") > -1) { dict = new Dictionary(); result.Add(s.Trim(), dict); } s = sr.ReadLine(); } } } else { return null; } return result; } public static void ClearCache() { _lanCache.Clear(); } public static bool CheckExit(string lanCode) { _lanCache = _lanCache ?? new Dictionary>(); return _lanCache.ContainsKey(lanCode.ToUpper()); } #region EnumerationGetText //public static Dictionary GetDropdownItemsFromEnum(int start = -999) where T : struct, IConvertible //{ // if (!typeof(T).IsEnum) // { // throw new ArgumentException("T must be an enumerated type"); // } // var result = new Dictionary(); // foreach (var k in Utility.EnumHelper.GetDisplayNameAndValues(start).ToList().OrderBy(k => k.Value)) // { // result.Add(GetText(k.Key.ToUpper()), k.Value); // } // return result; //} //public static Dictionary GetDropdownItemsFromEnum(List allowList) where T : struct, IConvertible //{ // if (!typeof(T).IsEnum) // { // throw new ArgumentException("T must be an enumerated type"); // } // var result = new Dictionary(); // foreach (var k in Utility.EnumHelper.GetDisplayNameAndValues().ToList().OrderBy(k => k.Value)) // { // if (allowList.Any(c => c == k.Value)) // { // result.Add(GetText(k.Key.ToUpper()), k.Value); // } // } // return result; //} //public static Dictionary GetDropdownItemsTextFromEnum(int start = -999) where T : struct, IConvertible //{ // if (!typeof(T).IsEnum) // { // throw new ArgumentException("T must be an enumerated type"); // } // var result = new Dictionary(); // foreach (var k in Utility.EnumHelper.GetDisplayNameAndValues(start).ToList().OrderBy(k => k.Value)) // { // result.Add(GetText(k.Key.ToUpper()), k.Key); // } // return result; //} //public static string GetText(T value) where T : struct, IConvertible //{ // return GetText(Utility.EnumHelper.GetDisplayValue(value)); //} //public static SelectList GetSelectListFromEnum(int seletedValue, int start = -999) where T : struct, IConvertible //{ // return new SelectList(GetDropdownItemsFromEnum(start), "Value", "Key", seletedValue); //} #endregion public static string GetText(this string text) { var lan = GetLanCache(); text = text.ToUpper(); if (lan.ContainsKey(text)) { return lan[text]; } else { return text; //using (var db = new DAL.HostnameRecordsContext()) //{ // string result = db.LanguageContents.Where(l => l.LanCode == LanCode && l.Key == text).Select(l => l.Value) // .FirstOrDefault(); // if (string.IsNullOrEmpty(result)) // { // return text; // return "(尚未設定文字)"; // } // else // { // return result; // } //} } } public static string GetHtml(this string text, bool displayEditBtn = false) { string result = GetText(text); if (displayEditBtn) { result += $" "; } return result; return GetText(text).Replace("\n", "
").Replace(Environment.NewLine, "
"); } public static Dictionary GetLanguage() { Dictionary result = new Dictionary(); //using (var db = new DAL.HostnameRecordsContext()) //{ // result = db.Languages.Select(l => new { l.LanCode, l.Name }) // .ToDictionary(t => t.LanCode, t => t.Name); //} return result; } public static List GetLanguageCategorys() { var dropItems = new List(); //dropItems.Add(new System.Web.Mvc.SelectListItem() {Text = "無分類", Value = ""}); //using (var db = new DAL.HostnameRecordsContext()) //{ // db.LanguageContentCategories.ToList().ForEach(x => dropItems.Add( // new System.Web.Mvc.SelectListItem() { Text = x.Name, Value = x.Id })); //} //if (dropItems.Count > 0) //{ // dropItems.First().Selected = true; //} return dropItems; } public static void DeleteLanguageContent(string key) { //using (var db = new DAL.HostnameRecordsContext()) //{ // var result = db.LanguageContents.Where(l => l.Key == key); // db.LanguageContents.RemoveRange(result); // db.SaveChanges(); //} } } }