LocalizationManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Nxr.Internal
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class LocalizationManager
  9. {
  10. private static LocalizationManager _instance;
  11. public static LocalizationManager GetInstance
  12. {
  13. get
  14. {
  15. if (_instance == null)
  16. {
  17. _instance = new LocalizationManager();
  18. }
  19. return _instance;
  20. }
  21. }
  22. public const string chinese = "Chinese";
  23. public const string english = "English";
  24. private Dictionary<string, string> dic_CN = new Dictionary<string, string>();
  25. private Dictionary<string, string> dic_EN = new Dictionary<string, string>();
  26. /// <summary>
  27. /// Read the configuration file and save the information of file in the dictionary.
  28. /// </summary>
  29. public LocalizationManager()
  30. {
  31. // cn
  32. TextAsset taCN = Resources.Load<TextAsset>(chinese);
  33. string text = taCN.text;
  34. string[] linesCN = text.Split('\n');
  35. foreach (string line in linesCN)
  36. {
  37. if (line == null || line.Length <= 1)
  38. {
  39. continue;
  40. }
  41. string[] keyAndValue = line.Split('=');
  42. // Debug.Log("line=" + line + "," + line.Length);
  43. dic_CN.Add(keyAndValue[0], keyAndValue[1].Replace("\\n", "\n"));
  44. }
  45. // en
  46. TextAsset taEN = Resources.Load<TextAsset>(english);
  47. text = taEN.text;
  48. string[] linesEN = text.Split('\n');
  49. foreach (string line in linesEN)
  50. {
  51. if (line == null || line.Length <= 1)
  52. {
  53. continue;
  54. }
  55. string[] keyAndValue = line.Split('=');
  56. // Debug.Log("line=" + line + "," + line.Length);
  57. dic_EN.Add(keyAndValue[0], keyAndValue[1].Replace("\\n", "\n"));
  58. }
  59. }
  60. /// <summary>
  61. /// Get the value of Dictionary.
  62. /// </summary>
  63. /// <param name="key"></param>
  64. /// <returns></returns>
  65. public string GetValue(string key)
  66. {
  67. Dictionary<string, string> dic = getDIC();
  68. if (dic.ContainsKey(key) == false)
  69. {
  70. return null;
  71. }
  72. string value = null;
  73. dic.TryGetValue(key, out value);
  74. return value;
  75. }
  76. public string GetValue(string key, string language)
  77. {
  78. Dictionary<string, string> dic = language == chinese ? dic_CN : dic_EN;
  79. if (dic.ContainsKey(key) == false)
  80. {
  81. return null;
  82. }
  83. string value = null;
  84. dic.TryGetValue(key, out value);
  85. return value;
  86. }
  87. private Dictionary<string, string> getDIC()
  88. {
  89. return isCN() ? dic_CN : dic_EN;
  90. }
  91. private bool isCN()
  92. {
  93. if (languageType != null) return languageType == chinese;
  94. return Application.systemLanguage == SystemLanguage.ChineseSimplified || Application.systemLanguage == SystemLanguage.Chinese
  95. || Application.systemLanguage == SystemLanguage.ChineseTraditional;
  96. }
  97. private string languageType = null;
  98. public void ChangeLanguage(string language)
  99. {
  100. languageType = language;
  101. }
  102. }
  103. }