| | |
| | | using NetTaste; |
| | | using System.Xml.Linq; |
| | | using Tiger.IBusiness; |
| | | using System.Data; |
| | | |
| | | namespace Tiger.Business |
| | | { |
| | |
| | | /// </summary> |
| | | public class Language : ILanguage |
| | | { |
| | | private static string curLocale => BizConfig.DefaultLanguage; |
| | | private static DataTable langDT; |
| | | private static Dictionary<string, Dictionary<string, string>> _dictionary = InitDictionary(); |
| | | public static Dictionary<string, Dictionary<string, string>> Dictionary => _dictionary; |
| | | public Dictionary<string, Dictionary<string, string>> GetDictionary() => InitDictionary(); |
| | |
| | | _dictionary = new(); |
| | | try |
| | | { |
| | | var assembly = Assembly.GetExecutingAssembly(); |
| | | var declaringType = MethodBase.GetCurrentMethod().DeclaringType; |
| | | var resource = assembly.GetManifestResourceNames().ToList(); |
| | | var langs = resource.Where(q => q.StartsWith($"{declaringType.Namespace}.{declaringType.Name}")).OrderBy(q => q).ToList(); |
| | | foreach (var lang in langs) |
| | | #region From Resource |
| | | //var assembly = Assembly.GetExecutingAssembly(); |
| | | //var declaringType = MethodBase.GetCurrentMethod().DeclaringType; |
| | | //var resource = assembly.GetManifestResourceNames().ToList(); |
| | | //var langs = resource.Where(q => q.StartsWith($"{declaringType.Namespace}.{declaringType.Name}")).OrderBy(q => q).ToList(); |
| | | //foreach (var lang in langs) |
| | | //{ |
| | | // var prefix = lang.Replace($"{declaringType.Namespace}.{declaringType.Name}.", "").Replace($".json", ""); |
| | | // using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(lang), Encoding.UTF8)) |
| | | // { |
| | | // string langJson = reader.ReadToEnd(); |
| | | // if (!langJson.IsNullOrEmpty()) |
| | | // { |
| | | // GetDicItem(JObject.Parse(langJson), prefix); |
| | | // } |
| | | // } |
| | | //} |
| | | #endregion |
| | | |
| | | #region From DB |
| | | var langFile = new FileInfo(@$"{AppDomain.CurrentDomain.BaseDirectory}Language.db"); |
| | | var db = new Database() { Name = langFile.Name, Type = SqlSugar.DbType.Sqlite, ConnectionString = @$"DataSource={langFile.FullName}" }.Client; |
| | | |
| | | langDT = db.Queryable<dynamic>().AS("Dictionary").ToDataTable(); |
| | | foreach (DataRow row in langDT.Rows) |
| | | { |
| | | var prefix = lang.Replace($"{declaringType.Namespace}.{declaringType.Name}.", "").Replace($".json", ""); |
| | | using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(lang), Encoding.UTF8)) |
| | | var key = row["Key"].ToString(); |
| | | var dic = new Dictionary<string, string>(); |
| | | foreach (DataColumn col in langDT.Columns) |
| | | { |
| | | string langJson = reader.ReadToEnd(); |
| | | if (!langJson.IsNullOrEmpty()) |
| | | if (col.ColumnName.StartsWith("L@")) |
| | | { |
| | | GetDicItem(JObject.Parse(langJson), prefix); |
| | | dic.Add(col.ColumnName.Replace("L@", ""), row[col].ToString()); |
| | | } |
| | | } |
| | | if (_dictionary.ContainsKey(key)) |
| | | { |
| | | _dictionary[key] = dic; |
| | | } |
| | | else |
| | | { |
| | | _dictionary.Add(key, dic); |
| | | } |
| | | } |
| | | #endregion |
| | | return _dictionary; |
| | | } |
| | | catch (Exception ex) |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取语言包 |
| | | /// </summary> |
| | | /// <param name="locale"></param> |
| | | /// <returns></returns> |
| | | public string GetJson(string locale) |
| | | { |
| | | //return GetJsonFromResource(locale); |
| | | return GetJsonFromDB(locale); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取语言包 |
| | | /// </summary> |
| | | /// <param name="locale"></param> |
| | | /// <returns></returns> |
| | | public string GetJsonFromResource(string locale) |
| | | { |
| | | var result = new JObject(); |
| | | try |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取本地语言包 |
| | | /// </summary> |
| | | /// <param name="locale"></param> |
| | | /// <returns></returns> |
| | | public static string GetJsonFromDB(string locale) |
| | | { |
| | | try |
| | | { |
| | | //从本地获取语言包 |
| | | var result = new JObject(); |
| | | var langFile = new FileInfo(@$"{AppDomain.CurrentDomain.BaseDirectory}Language.db"); |
| | | if (!Biz.DataSource.Exists(langFile.Name)) |
| | | { |
| | | Biz.DataSource.Add(new Database() |
| | | { |
| | | Name = langFile.Name, |
| | | Type = SqlSugar.DbType.Sqlite, |
| | | ConnectionString = @$"DataSource={langFile.FullName}", |
| | | }); |
| | | } |
| | | var db = Biz.DataSource[langFile.Name].Client; |
| | | langDT = db.Queryable<dynamic>().AS("Dictionary").ToDataTable(); |
| | | foreach (DataRow row in langDT.Rows) |
| | | { |
| | | var key = row["Key"].ToString(); |
| | | if (key == "routes.demo.editor.editor") |
| | | { |
| | | ; |
| | | } |
| | | var parent = key.Split(".", StringSplitOptions.RemoveEmptyEntries); |
| | | var node = result; |
| | | var dic = new Dictionary<string, string>(); |
| | | foreach (DataColumn col in langDT.Columns) |
| | | { |
| | | if (col.ColumnName.StartsWith("L@")) |
| | | { |
| | | dic.Add(col.ColumnName.Replace("L@", ""), row[col].ToString()); |
| | | } |
| | | } |
| | | if (_dictionary.ContainsKey(key)) |
| | | { |
| | | _dictionary[key] = dic; |
| | | } |
| | | else |
| | | { |
| | | _dictionary.Add(key, dic); |
| | | } |
| | | for (int i = 0; i < parent.Length; i++) |
| | | { |
| | | if (node.ContainsKey(parent[i])) |
| | | { |
| | | node = node[parent[i]] as JObject; |
| | | } |
| | | else |
| | | { |
| | | if (i + 1 == parent.Length) |
| | | { |
| | | var value = dic.ContainsKey(locale) ? dic[locale] : dic.First().Value; |
| | | node.Add(parent[i], value.ToString()); |
| | | } |
| | | else |
| | | { |
| | | var itemNode = new JObject(); |
| | | node.Add(parent[i], itemNode); |
| | | node = itemNode; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return result.ToString(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return ex.StackTraceMessage(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 选择语言 |
| | | /// </summary> |
| | | /// <param name="json"></param> |