LanguageSetting.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Collections;
  2. using System.Collections.Concurrent;
  3. using System.Diagnostics;
  4. using Newtonsoft.Json.Linq;
  5. using WingServerCommon.Config.Parameters;
  6. using WingServerCommon.Log;
  7. namespace WingServerCommon.Config
  8. {
  9. public class LanguageSetting
  10. {
  11. private static ConcurrentDictionary<string, IList<DataItemParameter>> _parameters;
  12. private static ConcurrentDictionary<string, IParameter> _params;
  13. /// <summary>
  14. /// 更新后的委托通知
  15. /// </summary>
  16. public static event EventHandler<LanguageConfigArgs> LanguageConfigEvent;
  17. /// <summary>
  18. /// Init load appsetting
  19. /// </summary>
  20. static LanguageSetting()
  21. {
  22. _parameters = new ConcurrentDictionary<string, IList<DataItemParameter>>();
  23. _params = new ConcurrentDictionary<string, IParameter>();
  24. AddOrUpdateLanguageConfig();
  25. }
  26. /// <summary>
  27. /// Add or Update Config File
  28. /// </summary>
  29. /// <param name="filePath">Config File Path</param>
  30. public static void AddOrUpdateLanguageConfig()
  31. {
  32. var languageJson = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge", "LanguageConfig.json"));
  33. JObject obj = JObject.Parse(languageJson);
  34. var languageKey = "LanguageConfigs";
  35. var regionKey = "RegionConfigs";
  36. var languageConfigs = obj["SystemLanguageConfig"][languageKey].ToObject<List<DataItemParameter>>();
  37. var regionConfigs = obj["SystemLanguageConfig"][regionKey].ToObject<List<DataItemParameter>>();
  38. //替换原先的值_parameters
  39. if (_parameters?.Count > 0)
  40. {
  41. if (languageConfigs != null && _parameters.ContainsKey(languageKey))
  42. {
  43. _parameters[languageKey] = languageConfigs;
  44. }
  45. if (regionConfigs != null && _parameters.ContainsKey(regionKey))
  46. {
  47. _parameters[regionKey] = regionConfigs;
  48. }
  49. }
  50. else
  51. {
  52. _parameters.TryAdd(languageKey, languageConfigs);
  53. _parameters.TryAdd(regionKey, regionConfigs);
  54. }
  55. ReloadPatientInfoConfig();
  56. //找到对应的token信息,发送通知
  57. var args = new LanguageConfigArgs();
  58. args.ConfigDictionaryKeys = _parameters.Keys.ToList();
  59. if (args.ConfigDictionaryKeys?.Count > 0)
  60. {
  61. LanguageConfigEvent?.Invoke(null, args);
  62. }
  63. }
  64. /// <summary>
  65. /// Add or Update Config File
  66. /// </summary>
  67. /// <param name="filePath">Config File Path</param>
  68. public static void AddOrUpdateServerStorageConfig(string key, List<DataItemParameter> values)
  69. {
  70. //替换原先的值_parameters
  71. if (_parameters?.Count > 0)
  72. {
  73. if (values != null && _parameters.ContainsKey(key))
  74. {
  75. _parameters[key] = values;
  76. }
  77. else
  78. {
  79. _parameters.TryAdd(key, values);
  80. }
  81. }
  82. else
  83. {
  84. _parameters.TryAdd(key, values);
  85. }
  86. }
  87. /// <summary>
  88. /// Get parameter by specifed section name
  89. /// </summary>
  90. /// <typeparam name="sectionName"></typeparam>
  91. /// <returns></returns>
  92. public static IDictionary<string, string> GetLanguageConfigParammeter(string sectionName)
  93. {
  94. if (_parameters.ContainsKey(sectionName))
  95. {
  96. _parameters.TryGetValue(sectionName, out IList<DataItemParameter> _value);
  97. var dic = new Dictionary<string, string>();
  98. if(_value?.Count > 0)
  99. {
  100. foreach (var item in _value)
  101. {
  102. if (!dic.Keys.Contains(item.Key))
  103. {
  104. dic.Add(item.Key, item.Value);
  105. }
  106. else
  107. {
  108. dic[item.Key] = item.Value;
  109. }
  110. }
  111. }
  112. return dic;
  113. }
  114. else
  115. {
  116. Logger.WriteLineWarn($"LanguageSetting GetLanguageConfigParammeter key is not exist, sectionName: {sectionName} not set correct");
  117. return new Dictionary<string, string>();
  118. }
  119. throw new System.ArgumentException($"{sectionName} not set correct");
  120. }
  121. /// <summary>
  122. /// Get parameter by specifed section name and return type
  123. /// </summary>
  124. /// <typeparam name="sectionName"></typeparam>
  125. /// <typeparam name="isReturnVal"></typeparam>
  126. /// <returns></returns>
  127. public static IList<string> GetLanguageConfigParammeter(string sectionName, bool isReturnVal)
  128. {
  129. if (_parameters.ContainsKey(sectionName))
  130. {
  131. _parameters.TryGetValue(sectionName, out IList<DataItemParameter> _value);
  132. var resultData = new List<string>();
  133. if (_value?.Count > 0)
  134. {
  135. if (isReturnVal)
  136. {
  137. resultData = _value.Select(c => c.Value).ToList();
  138. }
  139. else
  140. {
  141. resultData = _value.Select(c => c.Key).ToList();
  142. }
  143. }
  144. return resultData;
  145. }
  146. else
  147. {
  148. Logger.WriteLineWarn($"LanguageSetting GetLanguageConfigParammeter key is not exist, sectionName: {sectionName} not set correct");
  149. return new List<string>();
  150. }
  151. throw new System.ArgumentException($"{sectionName} not set correct");
  152. }
  153. /// <summary>
  154. /// reload patient Info
  155. /// </summary>
  156. /// <returns></returns>
  157. private static void ReloadPatientInfoConfig()
  158. {
  159. var languageData = GetLanguageConfigParammeter("LanguageConfigs", true);
  160. if (languageData?.Count > 0)
  161. {
  162. foreach(var item in languageData)
  163. {
  164. var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge", item, "patientInfo.json");
  165. AddOrUpdateConfig(configFilePath, item);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// Add or Update Config File
  171. /// </summary>
  172. private static void AddOrUpdateConfig(string filePath, string language)
  173. {
  174. var _newParameters = ParameterDictionarySerializer.Deserialize(filePath);
  175. //替换原先的值_parameters
  176. if (_newParameters?.Count > 0)
  177. {
  178. foreach(var key in _newParameters.Keys)
  179. {
  180. var newKey = language + "_" + key;
  181. if (_params != null && _params.ContainsKey(newKey))
  182. {
  183. _params[newKey] = _newParameters[key];
  184. }
  185. else
  186. {
  187. if (_params == null)
  188. {
  189. _params = new ConcurrentDictionary<string, IParameter>();
  190. }
  191. _params.TryAdd(newKey, _newParameters[key]);
  192. }
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// Get parameter by specifed section name and parameter name and language
  198. /// </summary>
  199. /// <typeparam name="T"></typeparam>
  200. /// <returns></returns>
  201. public static T GetParammeter<T>(string language, string sectionName, string paramName) where T : class
  202. {
  203. var key = GetAdapterKey(language, sectionName, paramName);
  204. if (_params.ContainsKey(key))
  205. {
  206. var res = _params.TryGetValue(key, out IParameter _value);
  207. if (_value != null)
  208. {
  209. var parameter = _value as T;
  210. if (parameter != null)
  211. {
  212. return parameter;
  213. }
  214. else
  215. {
  216. Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
  217. }
  218. }
  219. else
  220. {
  221. Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
  222. }
  223. }
  224. else
  225. {
  226. Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
  227. }
  228. throw new System.ArgumentException($"{language} - {sectionName} - {paramName} not set correct");
  229. }
  230. /// <summary>
  231. /// 获取适配Key
  232. /// </summary>
  233. /// <typeparam name="language"></typeparam>
  234. /// <typeparam name="sectionName"></typeparam>
  235. /// <typeparam name="paramName"></typeparam>
  236. /// <returns>最优Key</returns>
  237. private static string GetAdapterKey(string language, string sectionName, string paramName)
  238. {
  239. var key = language + "_" + sectionName + "_" + paramName;
  240. try
  241. {
  242. if (_params.Count > 0 && !_params.ContainsKey(key))
  243. {
  244. if (!string.IsNullOrEmpty(language) && language.Split('-').Length > 1)
  245. {
  246. var prefixLang = language.Split('-')[0] + "-";
  247. var keys = _params.Keys.Where(c => c.Contains(prefixLang));
  248. foreach (var keyItem in keys)
  249. {
  250. var filterStr = "_" + sectionName + "_" + paramName;
  251. if (keyItem.Contains(filterStr))
  252. {
  253. key = keyItem;
  254. break;
  255. }
  256. }
  257. }
  258. if (!_params.ContainsKey(key))
  259. {
  260. language = "en-US";
  261. key = language + "_" + sectionName + "_" + paramName;
  262. }
  263. }
  264. return key;
  265. }
  266. catch(Exception ex)
  267. {
  268. Logger.WriteLineWarn($"LanguageSetting GetAdapterKey key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
  269. language = "en-US";
  270. key = language + "_" + sectionName + "_" + paramName;
  271. return key;
  272. }
  273. }
  274. }
  275. /// <summary>
  276. /// 委托参数
  277. /// </summary>
  278. public class LanguageConfigArgs : EventArgs
  279. {
  280. /// <summary>
  281. /// 验证结果列表
  282. /// </summary>
  283. public IList<string> ConfigDictionaryKeys { get; set; } = new List<string>();
  284. }
  285. /// <summary>
  286. /// 参数项实体
  287. /// </summary>
  288. public class DataItemParameter
  289. {
  290. /// <summary>
  291. /// Key
  292. /// </summary>
  293. /// <value></value>
  294. public string Key { get; set; } = string.Empty;
  295. /// <summary>
  296. /// Value
  297. /// </summary>
  298. /// <value></value>
  299. public string Value { get; set; } = string.Empty;
  300. }
  301. }