You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 lines
3.5 KiB

using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Data;
public class LocalizationText
{
private static IDictionary<string, string> _content = new Dictionary<string, string>();
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<string, string> 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;
}
}
}
/// <summary>
/// 로컬에 존재하는 xml 파일을 읽어서 텍스트를 생성한다.
/// </summary>
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);
}
/// <summary>
/// 서버에서 받은 xml 파일을 읽어서 텍스트를 생성한다.
/// </summary>
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);
}
}