LanguageManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System;
  6. public class LanguageManager
  7. {
  8. public const string c_configFileName = "LanguageConfig";
  9. public const string c_defaultModuleKey = "default";
  10. public const string c_DataFilePrefix = "LangData_";
  11. public const string c_mainKey = "key";
  12. public const string c_valueKey = "value";
  13. static public SystemLanguage s_currentLanguage = SystemLanguage.ChineseSimplified; //当前语言
  14. static public Dictionary<string,Dictionary<string,string>> s_languageDataDict = new Dictionary<string, Dictionary<string,string>>();//所有语言数据
  15. private static LanguageSettingConfig config;
  16. static private bool isInit = false;
  17. public static bool IsInit
  18. {
  19. get { return LanguageManager.isInit; }
  20. set { isInit = value; }
  21. }
  22. public static void Init()
  23. {
  24. //Debug.Log("1 当前语言: " + Application.systemLanguage + " isInit " + isInit);
  25. if (!isInit)
  26. {
  27. isInit = true;
  28. if (config == null)
  29. {
  30. config = LanguageDataUtils.LoadEditorConfig();
  31. }
  32. SetLanguage(ApplicationManager.Langguage);
  33. }
  34. }
  35. public static void SetLanguage(SystemLanguage lang)
  36. {
  37. SystemLanguage oldLan = s_currentLanguage;
  38. if (config == null)
  39. return;
  40. if (lang == SystemLanguage.Chinese)
  41. lang = SystemLanguage.ChineseSimplified;
  42. if (config.gameExistLanguages.Contains(lang))
  43. {
  44. s_currentLanguage = lang;
  45. }
  46. else
  47. {
  48. //Debug.Log("当前语言不存在 " + lang);
  49. s_currentLanguage = config.defaultLanguage;
  50. }
  51. if (oldLan != s_currentLanguage)
  52. {
  53. s_languageDataDict.Clear();
  54. }
  55. GlobalEvent.DispatchEvent(LanguageEventEnum.LanguageChange, lang);
  56. }
  57. /// <summary>
  58. /// 兼容旧版本代码,不再建议使用
  59. /// </summary>
  60. [Obsolete]
  61. public static string GetContent(string contentID, List<object> contentParams)
  62. {
  63. return GetContent(c_defaultModuleKey, contentID, contentParams.ToArray());
  64. }
  65. /// <summary>
  66. /// 兼容旧版本代码,不再建议使用
  67. /// </summary>
  68. [Obsolete]
  69. public static string GetContent(string contentID, params object[] contentParams)
  70. {
  71. return GetContent(c_defaultModuleKey, contentID, contentParams);
  72. }
  73. public static string GetContent(string moduleName,string contentID, List<object> contentParams)
  74. {
  75. return GetContent(moduleName, contentID, contentParams.ToArray());
  76. }
  77. /// <summary>
  78. /// moduleName_key : MiniGame/title_0
  79. /// </summary>
  80. /// <param name="fullKeyName"></param>
  81. /// <param name="contentParams"></param>
  82. /// <returns></returns>
  83. public static string GetContentByKey(string fullKeyName, params object[] contentParams)
  84. {
  85. Init();
  86. if (string.IsNullOrEmpty(fullKeyName))
  87. return "Error : key is null";
  88. int indexEnd = fullKeyName.LastIndexOf("/");
  89. if (indexEnd < 0)
  90. return "Error : Format is error";
  91. string key = fullKeyName.Substring(indexEnd + 1);
  92. string fullFileName = fullKeyName.Remove(indexEnd);
  93. if (string.IsNullOrEmpty(fullFileName) || string.IsNullOrEmpty(key))
  94. return "Error : key is null";
  95. Dictionary<string, string> contentDic = null;
  96. if (s_languageDataDict.ContainsKey(fullFileName))
  97. {
  98. contentDic = s_languageDataDict[fullFileName];
  99. }
  100. else
  101. {
  102. DataTable data = LoadDataTable(s_currentLanguage, fullFileName);
  103. contentDic = new Dictionary<string, string>();
  104. foreach (var item in data.TableIDs)
  105. {
  106. contentDic.Add(item, data[item].GetString(c_valueKey));
  107. }
  108. s_languageDataDict.Add(fullFileName, contentDic);
  109. }
  110. if (!contentDic.ContainsKey(key))
  111. return "Error:no key in File:" + fullFileName + " key:" + key;
  112. string content = contentDic[key];
  113. if (contentParams != null && contentParams.Length > 0)
  114. {
  115. for (int i = 0; i < contentParams.Length; i++)
  116. {
  117. string replaceTmp = "{" + i + "}";
  118. if (contentParams[i] == null)
  119. continue;
  120. content = content.Replace(replaceTmp, contentParams[i].ToString());
  121. }
  122. }
  123. if (ApplicationManager.Instance != null && ApplicationManager.Instance.showLanguageValue && ApplicationManager.Instance.m_AppMode == AppMode.Developing)
  124. content = "[" + content + "]";
  125. return content;
  126. }
  127. private static Dictionary<string, int> loadTextFileTimesDic = new Dictionary<string, int>();
  128. private static DataTable LoadDataTable(SystemLanguage language, string fullFileName)
  129. {
  130. if (Application.isPlaying)
  131. {
  132. string name = GetLanguageDataName(language, fullFileName);
  133. TextAsset text = AssetsPoolManager.Load<TextAsset>(name);
  134. if (text == null)
  135. {
  136. Debug.LogError("Error: no Language file :" + name);
  137. return null;
  138. }
  139. if (loadTextFileTimesDic.ContainsKey(name))
  140. loadTextFileTimesDic[name]++;
  141. else
  142. {
  143. loadTextFileTimesDic.Add(name, 1);
  144. }
  145. DataTable data = DataTable.Analysis(text.text);
  146. return data;
  147. }
  148. else
  149. {
  150. return LanguageDataUtils.LoadFileData(language, fullFileName);
  151. }
  152. }
  153. public static string GetContent(string moduleName, string contentID, params object[] contentParams)
  154. {
  155. string fullkey = moduleName.Replace('_', '/') + "/" + contentID;
  156. return GetContentByKey(fullkey,contentParams);
  157. }
  158. public static string GetLanguageDataName(SystemLanguage langeuageName, string fullkeyFileName)
  159. {
  160. string modelName = fullkeyFileName.Replace('/', '_');
  161. return c_DataFilePrefix + langeuageName + "_" + modelName;
  162. }
  163. public static void Release()
  164. {
  165. s_languageDataDict.Clear();
  166. isInit = false;
  167. foreach (var item in loadTextFileTimesDic)
  168. {
  169. AssetsPoolManager.DestroyByPool(item.Key, item.Value);
  170. }
  171. loadTextFileTimesDic.Clear();
  172. }
  173. }
  174. public enum LanguageEventEnum
  175. {
  176. LanguageChange,
  177. }