LocalizedTextureCfgMgr.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 string GetLocalize(string key, bool notbase = false)
  153. {
  154. if (!isOpen)
  155. {
  156. return "";
  157. }
  158. if (notbase && curLanguageKey == oldLanguageKey)
  159. {
  160. return "";
  161. }
  162. if (curCfg == null)
  163. {
  164. ReadConfig();
  165. }
  166. if (curCfg == null)
  167. {
  168. return "";
  169. }
  170. if (curCfg.ContainsKey(key))
  171. {
  172. return curCfg[key].Language;
  173. }
  174. return "";
  175. }
  176. private void ReadlanguageCfg()
  177. {
  178. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(GameLanguageCfg.FileName);
  179. if (cfgdata == null)
  180. {
  181. return;
  182. }
  183. gameLanguageCfgs.Clear();
  184. foreach (var item in cfgdata)
  185. {
  186. GameLanguageCfg cfg = new GameLanguageCfg();
  187. cfg.key = item.Key;
  188. cfg.language = item.Value["Language"];
  189. gameLanguageCfgs.Add(cfg.key,cfg);
  190. }
  191. isOpen = FileHelper.CheckStringIsTrue(GetLanguageCfgByKey("openSetting"));
  192. }
  193. public void SetLanguageByKey(string key)
  194. {
  195. if (!isOpen)
  196. {
  197. return;
  198. }
  199. string lg = GetLanguageCfgByKey(key);
  200. if (!string.IsNullOrEmpty(lg))
  201. {
  202. SetLanguage(lg);
  203. }
  204. }
  205. public string GetLanguageCfgByKey(string key)
  206. {
  207. if (gameLanguageCfgs.ContainsKey(key))
  208. {
  209. return gameLanguageCfgs[key].language;
  210. }
  211. return "";
  212. }
  213. public string GetSystemDefaultLanguage()
  214. {
  215. string ret = LanguageFileEx.Unknown;
  216. switch (UnityEngine.Application.systemLanguage)
  217. {
  218. case UnityEngine.SystemLanguage.Vietnamese: //越南语
  219. {
  220. ret = LanguageFileEx.Vietnamese;
  221. }
  222. break;
  223. case UnityEngine.SystemLanguage.Thai: //泰语
  224. {
  225. ret = LanguageFileEx.Thai;
  226. }
  227. break;
  228. case UnityEngine.SystemLanguage.English:
  229. {
  230. ret = LanguageFileEx.English;
  231. }
  232. break;
  233. case UnityEngine.SystemLanguage.Chinese:
  234. case UnityEngine.SystemLanguage.ChineseSimplified:
  235. case UnityEngine.SystemLanguage.ChineseTraditional:
  236. {
  237. ret = LanguageFileEx.Chinese;
  238. }
  239. break;
  240. case UnityEngine.SystemLanguage.Unknown:
  241. {
  242. ret = LanguageFileEx.Unknown;
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. UnityEngine.Debug.Log(UnityEngine.Application.systemLanguage.ToString() + " 系统语言:" +ret);
  249. return GetLanguageCfgByKey(ret);
  250. }
  251. public string GetCurLanguageKey()
  252. {
  253. foreach (var item in gameLanguageCfgs)
  254. {
  255. if (item.Value.language == curLanguageKey && item.Key != LanguageFileEx.Unknown)
  256. {
  257. return item.Key;
  258. }
  259. }
  260. return "";
  261. }
  262. public static string GetLanguageLocalSetting()
  263. {
  264. return UnityEngine.PlayerPrefs.GetString("LanguageLocalSetting",string.Empty);
  265. }
  266. public static void SetLanguageLocalSetting(string key)
  267. {
  268. UnityEngine.PlayerPrefs.SetString("LanguageLocalSetting", key);
  269. }
  270. }