服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-05-30 7e25ed322740ed337296a990bac67e95bc250ac0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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;
            }
        }
    }
}