LanguageManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using System;
  2. using System.Collections.Generic;
  3. using Excel2Json;
  4. using Fort23.Core;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using Utility;
  9. namespace Core.Language
  10. {
  11. public class LanguageManager : Singleton<LanguageManager>, ITextTool
  12. {
  13. public enum LanguageType
  14. {
  15. LanguageChineseConfig = 0,
  16. LanguageEnglishConfig,
  17. LanguageDEConfig,
  18. LanguageFRConfig,
  19. LanguageRUConfig,
  20. LanguageTHConfig,
  21. /// <summary>
  22. /// 越南語
  23. /// </summary>
  24. LanguageVNConfig,
  25. }
  26. public TextAsset blockWord;
  27. private FontSettingConfig fontSettingConfig;
  28. //private LanguageChineseConfig textConfig;
  29. private Map<int, LanguageChineseConfig> allLangugeInfo = new Map<int, LanguageChineseConfig>();
  30. // public Map<int, StartGameLanguageChineseConfig> allLoginLangugeInfo = new Map<int, StartGameLanguageChineseConfig>();
  31. public Map<string, Font> fonts = new Map<string, Font>();
  32. public LanguageType currLanguageType;
  33. private FontSettingConfig.LanguageConfig currConfig;
  34. private bool isLaod;
  35. public async CTask InitManager()
  36. {
  37. Debug.Log("开始FontSettingConfig");
  38. AssetHandle assetBundle =
  39. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<FontSettingConfig>("FontSettingConfig.asset");
  40. fontSettingConfig = assetBundle.AssetObject<FontSettingConfig>();
  41. if (fontSettingConfig != null)
  42. {
  43. fontSettingConfig.Init();
  44. }
  45. else
  46. {
  47. Debug.Log("加载失败FontSettingConfig");
  48. }
  49. UGUITextTool.TextTool = this;
  50. await SwitchTextConfig(LanguageType.LanguageChineseConfig, null);
  51. }
  52. public async CTask SwitchTextConfig(LanguageType languageType, System.Action callBack)
  53. {
  54. if (currLanguageType == languageType && isLaod)
  55. {
  56. callBack?.Invoke();
  57. return;
  58. }
  59. isLaod = true;
  60. this.currLanguageType = languageType;
  61. Debug.Log("SwitchTextConfig");
  62. currConfig = fontSettingConfig.GetLanguageConfig(languageType);
  63. Debug.Log("SwitchTextConfig" + currConfig.fontConfig.Count);
  64. string configJson = languageType.ToString() + ".json";
  65. AssetHandle assetBundle =
  66. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(configJson);
  67. TextAsset ta = assetBundle.AssetObject<TextAsset>();
  68. LanguageChineseConfigHolder configDataHolder = JsonManager.FromJson<LanguageChineseConfigHolder>(ta.text);
  69. assetBundle.Release();
  70. configDataHolder.Init();
  71. allLangugeInfo.Clear();
  72. List<LanguageChineseConfig> configList = configDataHolder.configList;
  73. for (int i = 0; i < configList.Count; i++)
  74. {
  75. allLangugeInfo.Add(configList[i].ID, configList[i]);
  76. }
  77. // configJson = "StartGame" + languageType + ".json";
  78. // assetBundle =
  79. // await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(configJson);
  80. // ta = assetBundle.AssetObject<TextAsset>();
  81. // StartGameLanguageChineseConfigHolder configDataHolder2 = JsonManager.FromJson<StartGameLanguageChineseConfigHolder>(ta.text);
  82. // assetBundle.Release();
  83. // configDataHolder2.Init();
  84. // allLoginLangugeInfo.Clear();
  85. // List<StartGameLanguageChineseConfig> configList2 = configDataHolder2.configList;
  86. // for (int i = 0; i < configList2.Count; i++)
  87. // {
  88. // allLoginLangugeInfo.Add(configList2[i].ID, configList2[i]);
  89. // }
  90. callBack?.Invoke();
  91. // AIhelpService.Instance.ChangeLanguage();
  92. }
  93. public int GetFontIndix(string fontName)
  94. {
  95. if (fontSettingConfig == null)
  96. {
  97. return 0;
  98. }
  99. for (int i = 0; i < fontSettingConfig.languageConfig.Count; i++)
  100. {
  101. List<FontSettingConfig.FontConfig> fontConfig = fontSettingConfig.languageConfig[i].fontConfig;
  102. for (int j = 0; j < fontConfig.Count; j++)
  103. {
  104. if (fontConfig[j].fontName.Equals(fontName))
  105. {
  106. return fontConfig[j].index;
  107. }
  108. }
  109. }
  110. return 0;
  111. }
  112. // public void SetFont(UILabel label)
  113. // {
  114. // if (label.trueTypeFont == null)
  115. // {
  116. // return;
  117. // }
  118. //
  119. // string fontName = label.trueTypeFont.name;
  120. // int index = GetFontIndix(fontName);
  121. // if (currConfig == null)
  122. // {
  123. // return;
  124. // }
  125. //
  126. // for (int i = 0; i < currConfig.fontConfig.Count; i++)
  127. // {
  128. // if (currConfig.fontConfig[i].index == index)
  129. // {
  130. // fontName = currConfig.fontConfig[i].fontName;
  131. // break;
  132. // }
  133. // }
  134. //
  135. // if (fonts.ContainsKey(fontName))
  136. // {
  137. // label.trueTypeFont = fonts[fontName];
  138. // if (label.defaultConfig != null)
  139. // {
  140. // label.defaultConfig.defaultFont = label.trueTypeFont;
  141. // }
  142. // }
  143. // // LogTool.LogError("没有字体"+ currConfig.languageType.ToString()+" "+ index);
  144. // }
  145. // public void SetFont(UILabel uilabel, string fontName)
  146. // {
  147. // if (fonts.ContainsKey(fontName))
  148. // {
  149. // uilabel.trueTypeFont = fonts[fontName];
  150. // }
  151. // else
  152. // {
  153. // LoadResourceTask task = AssetBundleLoadManager.Instance.AddAssetTask(fontName, delegate(Object obj, object data)
  154. // {
  155. // if (!fonts.ContainsKey(fontName))
  156. // {
  157. // fonts.Add(fontName, (Font) obj);
  158. // }
  159. //
  160. // uilabel.trueTypeFont = (Font) obj;
  161. // }, erroCallBack: delegate() { LogTool.Log("没找到这个字体"); });
  162. // }
  163. // }
  164. public string Text(int id)
  165. {
  166. //return "-ok";
  167. LanguageChineseConfig icd = new LanguageChineseConfig();
  168. if (allLangugeInfo.TryGetValue(id, out icd))
  169. {
  170. return icd.txt.Replace("\\n", "\n");
  171. }
  172. return "无" + id;
  173. }
  174. public string Text(int id, params object[] data)
  175. {
  176. //return "-ok";
  177. LanguageChineseConfig icd = new LanguageChineseConfig();
  178. if (allLangugeInfo.TryGetValue(id, out icd))
  179. {
  180. string d = icd.txt;
  181. d = StringForString(d, data);
  182. return d;
  183. }
  184. return "无";
  185. }
  186. public string Text2(int id, string[] data)
  187. {
  188. //return "-ok";
  189. LanguageChineseConfig icd = new LanguageChineseConfig();
  190. if (allLangugeInfo.TryGetValue(id, out icd))
  191. {
  192. string d = icd.txt;
  193. d = StringForString(d, data);
  194. return d;
  195. }
  196. return "无";
  197. }
  198. public string Text2(int id, float[] data)
  199. {
  200. //return "-ok";
  201. LanguageChineseConfig icd = new LanguageChineseConfig();
  202. if (allLangugeInfo.TryGetValue(id, out icd))
  203. {
  204. string d = icd.txt;
  205. d = StringForString(d, data);
  206. return d;
  207. }
  208. return "无";
  209. }
  210. private string Text(int id, Array data)
  211. {
  212. //return "-ok";
  213. LanguageChineseConfig icd = new LanguageChineseConfig();
  214. if (allLangugeInfo.TryGetValue(id, out icd))
  215. {
  216. string d = icd.txt;
  217. d = StringForString(d, data);
  218. return d;
  219. }
  220. return "无";
  221. }
  222. public static string StringForString(string message, Array par)
  223. {
  224. try
  225. {
  226. if (par != null)
  227. {
  228. switch (par.Length)
  229. {
  230. case 0: return message;
  231. case 1: return string.Format(message, par.GetValue(0));
  232. case 2: return string.Format(message, par.GetValue(0), par.GetValue(1));
  233. case 3: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2));
  234. case 4: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3));
  235. case 5: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4));
  236. case 6: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5));
  237. case 7: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6));
  238. case 8: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7));
  239. case 9:
  240. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  241. par.GetValue(8));
  242. case 10:
  243. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  244. par.GetValue(8),
  245. par.GetValue(9));
  246. case 11:
  247. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  248. par.GetValue(8),
  249. par.GetValue(9), par.GetValue(10));
  250. }
  251. }
  252. }
  253. catch (Exception e)
  254. {
  255. LogTool.Exception(e);
  256. return message;
  257. }
  258. return message;
  259. }
  260. /// <summary>
  261. /// 用于临时写,便于后期统一替换.
  262. /// </summary>
  263. /// <param name="tepStr"></param>
  264. /// <returns></returns>
  265. public string Text(string tepStr, params object[] data)
  266. {
  267. if (tepStr == "")
  268. return "";
  269. return StringForString(tepStr, data);
  270. }
  271. // public string ConfigFontText(UILabel label, string text)
  272. // {
  273. // if (text == null || text == FixationString.empty || text.Length < 3)
  274. // {
  275. // return text;
  276. // }
  277. //
  278. // if (text[0] != '[')
  279. // {
  280. // return text;
  281. // }
  282. //
  283. // if (text[2] != '=')
  284. // {
  285. // return text;
  286. // }
  287. //
  288. // char charData = text[1];
  289. // System.Text.StringBuilder builder = null;
  290. // int endIndex = 0;
  291. //
  292. // builder = new System.Text.StringBuilder();
  293. // int i = 3;
  294. // bool isPP = false;
  295. // for (; i < text.Length; i++)
  296. // {
  297. // char c = text[i];
  298. // if (c != ']')
  299. // {
  300. // builder.Append(c);
  301. // }
  302. // else
  303. // {
  304. // isPP = true;
  305. // break;
  306. // }
  307. // }
  308. //
  309. // if (!isPP)
  310. // {
  311. // return text;
  312. // }
  313. //
  314. // string valueStr = builder.ToString();
  315. // endIndex = i;
  316. //
  317. // switch (charData)
  318. // {
  319. // case 'S':
  320. // case 's': //大小
  321. // int size = int.Parse(valueStr);
  322. // label.fontSize = size;
  323. // break;
  324. // case 'F':
  325. // case 'f': //字体
  326. // SetFont(label, valueStr);
  327. // break;
  328. // case 'X':
  329. // case 'x': //x间距
  330. // int x = int.Parse(valueStr);
  331. // label.spacingX = x;
  332. // break;
  333. // case 'Y':
  334. // case 'y': //y间距
  335. // int y = int.Parse(valueStr);
  336. // label.spacingY = y;
  337. // break;
  338. // case 'C':
  339. // case 'c': //y间距
  340. // int c = int.Parse(valueStr);
  341. // if (c == 1)
  342. // {
  343. // label.fontStyle = FontStyle.Bold;
  344. // }
  345. //
  346. // if (c == 2)
  347. // {
  348. // label.fontStyle = FontStyle.Normal;
  349. // }
  350. //
  351. // break;
  352. // case 'K':
  353. // case 'k': //y间距
  354. // int k = int.Parse(valueStr);
  355. // if (k == 1)
  356. // {
  357. // label.fontStyle = FontStyle.Italic;
  358. // }
  359. //
  360. // if (k == 2)
  361. // {
  362. // label.fontStyle = FontStyle.Normal;
  363. // }
  364. //
  365. // break;
  366. // }
  367. //
  368. // if (builder != null)
  369. // {
  370. // text = text.Remove(0, endIndex + 1);
  371. // return ConfigFontText(label, text);
  372. // }
  373. //
  374. // return text;
  375. // }
  376. public string GetLanguageMassge(int id)
  377. {
  378. return Text(id);
  379. }
  380. }
  381. }