using UnityEngine; using System.Collections.Generic; using System.Xml; using System.Data; public class LocalizationText { private static IDictionary _content = new Dictionary(); private static string _language = "EN"; private static string Language { get { return _language; } set { if (_language != value) { _language = value; CreateContent(); } } } 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 + "[" + LocalizationText.Language + "]" + " No Text defined"); return key; } result = result.Replace("\\n", "\n"); return result; } public static string GetLanguage() { return Language; } public static void SetLanguage(string language) { Language = language; } 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 != _language) { 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 파일을 읽어서 텍스트를 생성한다. /// private 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); } }