LocalizedTextureCfgMgr.cs 7.9 KB

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