LocalizedTextureCfgMgr.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. private string oldLanguageKey;
  45. private Dictionary<string, Dictionary<string, LocalizedSpriteCfg>> cfgs = new Dictionary<string, Dictionary<string, LocalizedSpriteCfg>>();
  46. private Dictionary<string, LocalizedSpriteCfg> curCfg;
  47. private Dictionary<string, GameLanguageCfg> gameLanguageCfgs = new Dictionary<string, GameLanguageCfg>();
  48. public override void Init()
  49. {
  50. ReadlanguageCfg();
  51. if (!isOpen)
  52. {
  53. return ;
  54. }
  55. //SetLanguageLocalSetting("_tw");
  56. string defaultlg = GetLanguageLocalSetting();
  57. string systemlg = GetSystemDefaultLanguage();
  58. string curKey = "";
  59. if (!string.IsNullOrEmpty(defaultlg))
  60. {
  61. curKey = defaultlg;
  62. }
  63. else if (!string.IsNullOrEmpty(systemlg))
  64. {
  65. curKey = systemlg;
  66. }
  67. else
  68. {
  69. curKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
  70. }
  71. ConfigMgr.CurLangKey = curKey;
  72. curLanguageKey = curKey;
  73. oldLanguageKey = curKey;
  74. baseKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
  75. ReadConfig();
  76. }
  77. private void ReadConfig()
  78. {
  79. string cfgname = GetLanguageCfgName(curLanguageKey);
  80. if (!cfgs.ContainsKey(curLanguageKey))
  81. {
  82. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(cfgname);
  83. if (cfgdata == null)
  84. {
  85. return;
  86. }
  87. Dictionary<string, LocalizedSpriteCfg> cfg = new Dictionary<string, LocalizedSpriteCfg>();
  88. foreach (var item in cfgdata)
  89. {
  90. LocalizedSpriteCfg localizedSpriteCfg = new LocalizedSpriteCfg();
  91. localizedSpriteCfg.key = item.Key;
  92. Dictionary<string, string> _value = item.Value;
  93. localizedSpriteCfg.Language = _value["Language"];
  94. localizedSpriteCfg.AssetPath = _value["AssetPath"];
  95. localizedSpriteCfg.OtherSetting = _value["OtherSetting"];
  96. localizedSpriteCfg.SetNativeSize = _value["SetNativeSize"];
  97. cfg.Add(item.Key, localizedSpriteCfg);
  98. }
  99. cfgs.Add(curLanguageKey, cfg);
  100. curCfg = cfg;
  101. }
  102. else
  103. {
  104. curCfg = cfgs[curLanguageKey];
  105. }
  106. }
  107. public void SetLanguage(string language)
  108. {
  109. if (!string.IsNullOrEmpty(language) && language != curLanguageKey)
  110. {
  111. curLanguageKey = language;
  112. ReadConfig();
  113. ConfigMgr.CurLangKey = language;
  114. }
  115. }
  116. private string GetLanguageCfgName(string lg)
  117. {
  118. return $"{baseName}{lg}";
  119. }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="key"></param>
  124. /// <param name="notbase">要求不是base才返回</param>
  125. /// <returns></returns>
  126. public LocalizedSpriteCfg GetLocalizedSpriteCfg(string key, bool notbase = true)
  127. {
  128. if (!isOpen)
  129. {
  130. return null;
  131. }
  132. if (notbase && (curLanguageKey == oldLanguageKey || curLanguageKey == baseKey))
  133. {
  134. return null;
  135. }
  136. if (curCfg == null)
  137. {
  138. ReadConfig();
  139. }
  140. if (curCfg == null)
  141. {
  142. return null;
  143. }
  144. if (curCfg.ContainsKey(key))
  145. {
  146. return curCfg[key];
  147. }
  148. return null;
  149. }
  150. public string GetLocalize(string key, bool notbase = false)
  151. {
  152. if (!isOpen)
  153. {
  154. return "";
  155. }
  156. if (notbase && curLanguageKey == oldLanguageKey)
  157. {
  158. return "";
  159. }
  160. if (curCfg == null)
  161. {
  162. ReadConfig();
  163. }
  164. if (curCfg == null)
  165. {
  166. return "";
  167. }
  168. if (curCfg.ContainsKey(key))
  169. {
  170. return curCfg[key].Language;
  171. }
  172. return "";
  173. }
  174. private void ReadlanguageCfg()
  175. {
  176. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(GameLanguageCfg.FileName);
  177. if (cfgdata == null)
  178. {
  179. return;
  180. }
  181. gameLanguageCfgs.Clear();
  182. foreach (var item in cfgdata)
  183. {
  184. GameLanguageCfg cfg = new GameLanguageCfg();
  185. cfg.key = item.Key;
  186. cfg.language = item.Value["Language"];
  187. gameLanguageCfgs.Add(cfg.key,cfg);
  188. }
  189. isOpen = FileHelper.CheckStringIsTrue(GetLanguageCfgByKey("openSetting"));
  190. }
  191. public void SetLanguageByKey(string key)
  192. {
  193. if (!isOpen)
  194. {
  195. return;
  196. }
  197. string lg = GetLanguageCfgByKey(key);
  198. if (!string.IsNullOrEmpty(lg))
  199. {
  200. SetLanguage(lg);
  201. }
  202. }
  203. public string GetLanguageCfgByKey(string key)
  204. {
  205. if (gameLanguageCfgs.ContainsKey(key))
  206. {
  207. return gameLanguageCfgs[key].language;
  208. }
  209. return "";
  210. }
  211. public string GetSystemDefaultLanguage()
  212. {
  213. string ret = LanguageFileEx.Unknown;
  214. switch (UnityEngine.Application.systemLanguage)
  215. {
  216. case UnityEngine.SystemLanguage.Vietnamese: //越南语
  217. {
  218. ret = LanguageFileEx.Vietnamese;
  219. }
  220. break;
  221. case UnityEngine.SystemLanguage.Thai: //泰语
  222. {
  223. ret = LanguageFileEx.Thai;
  224. }
  225. break;
  226. case UnityEngine.SystemLanguage.English:
  227. {
  228. ret = LanguageFileEx.English;
  229. }
  230. break;
  231. case UnityEngine.SystemLanguage.Chinese:
  232. case UnityEngine.SystemLanguage.ChineseSimplified:
  233. case UnityEngine.SystemLanguage.ChineseTraditional:
  234. {
  235. ret = LanguageFileEx.Chinese;
  236. }
  237. break;
  238. case UnityEngine.SystemLanguage.Unknown:
  239. {
  240. ret = LanguageFileEx.Unknown;
  241. }
  242. break;
  243. default:
  244. break;
  245. }
  246. UnityEngine.Debug.Log(UnityEngine.Application.systemLanguage.ToString() + " 系统语言:" +ret);
  247. return GetLanguageCfgByKey(ret);
  248. }
  249. public static string GetLanguageLocalSetting()
  250. {
  251. return UnityEngine.PlayerPrefs.GetString("LanguageLocalSetting",string.Empty);
  252. }
  253. public static void SetLanguageLocalSetting(string key)
  254. {
  255. UnityEngine.PlayerPrefs.SetString("LanguageLocalSetting", key);
  256. }
  257. }