LocalizedTextureCfgMgr.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public class LocalizedSpriteCfg
  7. {
  8. public string key;
  9. public string Language;
  10. public string AssetPath;
  11. public string OtherSetting;
  12. public string SetNativeSize;
  13. public LocalizedSpriteCfg()
  14. {
  15. key = "";
  16. Language = "";
  17. AssetPath = "";
  18. OtherSetting = "";
  19. SetNativeSize = "0";
  20. }
  21. public bool GetSetNativeSize()
  22. {
  23. return !string.IsNullOrEmpty(SetNativeSize) && SetNativeSize != "0";
  24. }
  25. }
  26. public class GameLanguageCfg
  27. {
  28. public const string FileName = "LanguageCfg";
  29. public string key;
  30. public string language;
  31. public GameLanguageCfg()
  32. {
  33. key = "";
  34. language = "";
  35. }
  36. }
  37. public class LocalizedTextureCfgMgr : Singleton<LocalizedTextureCfgMgr>
  38. {
  39. public static string baseName = "LocalizeTextureCfg";
  40. private static bool isOpen = false;
  41. public static bool IsOpen { get => isOpen; }
  42. private string baseKey;
  43. private string curLanguageKey;
  44. public string CurLanguageKey { get => curLanguageKey; }
  45. private string oldLanguageKey;
  46. private Dictionary<string, Dictionary<string, LocalizedSpriteCfg>> cfgs = new Dictionary<string, Dictionary<string, LocalizedSpriteCfg>>();
  47. private Dictionary<string, LocalizedSpriteCfg> curCfg;
  48. private Dictionary<string, GameLanguageCfg> gameLanguageCfgs = new Dictionary<string, GameLanguageCfg>();
  49. public override void Init()
  50. {
  51. ReadlanguageCfg();
  52. if (!isOpen)
  53. {
  54. return ;
  55. }
  56. //SetLanguageLocalSetting("_tw");
  57. string defaultlg = GetLanguageLocalSetting();
  58. string systemlg = GetSystemDefaultLanguage();
  59. string curKey = "";
  60. if (!string.IsNullOrEmpty(defaultlg))
  61. {
  62. curKey = defaultlg;
  63. }
  64. else if (!string.IsNullOrEmpty(systemlg))
  65. {
  66. curKey = systemlg;
  67. }
  68. else
  69. {
  70. curKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
  71. }
  72. ConfigMgr.CurLangKey = curKey;
  73. curLanguageKey = curKey;
  74. oldLanguageKey = "";
  75. baseKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
  76. ReadConfig();
  77. }
  78. private void ReadConfig()
  79. {
  80. string cfgname = GetLanguageCfgName(curLanguageKey);
  81. if (!cfgs.ContainsKey(curLanguageKey))
  82. {
  83. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(cfgname);
  84. if (cfgdata == null)
  85. {
  86. return;
  87. }
  88. Dictionary<string, LocalizedSpriteCfg> cfg = new Dictionary<string, LocalizedSpriteCfg>();
  89. foreach (var item in cfgdata)
  90. {
  91. LocalizedSpriteCfg localizedSpriteCfg = new LocalizedSpriteCfg();
  92. localizedSpriteCfg.key = item.Key;
  93. Dictionary<string, string> _value = item.Value;
  94. localizedSpriteCfg.Language = _value["Language"];
  95. localizedSpriteCfg.AssetPath = _value["AssetPath"];
  96. localizedSpriteCfg.OtherSetting = _value["OtherSetting"];
  97. localizedSpriteCfg.SetNativeSize = _value["SetNativeSize"];
  98. cfg.Add(item.Key, localizedSpriteCfg);
  99. }
  100. cfgs.Add(curLanguageKey, cfg);
  101. curCfg = cfg;
  102. }
  103. else
  104. {
  105. curCfg = cfgs[curLanguageKey];
  106. }
  107. }
  108. public void SetLanguage(string language)
  109. {
  110. if (!string.IsNullOrEmpty(language) && language != curLanguageKey)
  111. {
  112. oldLanguageKey = curLanguageKey;
  113. curLanguageKey = language;
  114. ReadConfig();
  115. ConfigMgr.CurLangKey = language;
  116. EventMgr.DispatchEvent(new CoreEvent<int>(ECoreEventType.EID_LANGUAGE_CHANGE, 1));
  117. }
  118. }
  119. private string GetLanguageCfgName(string lg)
  120. {
  121. return $"{baseName}{lg}";
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. /// <param name="key"></param>
  127. /// <param name="notbase">要求不是base才返回</param>
  128. /// <returns></returns>
  129. public LocalizedSpriteCfg GetLocalizedSpriteCfg(string key, bool notbase = true)
  130. {
  131. if (!isOpen)
  132. {
  133. return null;
  134. }
  135. if (notbase && (curLanguageKey == oldLanguageKey || curLanguageKey == baseKey))
  136. {
  137. return null;
  138. }
  139. if (curCfg == null)
  140. {
  141. ReadConfig();
  142. }
  143. if (curCfg == null)
  144. {
  145. return null;
  146. }
  147. if (curCfg.ContainsKey(key))
  148. {
  149. return curCfg[key];
  150. }
  151. return null;
  152. }
  153. public LocalizedSpriteCfg GetCfgByLgkey(string lgKey,string key,bool def = true)
  154. {
  155. if (!isOpen)
  156. {
  157. return null;
  158. }
  159. if (cfgs.ContainsKey(lgKey)&&cfgs[lgKey].ContainsKey(key))
  160. {
  161. return cfgs[lgKey][key];
  162. }
  163. if (def && cfgs[baseKey].ContainsKey(key))
  164. {
  165. return cfgs[baseKey][key];
  166. }
  167. return null;
  168. }
  169. public string GetLocalize(string key, bool notbase = false)
  170. {
  171. if (!isOpen)
  172. {
  173. return "";
  174. }
  175. if (notbase && curLanguageKey == oldLanguageKey)
  176. {
  177. return "";
  178. }
  179. if (curCfg == null)
  180. {
  181. ReadConfig();
  182. }
  183. if (curCfg == null)
  184. {
  185. return "";
  186. }
  187. if (curCfg.ContainsKey(key))
  188. {
  189. return curCfg[key].Language;
  190. }
  191. return "";
  192. }
  193. private void ReadlanguageCfg()
  194. {
  195. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(GameLanguageCfg.FileName);
  196. if (cfgdata == null)
  197. {
  198. return;
  199. }
  200. gameLanguageCfgs.Clear();
  201. foreach (var item in cfgdata)
  202. {
  203. GameLanguageCfg cfg = new GameLanguageCfg();
  204. cfg.key = item.Key;
  205. cfg.language = item.Value["Language"];
  206. gameLanguageCfgs.Add(cfg.key,cfg);
  207. }
  208. isOpen = FileHelper.CheckStringIsTrue(GetLanguageCfgByKey("openSetting"));
  209. }
  210. public void SetLanguageByKey(string key)
  211. {
  212. if (!isOpen)
  213. {
  214. return;
  215. }
  216. string lg = GetLanguageCfgByKey(key);
  217. if (!string.IsNullOrEmpty(lg))
  218. {
  219. SetLanguage(lg);
  220. }
  221. }
  222. public string GetLanguageCfgByKey(string key)
  223. {
  224. if (gameLanguageCfgs.ContainsKey(key))
  225. {
  226. return gameLanguageCfgs[key].language;
  227. }
  228. return "";
  229. }
  230. public string GetSystemDefaultLanguage()
  231. {
  232. string ret = LanguageFileEx.Unknown;
  233. switch (UnityEngine.Application.systemLanguage)
  234. {
  235. case UnityEngine.SystemLanguage.Vietnamese: //越南语
  236. {
  237. ret = LanguageFileEx.Vietnamese;
  238. }
  239. break;
  240. case UnityEngine.SystemLanguage.Thai: //泰语
  241. {
  242. ret = LanguageFileEx.Thai;
  243. }
  244. break;
  245. case UnityEngine.SystemLanguage.English:
  246. {
  247. ret = LanguageFileEx.English;
  248. }
  249. break;
  250. case UnityEngine.SystemLanguage.Chinese:
  251. case UnityEngine.SystemLanguage.ChineseSimplified:
  252. case UnityEngine.SystemLanguage.ChineseTraditional:
  253. {
  254. ret = LanguageFileEx.Chinese;
  255. }
  256. break;
  257. case UnityEngine.SystemLanguage.Unknown:
  258. {
  259. ret = LanguageFileEx.Unknown;
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. return GetLanguageCfgByKey(ret);
  266. }
  267. public string GetCurLanguageKey()
  268. {
  269. foreach (var item in gameLanguageCfgs)
  270. {
  271. if (item.Value.language == curLanguageKey && item.Key != LanguageFileEx.Unknown)
  272. {
  273. return item.Key;
  274. }
  275. }
  276. return "";
  277. }
  278. public static string GetLanguageLocalSetting()
  279. {
  280. return UnityEngine.PlayerPrefs.GetString("LanguageLocalSetting",string.Empty);
  281. }
  282. public static void SetLanguageLocalSetting(string key)
  283. {
  284. UnityEngine.PlayerPrefs.SetString("LanguageLocalSetting", key);
  285. }
  286. }