| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class LocalizedSpriteCfg
- {
- public string key;
- public string Language;
- public string AssetPath;
- public string OtherSetting;
- public string SetNativeSize;
- public LocalizedSpriteCfg()
- {
- key = "";
- Language = "";
- AssetPath = "";
- OtherSetting = "";
- SetNativeSize = "0";
- }
- public bool GetSetNativeSize()
- {
- return !string.IsNullOrEmpty(SetNativeSize) && SetNativeSize != "0";
- }
- }
- public class GameLanguageCfg
- {
- public const string FileName = "LanguageCfg";
- public string key;
- public string language;
- public GameLanguageCfg()
- {
- key = "";
- language = "";
- }
- }
- public class LocalizedTextureCfgMgr : Singleton<LocalizedTextureCfgMgr>
- {
- public static string baseName = "LocalizeTextureCfg";
- private static bool isOpen = false;
- public static bool IsOpen { get => isOpen; }
- private string baseKey;
- private string curLanguageKey;
- public string CurLanguageKey { get => curLanguageKey; }
- private string oldLanguageKey;
- private Dictionary<string, Dictionary<string, LocalizedSpriteCfg>> cfgs = new Dictionary<string, Dictionary<string, LocalizedSpriteCfg>>();
- private Dictionary<string, LocalizedSpriteCfg> curCfg;
- private Dictionary<string, GameLanguageCfg> gameLanguageCfgs = new Dictionary<string, GameLanguageCfg>();
- public override void Init()
- {
- ReadlanguageCfg();
- if (!isOpen)
- {
- return;
- }
- //SetLanguageLocalSetting("_tw");
- string defaultlg = GetLanguageLocalSetting();
- string systemlg = GetSystemDefaultLanguage();
- string curKey = "";
- if (!string.IsNullOrEmpty(defaultlg))
- {
- curKey = defaultlg;
- }
- else if (!string.IsNullOrEmpty(systemlg))
- {
- curKey = systemlg;
- }
- else
- {
- curKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
- }
- ConfigMgr.CurLangKey = curKey;
- curLanguageKey = curKey;
- oldLanguageKey = "";
- baseKey = GetLanguageCfgByKey(LanguageFileEx.Unknown);
- ReadConfig();
- }
- private void ReadConfig()
- {
- curCfg = AddCfgByCfgName(curLanguageKey);
- }
- private Dictionary<string, LocalizedSpriteCfg> AddCfgByCfgName(string name)
- {
- string cfgname = GetLanguageCfgName(name);
- if (!cfgs.ContainsKey(name))
- {
- Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(cfgname);
- if (cfgdata == null)
- {
- return null;
- }
- Dictionary<string, LocalizedSpriteCfg> cfg = new Dictionary<string, LocalizedSpriteCfg>();
- foreach (var item in cfgdata)
- {
- LocalizedSpriteCfg localizedSpriteCfg = new LocalizedSpriteCfg();
- localizedSpriteCfg.key = item.Key;
- Dictionary<string, string> _value = item.Value;
- localizedSpriteCfg.Language = _value["Language"];
- localizedSpriteCfg.AssetPath = _value["AssetPath"];
- localizedSpriteCfg.OtherSetting = _value["OtherSetting"];
- localizedSpriteCfg.SetNativeSize = _value["SetNativeSize"];
- cfg.Add(item.Key, localizedSpriteCfg);
- }
- cfgs.Add(name, cfg);
- return cfg;
- }
- return cfgs[name];
- }
- public void SetLanguage(string language)
- {
- if (!string.IsNullOrEmpty(language) && language != curLanguageKey)
- {
- oldLanguageKey = curLanguageKey;
- curLanguageKey = language;
- ReadConfig();
- ConfigMgr.CurLangKey = language;
- EventMgr.DispatchEvent(new CoreEvent<int>(ECoreEventType.EID_LANGUAGE_CHANGE, 1));
- }
- }
- private string GetLanguageCfgName(string lg)
- {
- return $"{baseName}{lg}";
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="key"></param>
- /// <param name="notbase">要求不是base才返回</param>
- /// <returns></returns>
- public LocalizedSpriteCfg GetLocalizedSpriteCfg(string key, bool notbase = true)
- {
- if (!isOpen ||
- (notbase && curLanguageKey == oldLanguageKey))
- {
- return null;
- }
- if (curCfg == null)
- {
- ReadConfig();
- }
- if (curCfg == null)
- {
- return null;
- }
- if (curCfg.ContainsKey(key))
- {
- return curCfg[key];
- }
- return null;
- }
- public LocalizedSpriteCfg GetCfgByLgkey(string lgKey, string key, bool def = true)
- {
- if (!isOpen)
- {
- return null;
- }
- Dictionary<string, LocalizedSpriteCfg> tcfg = AddCfgByCfgName(lgKey);
- if (tcfg != null && tcfg.ContainsKey(key))
- {
- return tcfg[key];
- }
- if (def)
- {
- tcfg = AddCfgByCfgName(baseKey);
- if (tcfg != null && tcfg.ContainsKey(key))
- {
- return tcfg[key];
- }
- }
- return null;
- }
- public string GetLocalize(string key, bool notbase = false)
- {
- if (!isOpen)
- {
- return "";
- }
- if (notbase && curLanguageKey == oldLanguageKey)
- {
- return "";
- }
- if (curCfg == null)
- {
- ReadConfig();
- }
- if (curCfg == null)
- {
- return "";
- }
- if (curCfg.ContainsKey(key))
- {
- return curCfg[key].Language;
- }
- return "";
- }
- private void ReadlanguageCfg()
- {
- Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(GameLanguageCfg.FileName);
- if (cfgdata == null)
- {
- return;
- }
- gameLanguageCfgs.Clear();
- foreach (var item in cfgdata)
- {
- GameLanguageCfg cfg = new GameLanguageCfg();
- cfg.key = item.Key;
- cfg.language = item.Value["Language"];
- gameLanguageCfgs.Add(cfg.key, cfg);
- }
- isOpen = FileHelper.CheckStringIsTrue(GetLanguageCfgByKey("openSetting"));
- }
- public void SetLanguageByKey(string key)
- {
- if (!isOpen)
- {
- return;
- }
- string lg = GetLanguageCfgByKey(key);
- if (!string.IsNullOrEmpty(lg))
- {
- SetLanguage(lg);
- }
- }
- public string GetLanguageCfgByKey(string key)
- {
- if (gameLanguageCfgs.ContainsKey(key))
- {
- return gameLanguageCfgs[key].language;
- }
- return "";
- }
- public string GetSystemDefaultLanguage()
- {
- string ret = LanguageFileEx.Unknown;
- switch (UnityEngine.Application.systemLanguage)
- {
- case UnityEngine.SystemLanguage.Vietnamese: //越南语
- {
- ret = LanguageFileEx.Vietnamese;
- }
- break;
- case UnityEngine.SystemLanguage.Thai: //泰语
- {
- ret = LanguageFileEx.Thai;
- }
- break;
- case UnityEngine.SystemLanguage.English:
- {
- ret = LanguageFileEx.English;
- }
- break;
- case UnityEngine.SystemLanguage.Chinese:
- case UnityEngine.SystemLanguage.ChineseSimplified:
- case UnityEngine.SystemLanguage.ChineseTraditional:
- {
- ret = LanguageFileEx.Chinese;
- }
- break;
- case UnityEngine.SystemLanguage.Unknown:
- {
- ret = LanguageFileEx.Unknown;
- }
- break;
- default:
- break;
- }
- UnityEngine.Debug.Log(UnityEngine.Application.systemLanguage.ToString() + " 系统语言:" +ret);
- return GetLanguageCfgByKey(ret);
- }
- public string GetCurLanguageKey()
- {
- foreach (var item in gameLanguageCfgs)
- {
- if (item.Value.language == curLanguageKey && item.Key != LanguageFileEx.Unknown)
- {
- return item.Key;
- }
- }
- return "";
- }
- public static string GetLanguageLocalSetting()
- {
- return UnityEngine.PlayerPrefs.GetString("LanguageLocalSetting", string.Empty);
- }
- public static void SetLanguageLocalSetting(string key)
- {
- UnityEngine.PlayerPrefs.SetString("LanguageLocalSetting", key);
- }
- }
|