LocalizedTextureCfgMgr.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. curCfg = AddCfgByCfgName(curLanguageKey);
  81. }
  82. private Dictionary<string, LocalizedSpriteCfg> AddCfgByCfgName(string name)
  83. {
  84. string cfgname = GetLanguageCfgName(name);
  85. if (!cfgs.ContainsKey(name))
  86. {
  87. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(cfgname);
  88. if (cfgdata == null)
  89. {
  90. return null;
  91. }
  92. Dictionary<string, LocalizedSpriteCfg> cfg = new Dictionary<string, LocalizedSpriteCfg>();
  93. foreach (var item in cfgdata)
  94. {
  95. LocalizedSpriteCfg localizedSpriteCfg = new LocalizedSpriteCfg();
  96. localizedSpriteCfg.key = item.Key;
  97. Dictionary<string, string> _value = item.Value;
  98. localizedSpriteCfg.Language = _value["Language"];
  99. localizedSpriteCfg.AssetPath = _value["AssetPath"];
  100. localizedSpriteCfg.OtherSetting = _value["OtherSetting"];
  101. localizedSpriteCfg.SetNativeSize = _value["SetNativeSize"];
  102. cfg.Add(item.Key, localizedSpriteCfg);
  103. }
  104. cfgs.Add(name, cfg);
  105. return cfg;
  106. }
  107. return cfgs[name];
  108. }
  109. public void SetLanguage(string language)
  110. {
  111. if (!string.IsNullOrEmpty(language) && language != curLanguageKey)
  112. {
  113. oldLanguageKey = curLanguageKey;
  114. curLanguageKey = language;
  115. ReadConfig();
  116. ConfigMgr.CurLangKey = language;
  117. EventMgr.DispatchEvent(new CoreEvent<int>(ECoreEventType.EID_LANGUAGE_CHANGE, 1));
  118. }
  119. }
  120. private string GetLanguageCfgName(string lg)
  121. {
  122. return $"{baseName}{lg}";
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <param name="key"></param>
  128. /// <param name="notbase">要求不是base才返回</param>
  129. /// <returns></returns>
  130. public LocalizedSpriteCfg GetLocalizedSpriteCfg(string key, bool notbase = true)
  131. {
  132. if (!isOpen ||
  133. (notbase && curLanguageKey == oldLanguageKey))
  134. {
  135. return null;
  136. }
  137. if (curCfg == null)
  138. {
  139. ReadConfig();
  140. }
  141. if (curCfg == null)
  142. {
  143. return null;
  144. }
  145. if (curCfg.ContainsKey(key))
  146. {
  147. return curCfg[key];
  148. }
  149. return null;
  150. }
  151. public LocalizedSpriteCfg GetCfgByLgkey(string lgKey, string key, bool def = true)
  152. {
  153. if (!isOpen)
  154. {
  155. return null;
  156. }
  157. Dictionary<string, LocalizedSpriteCfg> tcfg = AddCfgByCfgName(lgKey);
  158. if (tcfg != null && tcfg.ContainsKey(key))
  159. {
  160. return tcfg[key];
  161. }
  162. if (def)
  163. {
  164. tcfg = AddCfgByCfgName(baseKey);
  165. if (tcfg != null && tcfg.ContainsKey(key))
  166. {
  167. return tcfg[key];
  168. }
  169. }
  170. return null;
  171. }
  172. public string GetLocalize(string key, bool notbase = false)
  173. {
  174. if (!isOpen)
  175. {
  176. return "";
  177. }
  178. if (notbase && curLanguageKey == oldLanguageKey)
  179. {
  180. return "";
  181. }
  182. if (curCfg == null)
  183. {
  184. ReadConfig();
  185. }
  186. if (curCfg == null)
  187. {
  188. return "";
  189. }
  190. if (curCfg.ContainsKey(key))
  191. {
  192. return curCfg[key].Language;
  193. }
  194. return "";
  195. }
  196. private void ReadlanguageCfg()
  197. {
  198. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(GameLanguageCfg.FileName);
  199. if (cfgdata == null)
  200. {
  201. return;
  202. }
  203. gameLanguageCfgs.Clear();
  204. foreach (var item in cfgdata)
  205. {
  206. GameLanguageCfg cfg = new GameLanguageCfg();
  207. cfg.key = item.Key;
  208. cfg.language = item.Value["Language"];
  209. gameLanguageCfgs.Add(cfg.key, cfg);
  210. }
  211. isOpen = FileHelper.CheckStringIsTrue(GetLanguageCfgByKey("openSetting"));
  212. }
  213. public void SetLanguageByKey(string key)
  214. {
  215. if (!isOpen)
  216. {
  217. return;
  218. }
  219. string lg = GetLanguageCfgByKey(key);
  220. if (!string.IsNullOrEmpty(lg))
  221. {
  222. SetLanguage(lg);
  223. }
  224. }
  225. public string GetLanguageCfgByKey(string key)
  226. {
  227. if (gameLanguageCfgs.ContainsKey(key))
  228. {
  229. return gameLanguageCfgs[key].language;
  230. }
  231. return "";
  232. }
  233. public string GetSystemDefaultLanguage()
  234. {
  235. string ret = LanguageFileEx.Unknown;
  236. switch (UnityEngine.Application.systemLanguage)
  237. {
  238. case UnityEngine.SystemLanguage.Vietnamese: //越南语
  239. {
  240. ret = LanguageFileEx.Vietnamese;
  241. }
  242. break;
  243. case UnityEngine.SystemLanguage.Thai: //泰语
  244. {
  245. ret = LanguageFileEx.Thai;
  246. }
  247. break;
  248. case UnityEngine.SystemLanguage.English:
  249. {
  250. ret = LanguageFileEx.English;
  251. }
  252. break;
  253. case UnityEngine.SystemLanguage.Chinese:
  254. case UnityEngine.SystemLanguage.ChineseSimplified:
  255. case UnityEngine.SystemLanguage.ChineseTraditional:
  256. {
  257. ret = LanguageFileEx.Chinese;
  258. }
  259. break;
  260. case UnityEngine.SystemLanguage.Unknown:
  261. {
  262. ret = LanguageFileEx.Unknown;
  263. }
  264. break;
  265. default:
  266. break;
  267. }
  268. UnityEngine.Debug.Log(UnityEngine.Application.systemLanguage.ToString() + " 系统语言:" +ret);
  269. return GetLanguageCfgByKey(ret);
  270. }
  271. public string GetCurLanguageKey()
  272. {
  273. foreach (var item in gameLanguageCfgs)
  274. {
  275. if (item.Value.language == curLanguageKey && item.Key != LanguageFileEx.Unknown)
  276. {
  277. return item.Key;
  278. }
  279. }
  280. return "";
  281. }
  282. public static string GetLanguageLocalSetting()
  283. {
  284. return UnityEngine.PlayerPrefs.GetString("LanguageLocalSetting", string.Empty);
  285. }
  286. public static void SetLanguageLocalSetting(string key)
  287. {
  288. UnityEngine.PlayerPrefs.SetString("LanguageLocalSetting", key);
  289. }
  290. }