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;
|
|
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>
|
/// 多语言对象(未翻译)
|
/// </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 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
|
{
|
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);
|
}
|
}
|
}
|
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)
|
{
|
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="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;
|
}
|
}
|
}
|
}
|