add project files
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using OHM.UnityToolkit;
|
||||
|
||||
namespace OHM.UnityToolkit.Localization
|
||||
{
|
||||
[CreateAssetMenu(fileName = "locale_def", menuName = "OHM/Localization/New locale def")]
|
||||
public class LocaleDef : ScriptableObject
|
||||
{
|
||||
public SystemLanguage language;
|
||||
|
||||
public string languageCode;
|
||||
|
||||
public KeysDic keys;
|
||||
|
||||
public void Test()
|
||||
{
|
||||
LocalizationManager.Instance.TestLocale(this);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class KeysDic : SerializableDictionary<string, string>
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f6c6b3ccd7030f728464122b207409c0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OHM.UnityToolkit.Localization
|
||||
{
|
||||
[CreateAssetMenu(fileName = "locale_list", menuName = "OHM/Localization/New locales list")]
|
||||
public class LocalesList : ScriptableObject
|
||||
{
|
||||
public List<LocaleDef> locales;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 051df8610b6a0ef645af7b1dfa7323d3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using OHM.UnityToolkit;
|
||||
|
||||
namespace OHM.UnityToolkit.Localization
|
||||
{
|
||||
public class LocalizationManager : UniqueInstance<LocalizationManager>
|
||||
{
|
||||
public LocalesList localesList;
|
||||
|
||||
public bool autoSetLang;
|
||||
|
||||
public UnityEvent OnLangChanged;
|
||||
|
||||
private bool _showDebug;
|
||||
|
||||
public LocaleDef CurrentLocale { get; private set; }
|
||||
|
||||
protected override void AwakeInstance()
|
||||
{
|
||||
if (autoSetLang)
|
||||
{
|
||||
SetLanguage(Application.systemLanguage);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLanguage(SystemLanguage lang)
|
||||
{
|
||||
CurrentLocale = null;
|
||||
if (localesList == null || localesList.locales == null || localesList.locales.Count < 1)
|
||||
{
|
||||
Debug.LogError("No locale defined");
|
||||
return;
|
||||
}
|
||||
if (lang == SystemLanguage.Unknown)
|
||||
{
|
||||
lang = Application.systemLanguage;
|
||||
}
|
||||
foreach (LocaleDef locale in localesList.locales)
|
||||
{
|
||||
if (locale.language == lang)
|
||||
{
|
||||
CurrentLocale = locale;
|
||||
}
|
||||
}
|
||||
if (CurrentLocale == null)
|
||||
{
|
||||
CurrentLocale = localesList.locales[0];
|
||||
Debug.Log("No specific locale found for language" + lang + ". Using default : " + CurrentLocale.language);
|
||||
}
|
||||
OnLangChanged.Invoke();
|
||||
}
|
||||
|
||||
public void TestLocale(LocaleDef locale)
|
||||
{
|
||||
CurrentLocale = locale;
|
||||
OnLangChanged.Invoke();
|
||||
}
|
||||
|
||||
public string Localize(string key)
|
||||
{
|
||||
return LocalizationUtils.Localize(CurrentLocale, key);
|
||||
}
|
||||
|
||||
public string LocalizeFormat(string key, object[] args)
|
||||
{
|
||||
string localized = LocalizationUtils.Localize(CurrentLocale, key);
|
||||
try
|
||||
{
|
||||
localized = string.Format(localized, args);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
Debug.LogWarningFormat("Locale key {0} does not match the required format", key);
|
||||
}
|
||||
return localized;
|
||||
}
|
||||
|
||||
public void DrawDebug()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f93e818a9e9afd02bfae28ab1a7cacd5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace OHM.UnityToolkit.Localization
|
||||
{
|
||||
public static class LocalizationUtils
|
||||
{
|
||||
public static string Localize(LocaleDef locale, string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
if (locale == null)
|
||||
{
|
||||
return "#####";
|
||||
}
|
||||
string value;
|
||||
if (locale.keys != null && locale.keys.TryGetValue(key, out value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return "#" + key + "#";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7346a71832c8bf33a4431c08bd186c3e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OHM.UnityToolkit.Localization
|
||||
{
|
||||
public class LocalizedText : MonoBehaviour
|
||||
{
|
||||
public string key;
|
||||
|
||||
public TextWrapper text;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
text = GetComponent<TextWrapper>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
ApplyLocalization();
|
||||
LocalizationManager.Instance.OnLangChanged.AddListener(ApplyLocalization);
|
||||
}
|
||||
|
||||
public void ChangeKey(string key)
|
||||
{
|
||||
this.key = key;
|
||||
ApplyLocalization();
|
||||
}
|
||||
|
||||
public void ApplyLocalization()
|
||||
{
|
||||
SetText(string.IsNullOrEmpty(key)
|
||||
? string.Empty
|
||||
: LocalizationUtils.Localize(LocalizationManager.Instance.CurrentLocale, key));
|
||||
}
|
||||
|
||||
private void SetText(string content)
|
||||
{
|
||||
if (text != null)
|
||||
{
|
||||
text.text = content;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff6de81a7eca14b7e2b51b3bb4f69d8f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user