Last active
July 17, 2023 20:51
-
-
Save KVinS/ff108fbc187af6f94510 to your computer and use it in GitHub Desktop.
Unity3d localization script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine; | |
using System.Xml; | |
using System.IO; | |
using System; | |
using System.Text; | |
using System.Collections.Generic; | |
public class Local | |
{ | |
public static Local Instance | |
{ | |
get | |
{ | |
if (s_instance == null) | |
{ | |
new Local(); | |
} | |
return s_instance; | |
} | |
} | |
private const string DefLocale = "en"; | |
private static Local s_instance; | |
private readonly Dictionary<string, string> _localizationCash = new Dictionary<string, string>(); | |
private Local() | |
{ | |
s_instance = this; | |
InitLocal(); | |
} | |
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"); | |
_localizationCash.Clear(); | |
for (int i = 0; i < date.Count; i++) | |
{ | |
_localizationCash.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(); | |
switch (lang) | |
{ | |
case "English": | |
local = "en"; | |
break; | |
case "Russian": | |
case "Ukrainian": | |
case "Belarusian": | |
local = "ru"; | |
break; | |
case "German": | |
local = "de"; | |
break; | |
case "Spanish": | |
local = "es"; | |
break; | |
case "French": | |
local = "fr"; | |
break; | |
case "Portuguese": | |
local = "pt"; | |
break; | |
case "Finnish": | |
local = "fi"; | |
break; | |
case "Italian": | |
local = "it"; | |
break; | |
case "Thai": | |
local = "th"; | |
break; | |
case "Japanese": | |
local = "jp"; | |
break; | |
case "Korean": | |
local = "kr"; | |
break; | |
default: | |
local = DefLocale; | |
break; | |
} | |
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 _localizationCash.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 = _localizationCash[key.ToLower()]; | |
} | |
catch (Exception e) | |
{ | |
Debug.LogWarning("not found this key - " + key.ToLower()); | |
} | |
return result; | |
} | |
public string GetText(Keys key) | |
{ | |
return GetText(key.ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor(typeof(LocalizationUI))] | |
public class LocalizationEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
LocalizationUI localizationUi = (LocalizationUI)target; | |
if (GUILayout.Button("Generate Keys")) | |
{ | |
localizationUi.GenerateKeys(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
public class LocalizationUI : MonoBehaviour | |
{ | |
[SerializeField] private GameObject[] _textArray; | |
void Start() | |
{ | |
foreach (GameObject ui in _textArray) | |
{ | |
if (ui != null) | |
{ | |
if (ui.GetComponent<Text>() != null) | |
{ | |
ui.GetComponent<Text>().text = Local.Instance.GetText(ui.name); | |
} | |
else | |
{ | |
//ui.GetComponentInChildren<Text>().text = Local.getInstance().getText(ui.name); | |
} | |
if (ui.GetComponent<TextMesh>() != null) | |
{ | |
ui.GetComponent<TextMesh>().text = Local.Instance.GetText(ui.name); | |
} | |
else | |
{ | |
//ui.GetComponentInChildren<Text>().text = Local.getInstance().getText(ui.name); | |
} | |
} | |
} | |
} | |
public void GenerateKeys() | |
{ | |
Local.Instance.GenerateKeys(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment