public struct Translation { public SystemLanguage key; public string value; public Translation(SystemLanguage key, string value) { this.key = key; this.value = value; } }
[System.Serializable] public class Label{ [SerializeField] int _id; [SerializeField] List<Translation> translations = new List<Translation>(); public int id { get { return _id; } private set { _id = value; } } public Label(int id) { this.id = id; } }
public string Get(SystemLanguage language) { for (int i = 0; i < translations.Count; i++) { if (translations[i].key == language) { return translations[i].value; } } translations.Add(new Translation(language, string.Empty)); return translations[translations.Count - 1].value; } public void Set(SystemLanguage language, string str) { for (int i = 0; i < translations.Count; i++) { if (translations[i].key == language){ translations[i] = new Translation(language, str); return; } } translations.Add(new Translation(language, str)); }
[CreateAssetMenu(fileName="LabelsData", menuName="SimpleLocalizator/LabelsData")] public class LabelsData : ScriptableObject { [SerializeField] List<Label> _labels=new List<Label>(); Label _defaultLabel = new Label (-1, "not translated"); public static Label defaultLabel { get { return instance._defaultLabel; } } public static List<Label> labels { get { return instance._labels; } private set { instance._labels = value; } } }
static LabelsData _instance; public static LabelsData instance { get { if (_instance==null) { _instance = (LabelsData)Resources.Load ("LabelsData"); if (_instance == null) { _instance = CreateInstance<LabelsData> (); } } return _instance; } }
public static class LanguageManager{ static SystemLanguage _currentLanguage = SystemLanguage.English; public static SystemLanguage currentLanguage { get { return _currentLanguage; } set { _currentLanguage = value; if (onLanguageChanged != null) onLanguageChanged (); } } public static Action onLanguageChanged; }
public static bool autoDetectLanguage=true; private static bool init = false; static void Init() { if (!init) { init = true; if (autoDetectLanguage) { currentLanguage = Application.systemLanguage; } else { currentLanguage = currentLanguage; } Debug.Log("LanguageManager: initialized. Current language: " + currentLanguage); } }
public static string GetString(int labelID) { return GetString(labelID, currentLanguage); } public static string GetString(int labelID, SystemLanguage language) { Init(); for (int i = 0; i < LabelsData.labels.Count; i++) { if (LabelsData.labels[i].id == labelID) { return LabelsData.labels[i].Get(language); } } return LabelsData.defaultLabel.Get(language); }
public abstract class MultiLanguageComponent : MonoBehaviour { [SerializeField] SystemLanguage _currentLanguage = SystemLanguage.English; protected SystemLanguage currentLanguage { get { return _currentLanguage; } set { _currentLanguage = value; Refresh (); } } public void OnValidate() { currentLanguage = _currentLanguage; } void OnEnable() { OnLanguageRefresh (); LanguageManager.onLanguageChanged += OnLanguageRefresh; } void OnDisable() { LanguageManager.onLanguageChanged -= OnLanguageRefresh; } void OnLanguageRefresh() { currentLanguage = LanguageManager.currentLanguage; } protected virtual void Refresh() { } }
public abstract class MultiLanguageTextBase : MultiLanguageComponent{ [SerializeField] int _labelID; [SerializeField] bool toUpper=false; public int labelID { get { return _labelID; } set { _labelID = value; Refresh(); } } }
protected override void Refresh() { bool local = (Application.isEditor && !Application.isPlaying); string str = local ? LanguageManager.GetString(labelID, currentLanguage) : LanguageManager.GetString(labelID); if (toUpper) str = str.ToUpper(); VisualizeString(str); } protected abstract void VisualizeString(string str);
[RequireComponent(typeof(Text))] public class MultiLanguageTextUI : MultiLanguageTextBase { Text _text; public Text text { get { if (_text == null && gameObject!=null) _text = GetComponent<Text> (); return _text; } } protected override void VisualizeString(string str) { if (text != null) text.text = str; } }
using UnityEngine; namespace SimpleLocalizator { [System.Serializable] public struct Translation { public SystemLanguage key; public string value; public Translation(SystemLanguage key, string value) { this.key = key; this.value = value; } } }
using UnityEngine; using System.Collections.Generic; namespace SimpleLocalizator { [System.Serializable] public class Label{ #region Data [SerializeField] int _id; [SerializeField] List<Translation> translations = new List<Translation>(); private const string defaultText = "not translated"; #endregion #region Interface public int id { get { return _id; } private set { _id = value; } } public Label(int id) { this.id = id; } public string Get(SystemLanguage language) { for (int i = 0; i < translations.Count; i++) { if (translations[i].key == language) { return translations[i].value; } } translations.Add(new Translation(language, defaultText)); return translations[translations.Count - 1].value; } public void Set(SystemLanguage language, string str) { for (int i = 0; i < translations.Count; i++) { if (translations[i].key == language){ translations[i] = new Translation(language, str); return; } } translations.Add(new Translation(language, str)); } #endregion } }
using System.Collections.Generic; using UnityEngine; using System.Text; namespace SimpleLocalizator { [CreateAssetMenu(fileName="LabelsData", menuName="SimpleLocalizator/LabelsData")] public class LabelsData : ScriptableObject { [SerializeField] List<Label> _labels=new List<Label>(); Label _defaultLabel = new Label (-1); public static Label defaultLabel { get { return instance._defaultLabel; } } public static List<Label> labels { get { return instance._labels; } private set { instance._labels = value; } } static LabelsData _instance; public static LabelsData instance { get { if (_instance==null) { _instance = (LabelsData)Resources.Load ("LabelsData"); if (_instance == null) { _instance = CreateInstance<LabelsData> (); Debug.Log ("LabelsData: loaded instance from resources is null, created instance"); } } return _instance; } } } }
using UnityEngine; using System; namespace SimpleLocalizator { public static class LanguageManager{ #region Data public static bool autoDetectLanguage=true; static SystemLanguage _currentLanguage = SystemLanguage.English; private static bool init = false; #endregion #region Interface public static SystemLanguage currentLanguage { get { return _currentLanguage; } set { _currentLanguage = value; if (onLanguageChanged != null) onLanguageChanged (); } } public static Action onLanguageChanged; public static string GetString(int labelID) { return GetString(labelID, currentLanguage); } public static string GetString(int labelID, SystemLanguage language) { Init(); for (int i = 0; i < LabelsData.labels.Count; i++) { if (LabelsData.labels[i].id == labelID) { return LabelsData.labels[i].Get(language); } } return LabelsData.defaultLabel.Get(language); } #endregion #region Methods static void Init() { if (!init) { init = true; if (autoDetectLanguage){ currentLanguage = Application.systemLanguage; } else { currentLanguage = currentLanguage; } Debug.Log("LanguageManager: initialized. Current language: " + currentLanguage); } } #endregion } }
using UnityEngine; namespace SimpleLocalizator { public abstract class MultiLanguageComponent : MonoBehaviour { [SerializeField] SystemLanguage _currentLanguage = SystemLanguage.English; protected SystemLanguage currentLanguage { get { return _currentLanguage; } set { _currentLanguage = value; Refresh (); } } public void OnValidate() { currentLanguage = _currentLanguage; } void OnEnable() { OnLanguageRefresh (); LanguageManager.onLanguageChanged += OnLanguageRefresh; } void OnDisable() { LanguageManager.onLanguageChanged -= OnLanguageRefresh; } void OnLanguageRefresh() { currentLanguage = LanguageManager.currentLanguage; } protected virtual void Refresh() { } } }
using UnityEngine; namespace SimpleLocalizator { public abstract class MultiLanguageTextBase : MultiLanguageComponent{ #region Unity scene settings [SerializeField] int _labelID; [SerializeField] bool toUpper=false; #endregion #region Interface public int labelID { get { return _labelID; } set { _labelID = value; Refresh(); } } #endregion #region Methods protected override void Refresh() { bool local = (Application.isEditor && !Application.isPlaying); string str = local ? LanguageManager.GetString(labelID, currentLanguage) : LanguageManager.GetString(labelID); if (toUpper) str = str.ToUpper(); VisualizeString(str); } protected abstract void VisualizeString(string str); #endregion } }
using UnityEngine; using UnityEngine.UI; namespace SimpleLocalizator { [RequireComponent(typeof(Text))] public class MultiLanguageTextUI : MultiLanguageTextBase { Text _text; public Text text { get { if (_text == null && gameObject!=null) _text = GetComponent<Text> (); return _text; } } protected override void VisualizeString(string str) { if (text != null) text.text = str; } } }
Source: https://habr.com/ru/post/341744/
All Articles