LanguageManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.GetAll();
  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 = GetText(id);
  168. // LanguageChineseConfig icd = new LanguageChineseConfig();
  169. if (icd.ID > 0)
  170. {
  171. return icd.txt.Replace("\\n", "\n");
  172. }
  173. return "无" + id;
  174. }
  175. private LanguageChineseConfig GetText(int id)
  176. {
  177. LanguageChineseConfig icd = ConfigComponent.Instance.Get<LanguageChineseConfig>(id);
  178. return icd;
  179. }
  180. public string Text(int id, params object[] data)
  181. {
  182. //return "-ok";
  183. LanguageChineseConfig icd = GetText(id);
  184. if (icd.ID > 0)
  185. {
  186. string d = icd.txt;
  187. d = StringForString(d, data);
  188. return d;
  189. }
  190. return "无";
  191. }
  192. public string Text2(int id, string[] data)
  193. {
  194. //return "-ok";
  195. LanguageChineseConfig icd = GetText(id);
  196. if (icd.ID > 0)
  197. {
  198. string d = icd.txt;
  199. d = StringForString(d, data);
  200. return d;
  201. }
  202. return "无";
  203. }
  204. public string Text2(int id, float[] data)
  205. {
  206. //return "-ok";
  207. LanguageChineseConfig icd = GetText(id);
  208. if (icd.ID>0)
  209. {
  210. string d = icd.txt;
  211. d = StringForString(d, data);
  212. return d;
  213. }
  214. return "无";
  215. }
  216. private string Text(int id, Array data)
  217. {
  218. //return "-ok";
  219. LanguageChineseConfig icd = GetText(id);
  220. if (icd.ID>0)
  221. {
  222. string d = icd.txt;
  223. d = StringForString(d, data);
  224. return d;
  225. }
  226. return "无";
  227. }
  228. public static string StringForString(string message, Array par)
  229. {
  230. try
  231. {
  232. if (par != null)
  233. {
  234. switch (par.Length)
  235. {
  236. case 0: return message;
  237. case 1: return string.Format(message, par.GetValue(0));
  238. case 2: return string.Format(message, par.GetValue(0), par.GetValue(1));
  239. case 3: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2));
  240. case 4:
  241. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  242. par.GetValue(3));
  243. case 5:
  244. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  245. par.GetValue(3), par.GetValue(4));
  246. case 6:
  247. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  248. par.GetValue(3), par.GetValue(4), par.GetValue(5));
  249. case 7:
  250. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  251. par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6));
  252. case 8:
  253. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  254. par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7));
  255. case 9:
  256. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  257. par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  258. par.GetValue(8));
  259. case 10:
  260. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  261. par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  262. par.GetValue(8),
  263. par.GetValue(9));
  264. case 11:
  265. return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2),
  266. par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7),
  267. par.GetValue(8),
  268. par.GetValue(9), par.GetValue(10));
  269. }
  270. }
  271. }
  272. catch (Exception e)
  273. {
  274. LogTool.Exception(e);
  275. return message;
  276. }
  277. return message;
  278. }
  279. /// <summary>
  280. /// 用于临时写,便于后期统一替换.
  281. /// </summary>
  282. /// <param name="tepStr"></param>
  283. /// <returns></returns>
  284. public string Text(string tepStr, params object[] data)
  285. {
  286. if (tepStr == "")
  287. return "";
  288. return StringForString(tepStr, data);
  289. }
  290. // public string ConfigFontText(UILabel label, string text)
  291. // {
  292. // if (text == null || text == FixationString.empty || text.Length < 3)
  293. // {
  294. // return text;
  295. // }
  296. //
  297. // if (text[0] != '[')
  298. // {
  299. // return text;
  300. // }
  301. //
  302. // if (text[2] != '=')
  303. // {
  304. // return text;
  305. // }
  306. //
  307. // char charData = text[1];
  308. // System.Text.StringBuilder builder = null;
  309. // int endIndex = 0;
  310. //
  311. // builder = new System.Text.StringBuilder();
  312. // int i = 3;
  313. // bool isPP = false;
  314. // for (; i < text.Length; i++)
  315. // {
  316. // char c = text[i];
  317. // if (c != ']')
  318. // {
  319. // builder.Append(c);
  320. // }
  321. // else
  322. // {
  323. // isPP = true;
  324. // break;
  325. // }
  326. // }
  327. //
  328. // if (!isPP)
  329. // {
  330. // return text;
  331. // }
  332. //
  333. // string valueStr = builder.ToString();
  334. // endIndex = i;
  335. //
  336. // switch (charData)
  337. // {
  338. // case 'S':
  339. // case 's': //大小
  340. // int size = int.Parse(valueStr);
  341. // label.fontSize = size;
  342. // break;
  343. // case 'F':
  344. // case 'f': //字体
  345. // SetFont(label, valueStr);
  346. // break;
  347. // case 'X':
  348. // case 'x': //x间距
  349. // int x = int.Parse(valueStr);
  350. // label.spacingX = x;
  351. // break;
  352. // case 'Y':
  353. // case 'y': //y间距
  354. // int y = int.Parse(valueStr);
  355. // label.spacingY = y;
  356. // break;
  357. // case 'C':
  358. // case 'c': //y间距
  359. // int c = int.Parse(valueStr);
  360. // if (c == 1)
  361. // {
  362. // label.fontStyle = FontStyle.Bold;
  363. // }
  364. //
  365. // if (c == 2)
  366. // {
  367. // label.fontStyle = FontStyle.Normal;
  368. // }
  369. //
  370. // break;
  371. // case 'K':
  372. // case 'k': //y间距
  373. // int k = int.Parse(valueStr);
  374. // if (k == 1)
  375. // {
  376. // label.fontStyle = FontStyle.Italic;
  377. // }
  378. //
  379. // if (k == 2)
  380. // {
  381. // label.fontStyle = FontStyle.Normal;
  382. // }
  383. //
  384. // break;
  385. // }
  386. //
  387. // if (builder != null)
  388. // {
  389. // text = text.Remove(0, endIndex + 1);
  390. // return ConfigFontText(label, text);
  391. // }
  392. //
  393. // return text;
  394. // }
  395. public string GetLanguageMassge(int id)
  396. {
  397. return Text(id);
  398. }
  399. }
  400. }