LocalizedTextureCfgMgr.cs 7.2 KB

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