Skip to content

Instantly share code, notes, and snippets.

@takashi1975
Last active May 9, 2017 09:56
Show Gist options
  • Select an option

  • Save takashi1975/f84a6cc65aa508b6f58ff6c3f4a848a2 to your computer and use it in GitHub Desktop.

Select an option

Save takashi1975/f84a6cc65aa508b6f58ff6c3f4a848a2 to your computer and use it in GitHub Desktop.
Unity 多言語対応
<?xml version="1.0" encoding="utf-8"?>
<!-- Resourcesフォルダ以下に配置 -->
<languages>
<!-- 日本語 -->
<language name="Japanese">
<string name="app_name">アプリ名</string>
</language>
<!-- 英語(日本語以外) -->
<language name="English">
<string name="app_name">App name</string>
</language>
</languages>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
/**
* MEMO: 2017.04.19
* とりあえず 指定の書式のxml を読み込む
* エラー処理は基本考慮していない
*
* 定数 ... XMLMessageName.cs で定義
*/
public class XMLMessage : MonoBehaviour {
[SerializeField, Tooltip("Resourcesフォルダ以下のxmlファイル名(.xmlの指定なしでOKの様子)")]
private string filePath = null;
// 仕様に基づいたxmlから読み取った内容を保持
private Dictionary<string, Dictionary<string, string>> languages;
#if UNITY_EDITOR
[SerializeField]
private SystemLanguage testLanguage = SystemLanguage.Unknown;
#endif
// 定数の定義
private class keyTable {
/**
* XML
* languages
* +-language
* +-string (name)
*/
public const string language = "language";
public const string name = "name";
}
void Awake () {
//ファイルの読み込み
TextAsset xmlTextAsset = Instantiate (Resources.Load (this.filePath)) as TextAsset;
//Debug.Log (xmlTextAsset.text);
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (xmlTextAsset.text);
XmlNodeList nodes = xmlDoc.GetElementsByTagName (keyTable.language);
//Debug.Log (string.Format("nodes count : {0}", nodes.Count));
foreach (XmlNode node in nodes) {
// //language
// Debug.Log (string.Format("node Name : {0}, node.Attributes.Count : {1}", node.Name, node.Attributes.Count));
// Debug.Log (string.Format("node Name : {0}, node.ChildNodes.Count : {1}", node.Name, node.ChildNodes.Count));
//
// foreach (XmlAttribute attribute in node.Attributes) {
// Debug.Log(string.Format("[{0}] Attribute Name : {1}, Value : {2}", node.Name, attribute.Name, attribute.Value));
// }
//言語名
var language = node.Attributes.GetNamedItem (keyTable.name).Value;
//Debug.Log (string.Format("Language Name : {0}", language));
//登録内容 (まずは string を想定)
var values = new Dictionary<string, string>();
XmlNode child = node.FirstChild;
do {
var key = child.Attributes.GetNamedItem (keyTable.name).Value;
var value = child.FirstChild.Value;
//Debug.Log (string.Format("child Name:{0}, Key:{1}, Value:{2}", child.Name, key, value));
values.Add(key, value);
} while ((child = child.NextSibling) != null);
if (values.Count > 0) {
if (this.languages == null) {
this.languages = new Dictionary<string, Dictionary<string, string>>();
}
this.languages.Add (language, values);
}
}
}
// Use this for initialization
void Start () {
//使用例
Debug.Log (string.Format("Test 0 : {0}", this.getString(XMLMessageName.app_name)));
// //TEST
// Debug.Log (string.Format("Test 0 : {0}", this.languages["Japanese"]["app_name"]));
//
// Debug.Log (string.Format("Test 1 : {0}", this.getString("app_name")));
// Debug.Log (string.Format("Test 2 : {0}", this.getString("app_name", Application.systemLanguage)));
// Debug.Log (string.Format("Test 3 : {0}", this.getString("app_name", SystemLanguage.English)));
// Debug.Log (string.Format("Test 4 : {0}", this.getString("app_name", "Japanese")));
}
// Update is called once per frame
void Update () {
}
/**
* 文字列の取得 (システムの設定値から)
*/
public string getString(string key) {
#if UNITY_EDITOR
return this.getString (key, this.testLanguage);
#else
return this.getString (key, Application.systemLanguage);
#endif
}
/**
* 文字列の取得 (言語 SystemLanguage を指定)
*/
public string getString(string key, SystemLanguage language) {
var name = "";
if (language == SystemLanguage.Japanese) {
name = XMLMessageName.Language_Japanese;
} else {
name = XMLMessageName.Language_English;
}
return this.getString (key, name);
}
/**
* 文字列の取得
*/
public string getString(string key, string language) {
string value = "";
if (this.languages != null) {
//language
if (this.languages.ContainsKey (language)) {
var table = this.languages [language];
//string
if (table.ContainsKey (key)) {
//返り値
value = table [key];
}
}
}
return value;
}
}
/**
* 定数の定義
* message.xml で使用する名称を定義
*/
public class XMLMessageName
{
//言語名
public const string Language_Japanese = "Japanese";
public const string Language_English = "English";
//Key
public const string Key__TitleTweet = "app_name";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment