Language.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.Xml;
  6. public class Language
  7. {
  8. private static Dictionary<string, string> lanDict;
  9. public static void init ()
  10. {
  11. lanDict = new Dictionary<string, string>();
  12. }
  13. public static string GetStr(string id)
  14. {
  15. if(lanDict == null)
  16. {
  17. init ();
  18. }
  19. if(!lanDict.ContainsKey(id))
  20. {
  21. return "Topic missing id["+id+"]";
  22. }
  23. return lanDict[id];
  24. }
  25. private static SystemLanguage defaultLan = SystemLanguage.English;
  26. private static SystemLanguage currentLan;
  27. private static XmlDocument xml;
  28. public static void Init (SystemLanguage lan)
  29. {
  30. Debuger.Log("Init Language "+lan.ToString());
  31. currentLan = lan;
  32. TextAsset textAsset = (TextAsset)Resources.Load("XML/Language/"+lan.ToString());
  33. if(textAsset == null)
  34. {
  35. Debuger.LogError("Can not find language use default "+defaultLan);
  36. textAsset = (TextAsset)Resources.Load("XML/Language/"+defaultLan.ToString());
  37. currentLan = defaultLan;
  38. }
  39. if(xml == null)
  40. xml = new XmlDocument();
  41. xml.LoadXml(textAsset.text);
  42. }
  43. private static SystemLanguage GetSystemLanguage()
  44. {
  45. if (Config.USER_PORT == Config.USER_PORT_INTERNATIONAL)
  46. return SystemLanguage.English;
  47. else
  48. return SystemLanguage.Chinese;
  49. // return SystemLanguage.Chinese;
  50. }
  51. public static SystemLanguage GetCurrentLanguage()
  52. {
  53. if(xml == null)
  54. Init (GetSystemLanguage());
  55. return currentLan;
  56. }
  57. public static string GetStr(string page, string id)
  58. {
  59. if(xml == null)
  60. {
  61. Init (GetSystemLanguage());
  62. }
  63. try
  64. {
  65. XmlNode pageNode = xml.SelectSingleNode("lan/"+page);
  66. XmlNode node = pageNode.SelectSingleNode(id);
  67. if(node != null)
  68. return node.InnerText.Replace("\\n", "\n");
  69. }
  70. catch(Exception e)
  71. {
  72. Debuger.LogException(e);
  73. }
  74. string error = "Topic missing page["+page+"] id["+id+"]";
  75. Debuger.LogError(error);
  76. return error;
  77. }
  78. }