123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- using System;
- using System.Collections.Generic;
- using Excel2Json;
- using Fort23.Core;
- using Fort23.UTool;
- using UnityEngine;
- using UnityEngine.UI;
- using Utility;
- namespace Core.Language
- {
- public class LanguageManager : Singleton<LanguageManager>, ITextTool
- {
- public enum LanguageType
- {
- LanguageChineseConfig = 0,
- LanguageEnglishConfig,
- LanguageDEConfig,
- LanguageFRConfig,
- LanguageRUConfig,
- LanguageTHConfig,
- /// <summary>
- /// 越南語
- /// </summary>
- LanguageVNConfig,
- }
- public TextAsset blockWord;
- private FontSettingConfig fontSettingConfig;
- //private LanguageChineseConfig textConfig;
- private Map<int, LanguageChineseConfig> allLangugeInfo = new Map<int, LanguageChineseConfig>();
- // public Map<int, StartGameLanguageChineseConfig> allLoginLangugeInfo = new Map<int, StartGameLanguageChineseConfig>();
- public Map<string, Font> fonts = new Map<string, Font>();
- public LanguageType currLanguageType;
- private FontSettingConfig.LanguageConfig currConfig;
- private bool isLaod;
- public async CTask InitManager()
- {
- Debug.Log("开始FontSettingConfig");
- AssetHandle assetBundle =
- await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<FontSettingConfig>("FontSettingConfig.asset");
- fontSettingConfig = assetBundle.AssetObject<FontSettingConfig>();
- if (fontSettingConfig != null)
- {
- fontSettingConfig.Init();
- }
- else
- {
- Debug.Log("加载失败FontSettingConfig");
- }
- UGUITextTool.TextTool = this;
- await SwitchTextConfig(LanguageType.LanguageChineseConfig, null);
- }
- public async CTask SwitchTextConfig(LanguageType languageType, System.Action callBack)
- {
- if (currLanguageType == languageType && isLaod)
- {
- callBack?.Invoke();
- return;
- }
- isLaod = true;
- this.currLanguageType = languageType;
- Debug.Log("SwitchTextConfig");
- currConfig = fontSettingConfig.GetLanguageConfig(languageType);
- Debug.Log("SwitchTextConfig" + currConfig.fontConfig.Count);
- string configJson = languageType.ToString() + ".json";
- AssetHandle assetBundle =
- await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(configJson);
- TextAsset ta = assetBundle.AssetObject<TextAsset>();
- LanguageChineseConfigHolder configDataHolder = JsonManager.FromJson<LanguageChineseConfigHolder>(ta.text);
- assetBundle.Release();
- configDataHolder.Init();
- allLangugeInfo.Clear();
- List<LanguageChineseConfig> configList = configDataHolder.configList;
- for (int i = 0; i < configList.Count; i++)
- {
- allLangugeInfo.Add(configList[i].ID, configList[i]);
- }
- // configJson = "StartGame" + languageType + ".json";
- // assetBundle =
- // await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(configJson);
- // ta = assetBundle.AssetObject<TextAsset>();
- // StartGameLanguageChineseConfigHolder configDataHolder2 = JsonManager.FromJson<StartGameLanguageChineseConfigHolder>(ta.text);
- // assetBundle.Release();
- // configDataHolder2.Init();
- // allLoginLangugeInfo.Clear();
- // List<StartGameLanguageChineseConfig> configList2 = configDataHolder2.configList;
- // for (int i = 0; i < configList2.Count; i++)
- // {
- // allLoginLangugeInfo.Add(configList2[i].ID, configList2[i]);
- // }
- callBack?.Invoke();
- // AIhelpService.Instance.ChangeLanguage();
- }
- public int GetFontIndix(string fontName)
- {
- if (fontSettingConfig == null)
- {
- return 0;
- }
- for (int i = 0; i < fontSettingConfig.languageConfig.Count; i++)
- {
- List<FontSettingConfig.FontConfig> fontConfig = fontSettingConfig.languageConfig[i].fontConfig;
- for (int j = 0; j < fontConfig.Count; j++)
- {
- if (fontConfig[j].fontName.Equals(fontName))
- {
- return fontConfig[j].index;
- }
- }
- }
- return 0;
- }
- // public void SetFont(UILabel label)
- // {
- // if (label.trueTypeFont == null)
- // {
- // return;
- // }
- //
- // string fontName = label.trueTypeFont.name;
- // int index = GetFontIndix(fontName);
- // if (currConfig == null)
- // {
- // return;
- // }
- //
- // for (int i = 0; i < currConfig.fontConfig.Count; i++)
- // {
- // if (currConfig.fontConfig[i].index == index)
- // {
- // fontName = currConfig.fontConfig[i].fontName;
- // break;
- // }
- // }
- //
- // if (fonts.ContainsKey(fontName))
- // {
- // label.trueTypeFont = fonts[fontName];
- // if (label.defaultConfig != null)
- // {
- // label.defaultConfig.defaultFont = label.trueTypeFont;
- // }
- // }
- // // LogTool.LogError("没有字体"+ currConfig.languageType.ToString()+" "+ index);
- // }
- // public void SetFont(UILabel uilabel, string fontName)
- // {
- // if (fonts.ContainsKey(fontName))
- // {
- // uilabel.trueTypeFont = fonts[fontName];
- // }
- // else
- // {
- // LoadResourceTask task = AssetBundleLoadManager.Instance.AddAssetTask(fontName, delegate(Object obj, object data)
- // {
- // if (!fonts.ContainsKey(fontName))
- // {
- // fonts.Add(fontName, (Font) obj);
- // }
- //
- // uilabel.trueTypeFont = (Font) obj;
- // }, erroCallBack: delegate() { LogTool.Log("没找到这个字体"); });
- // }
- // }
- public string Text(int id)
- {
- //return "-ok";
- LanguageChineseConfig icd = new LanguageChineseConfig();
- if (allLangugeInfo.TryGetValue(id, out icd))
- {
- return icd.txt.Replace("\\n", "\n");
- }
- return "无" + id;
- }
-
- public string Text(int id, params object[] data)
- {
- //return "-ok";
- LanguageChineseConfig icd = new LanguageChineseConfig();
- if (allLangugeInfo.TryGetValue(id, out icd))
- {
- string d = icd.txt;
- d = StringForString(d, data);
- return d;
- }
- return "无";
- }
- public string Text2(int id, string[] data)
- {
- //return "-ok";
- LanguageChineseConfig icd = new LanguageChineseConfig();
- if (allLangugeInfo.TryGetValue(id, out icd))
- {
- string d = icd.txt;
- d = StringForString(d, data);
- return d;
- }
- return "无";
- }
- public string Text2(int id, float[] data)
- {
- //return "-ok";
- LanguageChineseConfig icd = new LanguageChineseConfig();
- if (allLangugeInfo.TryGetValue(id, out icd))
- {
- string d = icd.txt;
- d = StringForString(d, data);
- return d;
- }
- return "无";
- }
- private string Text(int id, Array data)
- {
- //return "-ok";
- LanguageChineseConfig icd = new LanguageChineseConfig();
- if (allLangugeInfo.TryGetValue(id, out icd))
- {
- string d = icd.txt;
- d = StringForString(d, data);
- return d;
- }
- return "无";
- }
- public static string StringForString(string message, Array par)
- {
- try
- {
- if (par != null)
- {
- switch (par.Length)
- {
- case 0: return message;
- case 1: return string.Format(message, par.GetValue(0));
- case 2: return string.Format(message, par.GetValue(0), par.GetValue(1));
- case 3: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2));
- case 4: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3));
- case 5: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4));
- case 6: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5));
- 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));
- 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));
- case 9:
- 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),
- par.GetValue(8));
- case 10:
- 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),
- par.GetValue(8),
- par.GetValue(9));
- case 11:
- 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),
- par.GetValue(8),
- par.GetValue(9), par.GetValue(10));
- }
- }
- }
- catch (Exception e)
- {
- LogTool.Exception(e);
- return message;
- }
- return message;
- }
- /// <summary>
- /// 用于临时写,便于后期统一替换.
- /// </summary>
- /// <param name="tepStr"></param>
- /// <returns></returns>
- public string Text(string tepStr, params object[] data)
- {
- if (tepStr == "")
- return "";
- return StringForString(tepStr, data);
- }
- // public string ConfigFontText(UILabel label, string text)
- // {
- // if (text == null || text == FixationString.empty || text.Length < 3)
- // {
- // return text;
- // }
- //
- // if (text[0] != '[')
- // {
- // return text;
- // }
- //
- // if (text[2] != '=')
- // {
- // return text;
- // }
- //
- // char charData = text[1];
- // System.Text.StringBuilder builder = null;
- // int endIndex = 0;
- //
- // builder = new System.Text.StringBuilder();
- // int i = 3;
- // bool isPP = false;
- // for (; i < text.Length; i++)
- // {
- // char c = text[i];
- // if (c != ']')
- // {
- // builder.Append(c);
- // }
- // else
- // {
- // isPP = true;
- // break;
- // }
- // }
- //
- // if (!isPP)
- // {
- // return text;
- // }
- //
- // string valueStr = builder.ToString();
- // endIndex = i;
- //
- // switch (charData)
- // {
- // case 'S':
- // case 's': //大小
- // int size = int.Parse(valueStr);
- // label.fontSize = size;
- // break;
- // case 'F':
- // case 'f': //字体
- // SetFont(label, valueStr);
- // break;
- // case 'X':
- // case 'x': //x间距
- // int x = int.Parse(valueStr);
- // label.spacingX = x;
- // break;
- // case 'Y':
- // case 'y': //y间距
- // int y = int.Parse(valueStr);
- // label.spacingY = y;
- // break;
- // case 'C':
- // case 'c': //y间距
- // int c = int.Parse(valueStr);
- // if (c == 1)
- // {
- // label.fontStyle = FontStyle.Bold;
- // }
- //
- // if (c == 2)
- // {
- // label.fontStyle = FontStyle.Normal;
- // }
- //
- // break;
- // case 'K':
- // case 'k': //y间距
- // int k = int.Parse(valueStr);
- // if (k == 1)
- // {
- // label.fontStyle = FontStyle.Italic;
- // }
- //
- // if (k == 2)
- // {
- // label.fontStyle = FontStyle.Normal;
- // }
- //
- // break;
- // }
- //
- // if (builder != null)
- // {
- // text = text.Remove(0, endIndex + 1);
- // return ConfigFontText(label, text);
- // }
- //
- // return text;
- // }
- public string GetLanguageMassge(int id)
- {
- return Text(id);
- }
- }
- }
|