123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using Newtonsoft.Json.Linq;
- using WingServerCommon.Config.Parameters;
- using WingServerCommon.Log;
- namespace WingServerCommon.Config
- {
- public class LanguageSetting
- {
- private static ConcurrentDictionary<string, IList<DataItemParameter>> _parameters;
- private static ConcurrentDictionary<string, IParameter> _params;
- /// <summary>
- /// 更新后的委托通知
- /// </summary>
- public static event EventHandler<LanguageConfigArgs> LanguageConfigEvent;
- /// <summary>
- /// Init load appsetting
- /// </summary>
- static LanguageSetting()
- {
- _parameters = new ConcurrentDictionary<string, IList<DataItemParameter>>();
- _params = new ConcurrentDictionary<string, IParameter>();
- AddOrUpdateLanguageConfig();
- }
- /// <summary>
- /// Add or Update Config File
- /// </summary>
- /// <param name="filePath">Config File Path</param>
- public static void AddOrUpdateLanguageConfig()
- {
- var languageJson = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge", "LanguageConfig.json"));
- JObject obj = JObject.Parse(languageJson);
- var languageKey = "LanguageConfigs";
- var regionKey = "RegionConfigs";
- var languageConfigs = obj["SystemLanguageConfig"][languageKey].ToObject<List<DataItemParameter>>();
- var regionConfigs = obj["SystemLanguageConfig"][regionKey].ToObject<List<DataItemParameter>>();
- //替换原先的值_parameters
- if (_parameters?.Count > 0)
- {
- if (languageConfigs != null && _parameters.ContainsKey(languageKey))
- {
- _parameters[languageKey] = languageConfigs;
- }
- if (regionConfigs != null && _parameters.ContainsKey(regionKey))
- {
- _parameters[regionKey] = regionConfigs;
- }
- }
- else
- {
- _parameters.TryAdd(languageKey, languageConfigs);
- _parameters.TryAdd(regionKey, regionConfigs);
- }
- ReloadPatientInfoConfig();
- //找到对应的token信息,发送通知
- var args = new LanguageConfigArgs();
- args.ConfigDictionaryKeys = _parameters.Keys.ToList();
- if (args.ConfigDictionaryKeys?.Count > 0)
- {
- LanguageConfigEvent?.Invoke(null, args);
- }
- }
- /// <summary>
- /// Add or Update Config File
- /// </summary>
- /// <param name="filePath">Config File Path</param>
- public static void AddOrUpdateServerStorageConfig(string key, List<DataItemParameter> values)
- {
- //替换原先的值_parameters
- if (_parameters?.Count > 0)
- {
- if (values != null && _parameters.ContainsKey(key))
- {
- _parameters[key] = values;
- }
- else
- {
- _parameters.TryAdd(key, values);
- }
- }
- else
- {
- _parameters.TryAdd(key, values);
- }
- }
- /// <summary>
- /// Get parameter by specifed section name
- /// </summary>
- /// <typeparam name="sectionName"></typeparam>
- /// <returns></returns>
- public static IDictionary<string, string> GetLanguageConfigParammeter(string sectionName)
- {
- if (_parameters.ContainsKey(sectionName))
- {
- _parameters.TryGetValue(sectionName, out IList<DataItemParameter> _value);
- var dic = new Dictionary<string, string>();
- if(_value?.Count > 0)
- {
- foreach (var item in _value)
- {
- if (!dic.Keys.Contains(item.Key))
- {
- dic.Add(item.Key, item.Value);
- }
- else
- {
- dic[item.Key] = item.Value;
- }
- }
- }
- return dic;
- }
- else
- {
- Logger.WriteLineWarn($"LanguageSetting GetLanguageConfigParammeter key is not exist, sectionName: {sectionName} not set correct");
- return new Dictionary<string, string>();
- }
- throw new System.ArgumentException($"{sectionName} not set correct");
- }
- /// <summary>
- /// Get parameter by specifed section name and return type
- /// </summary>
- /// <typeparam name="sectionName"></typeparam>
- /// <typeparam name="isReturnVal"></typeparam>
- /// <returns></returns>
- public static IList<string> GetLanguageConfigParammeter(string sectionName, bool isReturnVal)
- {
- if (_parameters.ContainsKey(sectionName))
- {
- _parameters.TryGetValue(sectionName, out IList<DataItemParameter> _value);
- var resultData = new List<string>();
- if (_value?.Count > 0)
- {
- if (isReturnVal)
- {
- resultData = _value.Select(c => c.Value).ToList();
- }
- else
- {
- resultData = _value.Select(c => c.Key).ToList();
- }
- }
- return resultData;
- }
- else
- {
- Logger.WriteLineWarn($"LanguageSetting GetLanguageConfigParammeter key is not exist, sectionName: {sectionName} not set correct");
- return new List<string>();
- }
- throw new System.ArgumentException($"{sectionName} not set correct");
- }
-
- /// <summary>
- /// reload patient Info
- /// </summary>
- /// <returns></returns>
- private static void ReloadPatientInfoConfig()
- {
- var languageData = GetLanguageConfigParammeter("LanguageConfigs", true);
- if (languageData?.Count > 0)
- {
- foreach(var item in languageData)
- {
- var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge", item, "patientInfo.json");
- AddOrUpdateConfig(configFilePath, item);
- }
- }
- }
- /// <summary>
- /// Add or Update Config File
- /// </summary>
- private static void AddOrUpdateConfig(string filePath, string language)
- {
- var _newParameters = ParameterDictionarySerializer.Deserialize(filePath);
- //替换原先的值_parameters
- if (_newParameters?.Count > 0)
- {
- foreach(var key in _newParameters.Keys)
- {
- var newKey = language + "_" + key;
- if (_params != null && _params.ContainsKey(newKey))
- {
- _params[newKey] = _newParameters[key];
- }
- else
- {
- if (_params == null)
- {
- _params = new ConcurrentDictionary<string, IParameter>();
- }
- _params.TryAdd(newKey, _newParameters[key]);
- }
- }
- }
- }
- /// <summary>
- /// Get parameter by specifed section name and parameter name and language
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static T GetParammeter<T>(string language, string sectionName, string paramName) where T : class
- {
- var key = GetAdapterKey(language, sectionName, paramName);
- if (_params.ContainsKey(key))
- {
- var res = _params.TryGetValue(key, out IParameter _value);
- if (_value != null)
- {
- var parameter = _value as T;
- if (parameter != null)
- {
- return parameter;
- }
- else
- {
- Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
- }
- }
- else
- {
- Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
- }
- }
- else
- {
- Logger.WriteLineWarn($"LanguageSetting GetParammeter key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
- }
- throw new System.ArgumentException($"{language} - {sectionName} - {paramName} not set correct");
- }
- /// <summary>
- /// 获取适配Key
- /// </summary>
- /// <typeparam name="language"></typeparam>
- /// <typeparam name="sectionName"></typeparam>
- /// <typeparam name="paramName"></typeparam>
- /// <returns>最优Key</returns>
- private static string GetAdapterKey(string language, string sectionName, string paramName)
- {
- var key = language + "_" + sectionName + "_" + paramName;
- try
- {
- if (_params.Count > 0 && !_params.ContainsKey(key))
- {
- if (!string.IsNullOrEmpty(language) && language.Split('-').Length > 1)
- {
- var prefixLang = language.Split('-')[0] + "-";
- var keys = _params.Keys.Where(c => c.Contains(prefixLang));
- foreach (var keyItem in keys)
- {
- var filterStr = "_" + sectionName + "_" + paramName;
- if (keyItem.Contains(filterStr))
- {
- key = keyItem;
- break;
- }
- }
- }
- if (!_params.ContainsKey(key))
- {
- language = "en-US";
- key = language + "_" + sectionName + "_" + paramName;
- }
- }
- return key;
- }
- catch(Exception ex)
- {
- Logger.WriteLineWarn($"LanguageSetting GetAdapterKey key is not exist, language: {language} - sectionName: {sectionName} - paramName: {paramName} not set correct");
- language = "en-US";
- key = language + "_" + sectionName + "_" + paramName;
- return key;
- }
- }
- }
- /// <summary>
- /// 委托参数
- /// </summary>
- public class LanguageConfigArgs : EventArgs
- {
- /// <summary>
- /// 验证结果列表
- /// </summary>
- public IList<string> ConfigDictionaryKeys { get; set; } = new List<string>();
- }
- /// <summary>
- /// 参数项实体
- /// </summary>
- public class DataItemParameter
- {
- /// <summary>
- /// Key
- /// </summary>
- /// <value></value>
- public string Key { get; set; } = string.Empty;
- /// <summary>
- /// Value
- /// </summary>
- /// <value></value>
- public string Value { get; set; } = string.Empty;
- }
- }
|