using System; using System.Collections.Generic; using System.Xml; using UnityEngine; public class LocalizationText { private static IDictionary _content = new Dictionary(); private static SystemLanguage _language = SystemLanguage.English; public static SystemLanguage Language => _language; private static string _languageTag = "EN"; public static string LanguageTag => _languageTag; public static event Action OnLanguageChange; public static string GetText(string key) { string result = ""; if (string.IsNullOrEmpty(key)) return result; Content.TryGetValue(key, out result); if (string.IsNullOrEmpty(result)) { Logger.Log(key + "[" + Language + "]" + " No Text defined"); return key; } result = result.Replace("\\n", "\n"); return result; } public static void SetLanguage(SystemLanguage language) { if(_language == language) return; _language = language; switch (Language) { case SystemLanguage.Korean: _languageTag = "KO"; break; case SystemLanguage.Japanese: _languageTag = "JA"; break; default: _languageTag = "EN"; break; } CreateContent(); OnLanguageChange?.Invoke(); } private static IDictionary Content { get { if (_content == null || _content.Count == 0) { CreateContent(); } return _content; } } private static void AddContent(XmlNode xNode) { foreach (XmlNode node in xNode.ChildNodes) { if (node.LocalName != "TextKey") { continue; } string value = node.Attributes.GetNamedItem("name").Value; string text = string.Empty; foreach (XmlNode langNode in node) { if (langNode.LocalName != _languageTag) { continue; } text = langNode.InnerText; if (_content.ContainsKey(value)) { _content.Remove(value); _content.Add(value, value + " has been found multiple times in the XML allowed only once!"); } else { _content.Add(value, (!string.IsNullOrEmpty(text)) ? text : ("No Text for " + value + " found")); } break; } } } /// /// 로컬에 존재하는 xml 파일을 읽어서 텍스트를 생성한다. /// public static void CreateContent() { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(Resources.Load("Localization").ToString()); if (xmlDocument == null) { System.Console.WriteLine("Couldnt Load Xml"); return; } if (_content != null) { _content.Clear(); } XmlNode xNode = xmlDocument.ChildNodes.Item(1).ChildNodes.Item(0); AddContent(xNode); } /// /// 서버에서 받은 xml 파일을 읽어서 텍스트를 생성한다. /// public static void CreateContent(string xml) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); if (xmlDocument == null) { System.Console.WriteLine("Couldnt Load Xml"); return; } if (_content != null) { _content.Clear(); } XmlNode xNode = xmlDocument.ChildNodes.Item(1).ChildNodes.Item(0); AddContent(xNode); } }