using Tiger.Model;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Rhea.Common;
|
using System.Net;
|
using System.Net.Http;
|
using System.Reflection;
|
using System.Linq;
|
using Apache.NMS.ActiveMQ.Commands;
|
using System.IO;
|
using Newtonsoft.Json.Linq;
|
using NetTaste;
|
using System.Xml.Linq;
|
using Tiger.IBusiness;
|
using System.Data;
|
using Tiger.Model.Entitys.MES.Position;
|
|
namespace Tiger.Business
|
{
|
public partial class Biz
|
{
|
/// <summary>
|
/// 多语言翻译,按BizSettings设置的默认语言(DefaultLanguage)翻译
|
/// </summary>
|
/// <param name="key"></param>
|
/// <param name="args"></param>
|
/// <returns></returns>
|
public static string T(string key, params object?[] args) => new Locale(key, args).Default();
|
/// <summary>
|
/// 多语言翻译,按传入的语言翻译,传入语言为空则按BizSettings设置的默认语言(DefaultLanguage)翻译
|
/// </summary>
|
/// <param name="locale"></param>
|
/// <param name="language"></param>
|
/// <returns></returns>
|
public static string T(Locale locale, string language) => language.IsNullOrEmpty() ? locale.Default() : locale[language];
|
/// <summary>
|
/// 多语言对象(未翻译)
|
/// </summary>
|
/// <param name="key"></param>
|
/// <param name="args"></param>
|
/// <returns></returns>
|
public static Locale L(string key, params object?[] args) => new Locale(key, args);
|
/// <summary>
|
/// 国际化语言包
|
/// </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();
|
/// <summary>
|
/// 获取语言包字典
|
/// </summary>
|
/// <returns></returns>
|
public static Dictionary<string, Dictionary<string, string>> InitDictionary()
|
{
|
_dictionary = new();
|
try
|
{
|
#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 key = row["Key"].ToString();
|
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);
|
}
|
}
|
#endregion
|
return _dictionary;
|
}
|
catch (Exception ex)
|
{
|
Logger.Default.Fatal(ex, "获取语言包字典异常");
|
return new();
|
}
|
}
|
|
/// <summary>
|
/// 获取字典词条
|
/// </summary>
|
/// <param name="root"></param>
|
/// <param name="prefix"></param>
|
/// <returns></returns>
|
private static void GetDicItem(JToken root, string prefix)
|
{
|
if (root.Type == JTokenType.Object)
|
{
|
foreach (var property in root.Children<JProperty>())
|
{
|
GetDicItem(property.Value, prefix);
|
}
|
}
|
else if (root.Type == JTokenType.Array)
|
{
|
foreach (var item in root.Children<JToken>())
|
{
|
GetDicItem(item, prefix);
|
}
|
}
|
else //if (root.Type == JTokenType.Property || root.Type == JTokenType.String || root.Type == JTokenType.Integer || root.Type == JTokenType.Float || root.Type == JTokenType.Boolean || root.Type == JTokenType.Date || root.Type == JTokenType.Bytes)
|
{
|
var prefixs = ($"{prefix}.{root.Path}").Split('.');
|
var key = string.Join(".", prefixs.SkipLast(1));
|
if (!_dictionary.ContainsKey(key))
|
{
|
_dictionary.Add(key, new());
|
}
|
var keyDic = _dictionary[key];
|
if (_dictionary.ContainsKey(prefixs.Last()))
|
{
|
keyDic[prefixs.Last()] = root.ToString();
|
}
|
else
|
{
|
keyDic.Add(prefixs.Last(), root.ToString());
|
}
|
}
|
}
|
|
/// <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
|
{
|
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 parent = lang.Replace($"{declaringType.Namespace}.{declaringType.Name}.", "").Replace($".json", "").Split(".", StringSplitOptions.RemoveEmptyEntries);
|
var node = result;
|
foreach (var item in parent)
|
{
|
if (node.ContainsKey(item))
|
{
|
node = node[item] as JObject;
|
}
|
else
|
{
|
var itemNode = new JObject();
|
node.Add(item, itemNode);
|
node = itemNode;
|
}
|
}
|
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(lang), Encoding.UTF8))
|
{
|
string langJson = reader.ReadToEnd();
|
if (!langJson.IsNullOrEmpty())
|
{
|
JObject objs = SelectLocale(JObject.Parse(langJson), locale ?? "");
|
foreach (var obj in objs)
|
{
|
node.Add(obj.Key, obj.Value);
|
}
|
}
|
}
|
}
|
return result.ToString();
|
}
|
catch (Exception ex)
|
{
|
return ex.StackTraceMessage();
|
}
|
}
|
|
/// <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>
|
/// <param name="locale"></param>
|
/// <returns></returns>
|
private static JObject SelectLocale(JObject json, string locale)
|
{
|
var result = new JObject();
|
foreach (var obj in json)
|
{
|
if (obj.Value.HasValues && obj.Value.First().HasValues && !obj.Value.First().First().HasValues)
|
{
|
if((obj.Value as JObject).ContainsKey(locale))
|
{
|
result.Add(obj.Key, obj.Value[locale].ToString());
|
}
|
else
|
{
|
result.Add(obj.Key, obj.Value.First().First().ToString());
|
//result.Add(obj.Key, SelectLocale(obj.Value as JObject, locale));
|
}
|
}
|
else
|
{
|
if (obj.Value.Type == JTokenType.Object)
|
{
|
result.Add(obj.Key, SelectLocale(obj.Value as JObject, locale));
|
}
|
else
|
{
|
result.Add(obj.Key, obj.Value?.ToString());
|
}
|
}
|
}
|
return result;
|
}
|
}
|
}
|
}
|