using System; using UnityEngine; using UnityEngine.Events; using OHM.UnityToolkit; namespace OHM.UnityToolkit.Localization { public class LocalizationManager : UniqueInstance { 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() { } } }