Create a gist now

Instantly share code, notes, and snippets.

@KVinS /Local.cs
Last active Jun 16, 2017

What would you like to do?
Unity3d localization script
using UnityEngine;
using System.Xml;
using System.IO;
using System;
using System.Text;
using System.Collections.Generic;
public class Local
{
private static Local instance;
private static string defLocale = "en";
private Dictionary<string, string> d_text = new Dictionary<string, string>();
private Local()
{
instance = this;
initLocal();
}
public static Local getInstance()
{
if (instance == null)
{
new Local();
}
return instance;
}
private void initLocal(string language)
{
TextAsset textAsset = (TextAsset)Resources.Load("Localization/strings-" + language);
if (textAsset == null)
{
textAsset = (TextAsset)Resources.Load("Localization/strings-" + defLocale);
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(textAsset.text);
XmlElement myElement = xmldoc.DocumentElement;
XmlNodeList date = myElement.GetElementsByTagName("string");
d_text.Clear();
for (int i = 0; i < date.Count; i++)
{
d_text.Add(date[i].Attributes[0].InnerText.ToLower(), date[i].InnerText);
}
}
private void initLocal()
{
initLocal(getSystemLanguage());
}
private string getSystemLanguage()
{
string local = "";
string lang = Application.systemLanguage.ToString();
if (lang.Equals("English"))
{
local = "en";
}
else if (lang.Equals("Russian"))
{
local = "ru";
}
else if (lang.Equals("Ukrainian"))
{
local = "ru";
}
else if (lang.Equals("Belarusian"))
{
local = "ru";
}
else if (lang.Equals("German"))
{
local = "de";
}
else if (lang.Equals("Spanish"))
{
local = "es";
}
else if (lang.Equals("French"))
{
local = "fr";
}
else if (lang.Equals("Portuguese"))
{
local = "pt";
}
else if (lang.Equals("Finnish"))
{
local = "fi";
}
else if (lang.Equals("Italian"))
{
local = "it";
}
else if (lang.Equals("Thai"))
{
local = "th";
}
else if (lang.Equals("Japanese"))
{
local = "jp";
}
else if (lang.Equals("Korean"))
{
local = "kr";
}
else
{
local = defLocale;
}
return local;
}
public void generateKeys()
{
initLocal("ru");
string path = Directory.GetCurrentDirectory() + "/Assets/Localization/LocalKeys.cs";
Debug.Log(path);
if (File.Exists(path))
{
File.Delete(path);
}
string textFile = "public enum Keys{";
foreach (string key in d_text.Keys)
{
Debug.Log("Added key: " + key);
textFile += key + ", ";
}
textFile = textFile.Substring(0, textFile.Length - 2);
textFile += "}";
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes(textFile);
fs.Write(info, 0, info.Length);
}
Debug.Log("generate Keys finish");
}
public string getText(string key)
{
string result = "";
try
{
result = d_text[key.ToLower()];
}
catch (Exception e)
{
Debug.LogWarning("not found this key - " + key.ToLower());
}
return result;
}
public string getText(Keys key)
{
return getText(key.ToString());
}
}
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(LocalizationUI))]
public class LocalizationEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
LocalizationUI myScript = (LocalizationUI)target;
if (GUILayout.Button("Generate Keys"))
{
myScript.generateKeys();
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LocalizationUI : MonoBehaviour
{
public GameObject[] goArray;
void Start()
{
foreach (GameObject ui in goArray)
{
if (ui != null)
{
if (ui.GetComponent<TextController>() != null)
{
ui.GetComponent<TextController>().SetText(Local.getInstance().getText(ui.name));
} else {
if (ui.GetComponent<Text>() != null)
{
ui.GetComponent<Text>().text = Local.getInstance().getText(ui.name);
}
else
{
//ui.GetComponentInChildren<Text>().text = Local.getInstance().getText(ui.name);
}
if (ui.GetComponent<TextMesh>() != null)
{
ui.GetComponent<TextMesh>().text = Local.getInstance().getText(ui.name);
}
else
{
//ui.GetComponentInChildren<Text>().text = Local.getInstance().getText(ui.name);
}
}
}
}
}
public void generateKeys()
{
Local.getInstance().generateKeys();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment