LanguageManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public enum CurrentLanguage
  7. {
  8. Default,
  9. English,
  10. ChineseSimplified,
  11. ChineseTraditional,
  12. }
  13. public class MulLanStr
  14. {
  15. public string ID;
  16. public string Page;
  17. public MulLanStr(string pageAndID)
  18. {
  19. string[] strings = pageAndID.Split(new[] { LanguageLabel.LanguagePageSeperator }, StringSplitOptions.None);
  20. Page = strings[0];
  21. ID = strings[1];
  22. }
  23. public override string ToString()
  24. {
  25. return Language.GetStr(Page, ID);
  26. }
  27. }
  28. public class LanguageManager : Regist
  29. {
  30. #region Config
  31. public static Action<CurrentLanguage, CurrentLanguage> OnLanguageChange;
  32. public static CurrentLanguage CurrentLanguage;
  33. public static Dictionary<Text, object[]> TextDictionary = new Dictionary<Text, object[]>();
  34. public static Dictionary<TextMesh, object[]> TextMeshDictionary = new Dictionary<TextMesh, object[]>();
  35. #endregion
  36. public static void Add(Text text, params object[] objs)
  37. {
  38. if (TextDictionary.ContainsKey(text))
  39. {
  40. TextDictionary[text] = objs;
  41. text.text = Translate(objs);
  42. }
  43. else
  44. {
  45. TextDictionary.Add(text, objs);
  46. text.text = Translate(objs);
  47. }
  48. }
  49. public static void Add(TextMesh textMesh, params object[] objs)
  50. {
  51. if (TextMeshDictionary.ContainsKey(textMesh))
  52. {
  53. TextMeshDictionary[textMesh] = objs;
  54. textMesh.text = Translate(objs);
  55. }
  56. else
  57. {
  58. TextMeshDictionary.Add(textMesh, objs);
  59. textMesh.text = Translate(objs);
  60. }
  61. }
  62. public static void SwitchLanguage(CurrentLanguage currentLanguage)
  63. {
  64. CurrentLanguage fromLanguage = CurrentLanguage;
  65. CurrentLanguage = currentLanguage;
  66. Language.SwitchLanguage(currentLanguage);
  67. Manager.SwitchLanguage();
  68. foreach (var regist in Initializer.RegistList)
  69. {
  70. regist.SwitchLanguage();
  71. }
  72. foreach (var kv in TextDictionary)
  73. {
  74. kv.Key.text = Translate(kv.Value);
  75. }
  76. foreach (var kv in TextMeshDictionary)
  77. {
  78. kv.Key.text = Translate(kv.Value);
  79. }
  80. foreach (var kv in AchieveManager.AchieveItemDictionary)
  81. {
  82. if (kv.Value.Item != null)
  83. {
  84. kv.Value.DescriptionText.text = AchieveItem.GetDescription(kv.Value.Description, kv.Value.TargetValue);
  85. }
  86. }
  87. for (int i = 0; i < Manager.SkillList.Count; i++)
  88. {
  89. Manager.SkillList[i].SwitchLanguage();
  90. }
  91. if (OnLanguageChange != null)
  92. {
  93. OnLanguageChange.Invoke(fromLanguage, currentLanguage);
  94. }
  95. }
  96. public static string Translate(object[] objs)
  97. {
  98. string str = "";
  99. for (int i = 0; i < objs.Length; i++)
  100. {
  101. MulLanStr mulLanStr = objs[i] as MulLanStr;
  102. if (mulLanStr == null)
  103. {
  104. str += objs[i];
  105. }
  106. else
  107. {
  108. str += mulLanStr.ToString();
  109. }
  110. }
  111. return str;
  112. }
  113. }