LanguageUtils.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections.Generic;
  2. namespace XGame.Framework.i18n
  3. {
  4. public static class LanguageUtils
  5. {
  6. private static Dictionary<string, LanguageType> _langFlagNameMap = new Dictionary<string, LanguageType>()
  7. {
  8. {"ar",LanguageType.AR },
  9. {"en",LanguageType.EN },
  10. {"zh-cn",LanguageType.ZH_CN },
  11. {"zh-hk",LanguageType.ZH_HK },
  12. {"zh-sg",LanguageType.ZH_SG },
  13. };
  14. public static string ToName(this LanguageType flag)
  15. {
  16. switch(flag)
  17. {
  18. case LanguageType.ZH_CN:
  19. return "zh-cn";
  20. case LanguageType.ZH_HK:
  21. return "zh-hk";
  22. case LanguageType.ZH_SG:
  23. return "zh-sg";
  24. default:
  25. return flag.ToString().ToLower();
  26. }
  27. }
  28. public static LanguageType ToLanguageType(string languageName)
  29. {
  30. if (_langFlagNameMap.TryGetValue(languageName, out var langFlag))
  31. {
  32. return langFlag;
  33. }
  34. Log.Error($"多语言类型转换错误. Name:{languageName}");
  35. return LanguageType.NONE;
  36. }
  37. public static bool IsLanguage(string languageName)
  38. {
  39. return _langFlagNameMap.ContainsKey(languageName);
  40. }
  41. }
  42. }