LanguageMgr.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using BeinLab.Util;
  2. using PublicTools.XMLDataBase;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace XRTool.Util
  7. {
  8. /// <summary>
  9. /// 语言控制器
  10. /// 提供语言切换时事件
  11. /// 提供语言包读取,解析
  12. /// 语言包放到stream包中
  13. /// </summary>
  14. public class LanguageMgr : Singleton<LanguageMgr>
  15. {
  16. /// <summary>
  17. /// 改变语言的事件
  18. /// </summary>
  19. public event Action<LanguagePackConf> changeLanuage;
  20. /// <summary>
  21. /// 当前的语言
  22. /// </summary>
  23. private LanguagePackConf curLanguage;
  24. /// <summary>
  25. /// 语言包列表
  26. /// </summary>
  27. private List<LanguagePackConf> languagePackList;
  28. private string tableName;
  29. private TableInterface table;
  30. public int LanguageCount
  31. {
  32. get
  33. {
  34. if (languagePackList != null)
  35. {
  36. return languagePackList.Count;
  37. }
  38. return 0;
  39. }
  40. }
  41. //private int languageIndex = 0;
  42. /// <summary>
  43. /// 当开始引用的时候创建实例
  44. /// 读取配置表
  45. /// </summary>
  46. public LanguageMgr()
  47. {
  48. GameSession.OnFocusGame += OnFocusGame;
  49. InitLanguage();
  50. }
  51. private void InitLanguage()
  52. {
  53. if (BuildConfigMgr.Instance.IsInit)
  54. {
  55. if (TimerMgr.Instance)
  56. {
  57. TimerMgr.Instance.CreateTimer(() =>
  58. {
  59. ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
  60. }, 0.5f);
  61. }
  62. else
  63. {
  64. TimerMgr.InitComplte += () =>
  65. {
  66. TimerMgr.Instance.CreateTimer(() =>
  67. {
  68. ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
  69. }, 0.5f);
  70. };
  71. }
  72. }
  73. else
  74. {
  75. UnityLog.Instance.LogError("配置文件丢失!");
  76. }
  77. }
  78. private void OnFocusGame()
  79. {
  80. if (!IsInit)
  81. {
  82. if (TimerMgr.Instance)
  83. {
  84. TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
  85. }
  86. else
  87. {
  88. TimerMgr.InitComplte += () =>
  89. {
  90. TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
  91. };
  92. }
  93. return;
  94. }
  95. if (CurLanguage == null)
  96. {
  97. if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
  98. {
  99. UnityLog.Instance.LogError("没有找到语言包");
  100. InitLanguage();
  101. return;
  102. }
  103. else
  104. {
  105. CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
  106. }
  107. }
  108. if (CurLanguage != null)
  109. {
  110. if (CurLanguage.LanguageMap == null)
  111. {
  112. CurLanguage.ReadLanguageMap();
  113. }
  114. changeLanuage?.Invoke(CurLanguage);
  115. IsInit = true;
  116. }
  117. }
  118. private bool isInit = false;
  119. public LanguagePackConf CurLanguage { get => curLanguage; set => curLanguage = value; }
  120. public bool IsInit { get => isInit; set => isInit = value; }
  121. /// <summary>
  122. /// 读取语言包
  123. /// </summary>
  124. public void ReadLanguage(string path)
  125. {
  126. //languageIndex = SystemSettingMgr.Instance.settings.Language;
  127. tableName = typeof(LanguagePackConf).Name;
  128. #if UNITY_EDITOR || !UNITY_ANDROID
  129. table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
  130. OpenTable();
  131. #else
  132. string fullPath = Path.Combine(path, tableName + ".xml");
  133. if (GameSession.Instance)
  134. {
  135. GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<LanguagePackConf> packList) =>
  136. {
  137. InitPackage(packList);
  138. }));
  139. }
  140. else
  141. {
  142. GameSession.InitComplte += () =>
  143. {
  144. GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<LanguagePackConf> packList) =>
  145. {
  146. InitPackage(packList);
  147. }));
  148. };
  149. }
  150. #endif
  151. }
  152. /// <summary>
  153. /// 打开语言包配置表
  154. /// </summary>
  155. public void OpenTable()
  156. {
  157. if (!table.Open())
  158. {
  159. table.Create(tableName);
  160. UnityLog.Instance.Log("create table" + tableName);
  161. }
  162. //languagePackList = table.FindAllData<LanguagePackConf>();
  163. InitPackage(table.FindAllData<LanguagePackConf>());
  164. }
  165. public void InitPackage(List<LanguagePackConf> packList)
  166. {
  167. languagePackList = packList;
  168. if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
  169. {
  170. UnityLog.Instance.LogError("没有找到语言包");
  171. }
  172. else
  173. {
  174. CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
  175. if (CurLanguage.LanguageMap == null)
  176. {
  177. CurLanguage.ReadLanguageMap();
  178. }
  179. changeLanuage?.Invoke(CurLanguage);
  180. IsInit = true;
  181. }
  182. }
  183. /// <summary>
  184. /// 选中某一个语言时,默认是中文
  185. /// </summary>
  186. /// <param name="index"></param>
  187. public void SelectLanguage(int index)
  188. {
  189. if (index >= 0 && index != SystemSettingMgr.Instance.settings.Language && index < languagePackList.Count)
  190. {
  191. SystemSettingMgr.Instance.settings.Language = index;
  192. CurLanguage = languagePackList[index];
  193. if (CurLanguage.LanguageMap == null)
  194. {
  195. CurLanguage.ReadLanguageMap();
  196. }
  197. changeLanuage?.Invoke(CurLanguage);
  198. SystemSettingMgr.Instance.SaveSettings();
  199. }
  200. else
  201. {
  202. UnityLog.Instance.Log("未切换语言" + index);
  203. }
  204. }
  205. /// <summary>
  206. /// 通过Key值获取对应的语言
  207. /// </summary>
  208. /// <param name="prikey"></param>
  209. /// <returns></returns>
  210. public LanguageConf GetMessage(string prikey)
  211. {
  212. if (!string.IsNullOrEmpty(prikey))
  213. {
  214. prikey = prikey.Trim();
  215. if (!prikey.StartsWith(BuildConfig.Instance.prefix))
  216. {
  217. prikey = BuildConfig.Instance.prefix + prikey;
  218. }
  219. if (CurLanguage != null)
  220. {
  221. return CurLanguage.GetLanguage(prikey);
  222. }
  223. }
  224. return null;
  225. }
  226. }
  227. }