Language.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = new Dictionary<string, string>();
  9. private static SystemLanguage currentLan;
  10. private static bool initialized;
  11. public static void Init (SystemLanguage lan)
  12. {
  13. Debuger.Log("Init Language "+lan.ToString());
  14. initialized = true;
  15. currentLan = lan;
  16. TextAsset textAsset = (TextAsset)Resources.Load("XML/Config/All/word_config");
  17. XmlDocument xml = new XmlDocument();
  18. xml.LoadXml(textAsset.text);
  19. lanDict.Clear ();
  20. XmlNode mapNode = xml.SelectSingleNode("data");
  21. XmlNodeList nodeList = mapNode.SelectNodes("item");
  22. for(int i=0; i<nodeList.Count; i++)
  23. {
  24. XmlElement el = nodeList [i] as XmlElement;
  25. string title = el.GetAttribute ("title");
  26. string label = el.GetAttribute ("label");
  27. string word = el.GetAttribute (lan.ToString());
  28. string key = title + "_" + label;
  29. if (lanDict.ContainsKey (key))
  30. Debuger.LogError ("Language duplicate "+key);
  31. else
  32. lanDict.Add (key, word);
  33. }
  34. }
  35. private static SystemLanguage GetSystemLanguage()
  36. {
  37. // return SystemLanguage.Chinese;
  38. // return SystemLanguage.English;
  39. if(Application.systemLanguage == SystemLanguage.Chinese || Application.systemLanguage == SystemLanguage.ChineseSimplified)
  40. return SystemLanguage.Chinese;
  41. return SystemLanguage.English;
  42. }
  43. public static SystemLanguage GetCurrentLanguage()
  44. {
  45. if(!initialized)
  46. Init (GetSystemLanguage());
  47. return currentLan;
  48. }
  49. public static string GetStr(string page, string id)
  50. {
  51. if (!initialized)
  52. Init (GetSystemLanguage ());
  53. string key = page + "_" + id;
  54. if (lanDict.ContainsKey (key)) {
  55. string str = lanDict [key];
  56. str = str.Replace ("\\n", "\n");
  57. return str;
  58. }
  59. string error = "Topic missing page["+page+"] id["+id+"]";
  60. Debuger.LogError(error);
  61. return error;
  62. }
  63. }