Language.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return SystemLanguage.English;
  46. // return SystemLanguage.Chinese;
  47. }
  48. public static SystemLanguage GetCurrentLanguage()
  49. {
  50. if(xml == null)
  51. Init (GetSystemLanguage());
  52. return currentLan;
  53. }
  54. public static string GetStr(string page, string id)
  55. {
  56. if(xml == null)
  57. {
  58. Init (GetSystemLanguage());
  59. }
  60. try
  61. {
  62. XmlNode pageNode = xml.SelectSingleNode("lan/"+page);
  63. XmlNode node = pageNode.SelectSingleNode(id);
  64. if(node != null)
  65. return node.InnerText.Replace("\\n", "\n");
  66. }
  67. catch(Exception e)
  68. {
  69. Debuger.LogException(e);
  70. }
  71. string error = "Topic missing page["+page+"] id["+id+"]";
  72. Debuger.LogError(error);
  73. return error;
  74. }
  75. }