Skip to content

Instantly share code, notes, and snippets.

@dron247
Created April 5, 2017 07:23
Show Gist options
  • Save dron247/35976ea7f6dafec9bb174194ee186dab to your computer and use it in GitHub Desktop.
Save dron247/35976ea7f6dafec9bb174194ee186dab to your computer and use it in GitHub Desktop.
Unity localization example
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class LocalizedText : MonoBehaviour {
// Use this for initialization
void Awake() {
var stringId = gameObject.name;
var language = Application.systemLanguage;
var message = Strings.GetLocalized(stringId, language);
GetComponent<Text>().text = message;
}
}
using System.Collections.Generic;
using UnityEngine;
using RegistryDictionary = System.Collections.Generic.Dictionary<string, System.Collections.Generic.IList<string>>;
using LocalizationsRegistry = System.Collections.Generic.Dictionary
<UnityEngine.SystemLanguage, System.Collections.Generic.Dictionary<string, System.Collections.Generic.IList<string>>
>;
using Random = System.Random;
public class Strings {
private const int SupportedLanguagesCount = 5;
private const int PhrasesCount = 10;
private const string Placeholder = "placeholder";
private const SystemLanguage DefaultLanguage = SystemLanguage.Russian;
private static readonly Random Randomizer = new Random();
private static LocalizationsRegistry _stringsRegistry;
private Strings() {
// instance is forbidden
}
private static LocalizationsRegistry StringsRegistry {
// lazy init of dictionaries
get { return _stringsRegistry ?? (_stringsRegistry = CreateLocalizationsRegistry(SupportedLanguagesCount)); }
}
/// <summary>
/// Localized string by string key
/// </summary>
/// <param name="stringId">Key of string</param>
/// <param name="lang">Locale you want to get</param>
/// <param name="notFound">Tect which will be returned if nothing found</param>
/// <returns></returns>
public static string GetLocalized(string stringId, SystemLanguage lang, string notFound = null) {
// check lang is supported
if (StringsRegistry.ContainsKey(lang)) {
var dict = StringsRegistry[lang];
if (!dict.ContainsKey(stringId)) // check contains value
return lang == DefaultLanguage ? notFound ?? Placeholder : GetLocalized(stringId, DefaultLanguage);
var variants = dict[stringId];
// check value
switch (variants.Count) {
case 0: // empty entry
return notFound ?? Placeholder;
case 1: // only one value
return variants[0];
default: // multiple values, should select one randomly
return variants[Randomizer.Next(0, variants.Count)];
}
}
return lang == DefaultLanguage ? notFound ?? Placeholder : GetLocalized(stringId, DefaultLanguage);
}
#region Loacalization init
private static LocalizationsRegistry CreateLocalizationsRegistry(int capacity) {
return new LocalizationsRegistry(capacity) {
{SystemLanguage.Russian, CreateDictionaryRussian()} // Russian support
};
}
// This should be generated but YAGNI and stuff
private static RegistryDictionary CreateDictionaryRussian() {
var localizedDictionary = new RegistryDictionary(PhrasesCount);
// подсказки с рандомом
var hints = new List<string> {
"message 1",
"message 2",
"message 3",
"message 4",
"message 5",
"message 6",
"message 7"
};
localizedDictionary.Add("Helper", hints);
// первая часть текста с экрана инфы в мэйн меню
var mainMenuInfo1 = new List<string> {
"info 1"
};
localizedDictionary.Add("MainMenuInfo1", mainMenuInfo1);
// вторая часть текста с экрана инфы в мэйн меню
var mainMenuInfo2 = new List<string> {
"info 2"
};
localizedDictionary.Add("MainMenuInfo2", mainMenuInfo2);
return localizedDictionary;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment