LocalizedTextureCfgMgr.cs 8.3 KB

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