2022-09-08 08:04:32 -07:00

298 lines
10 KiB
C#

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<string, Dictionary<string, string>> _lanCache;
private static Dictionary<string, string> GetLanCache()
{
_lanCache = _lanCache ?? new Dictionary<string, Dictionary<string, string>>();
if (!_lanCache.ContainsKey(LanCode))
{
_lanCache.Add(LanCode, new Dictionary<string, string>());
}
return _lanCache[LanCode];
}
private static Dictionary<string, string> GetDicFromFile(string file)
{
var dict = new Dictionary<string, string>();
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<string, Dictionary<string, string>> GetLanguageSettingsFromFile(string file)
{
var result = new Dictionary<string, Dictionary<string, string>>();
var dict = new Dictionary<string, string>();
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<string, string>();
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<string, Dictionary<string, string>>();
return _lanCache.ContainsKey(lanCode.ToUpper());
}
#region EnumerationGetText
//public static Dictionary<string, int> GetDropdownItemsFromEnum<T>(int start = -999) where T : struct, IConvertible
//{
// if (!typeof(T).IsEnum)
// {
// throw new ArgumentException("T must be an enumerated type");
// }
// var result = new Dictionary<string, int>();
// foreach (var k in Utility.EnumHelper<T>.GetDisplayNameAndValues(start).ToList().OrderBy(k => k.Value))
// {
// result.Add(GetText(k.Key.ToUpper()), k.Value);
// }
// return result;
//}
//public static Dictionary<string, int> GetDropdownItemsFromEnum<T>(List<int> allowList) where T : struct, IConvertible
//{
// if (!typeof(T).IsEnum)
// {
// throw new ArgumentException("T must be an enumerated type");
// }
// var result = new Dictionary<string, int>();
// foreach (var k in Utility.EnumHelper<T>.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<string, string> GetDropdownItemsTextFromEnum<T>(int start = -999) where T : struct, IConvertible
//{
// if (!typeof(T).IsEnum)
// {
// throw new ArgumentException("T must be an enumerated type");
// }
// var result = new Dictionary<string, string>();
// foreach (var k in Utility.EnumHelper<T>.GetDisplayNameAndValues(start).ToList().OrderBy(k => k.Value))
// {
// result.Add(GetText(k.Key.ToUpper()), k.Key);
// }
// return result;
//}
//public static string GetText<T>(T value) where T : struct, IConvertible
//{
// return GetText(Utility.EnumHelper<T>.GetDisplayValue(value));
//}
//public static SelectList GetSelectListFromEnum<T>(int seletedValue, int start = -999) where T : struct, IConvertible
//{
// return new SelectList(GetDropdownItemsFromEnum<T>(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 += $"<button class=\"btn btn-primary btn-sm\" type=\"button\" data-toggle=\"LanTextArea\" data-target=\"{text}\"><i class=\"fa fa-pencil\"></i></button> ";
}
return result;
return GetText(text).Replace("\n", "<br>").Replace(Environment.NewLine, "<br>");
}
public static Dictionary<string, string> GetLanguage()
{
Dictionary<string, string> result = new Dictionary<string, string>();
//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<System.Web.Mvc.SelectListItem> GetLanguageCategorys()
{
var dropItems = new List<System.Web.Mvc.SelectListItem>();
//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();
//}
}
}
}