LanguagePackConf.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using BeinLab.Util;
  2. using PublicTools.XMLDataBase;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace XRTool.Util
  7. {
  8. /// <summary>
  9. /// 语言包的配置
  10. /// </summary>
  11. public class LanguagePackConf
  12. {
  13. private string language = "简体中文";
  14. private string packagePath;
  15. /// 语言包的名称
  16. /// </summary>
  17. public string Language { get => language; set => language = value; }
  18. /// <summary>
  19. /// 语言包的路径
  20. /// </summary>
  21. public string PackagePath { get => packagePath; set => packagePath = value; }
  22. public Dictionary<string, LanguageConf> LanguageMap { get => languageMap; set => languageMap = value; }
  23. private int min, max;
  24. public int GetMax()
  25. {
  26. return max;
  27. }
  28. public int GetMin()
  29. {
  30. return min;
  31. }
  32. /// <summary>
  33. /// 语言文本映射
  34. /// </summary>
  35. private Dictionary<string, LanguageConf> languageMap;
  36. public void ReadLanguageMap()
  37. {
  38. if (LanguageMap == null)
  39. {
  40. LanguageMap = new Dictionary<string, LanguageConf>();
  41. string tableName = typeof(LanguageConf).Name;
  42. string path = Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath, packagePath);
  43. #if UNITY_EDITOR || !UNITY_ANDROID
  44. XSql.Instance.CloseTable(tableName);
  45. var table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
  46. table.Open();
  47. InitLanguage(table.FindAllData<LanguageConf>());
  48. #else
  49. string fullPath = Path.Combine(path, tableName + ".xml");
  50. GameNode.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<LanguageConf> packList) =>
  51. {
  52. InitLanguage(packList);
  53. }));
  54. #endif
  55. }
  56. }
  57. public void InitLanguage(List<LanguageConf> list)
  58. {
  59. if (list != null)
  60. {
  61. for (int i = 0; i < list.Count; i++)
  62. {
  63. if (!LanguageMap.ContainsKey(list[i].PriKey))
  64. {
  65. if (i == 0)
  66. {
  67. string id= list[i].PriKey;
  68. id= id.Replace(BuildConfig.Instance.prefix,"");
  69. int.TryParse(id,out min);
  70. }
  71. else if (i == list.Count - 1)
  72. {
  73. string id = list[i].PriKey;
  74. id = id.Replace(BuildConfig.Instance.prefix, "");
  75. int.TryParse(id, out max);
  76. }
  77. LanguageMap.Add(list[i].PriKey, list[i]);
  78. }
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// 获取对应的语言文本的配置
  84. ///
  85. /// </summary>
  86. /// <param name="prikey"></param>
  87. public LanguageConf GetLanguage(string prikey)
  88. {
  89. if (!string.IsNullOrEmpty(prikey))
  90. {
  91. prikey = prikey.Trim();
  92. if (!prikey.StartsWith(BuildConfig.Instance.prefix))
  93. {
  94. prikey = BuildConfig.Instance.prefix + prikey;
  95. }
  96. if (LanguageMap != null && LanguageMap.ContainsKey(prikey))
  97. {
  98. return LanguageMap[prikey];
  99. }
  100. }
  101. return null;
  102. }
  103. }
  104. }