namespace UIWidgets.l10n { using System; using System.Globalization; using UnityEngine; /// /// Locale changed event handler. /// public delegate void LocaleChangedEventHandler(); /// /// Localization class. /// public static class Localization { /// /// Locale changed event. /// public static event LocaleChangedEventHandler OnLocaleChanged = DoNothing; /// /// Translate the specified string. /// public static Func GetTranslation; /// /// Get country code. /// Used to instantiate System.Globalization.CultureInfo. /// /// Country code. public static Func GetCountryCode; static CultureInfo сulture; /// /// Get culture info (based on country code). /// public static CultureInfo Culture { get { if (сulture == null) { var code = GetCountryCode(); сulture = GetCulture(code); } return сulture; } } /// /// Get culture. /// public static Func GetCulture = DefaultGetCulture; /// /// Default implementation of get culture. /// /// Country code. /// Culture. public static CultureInfo DefaultGetCulture(string code) { if (string.IsNullOrEmpty(code)) { return CultureInfo.InvariantCulture; } var new_code = string.Format("{0}-{1}", code, code.ToUpperInvariant()); try { var culture = new CultureInfo(code); if (culture.IsNeutralCulture) { return new CultureInfo(new_code); } } catch (NotSupportedException) { try { return new CultureInfo(new_code); } catch (NotSupportedException) { } } return CultureInfo.InvariantCulture; } #if I2_LOCALIZATION_SUPPORT /// /// Translate the input string using I2 Localization. /// /// String to translate. /// Translated string. public static string I2Translation(string input) { var result = I2.Loc.LocalizationManager.GetTranslation(input); return (result != null) ? result : input; } /// /// Get country code of the current locale using I2 Localization. /// /// Country code. public static string I2CountryCode() { return I2.Loc.LocalizationManager.CurrentLanguageCode; } #endif static Localization() { Init(); } #if UNITY_2019_3_OR_NEWER [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] #endif static void Init() { сulture = null; GetCulture = DefaultGetCulture; #if I2_LOCALIZATION_SUPPORT GetTranslation = I2Translation; GetCountryCode = I2CountryCode; I2.Loc.LocalizationManager.OnLocalizeEvent += LocaleChanged; #else GetTranslation = NoTranslation; GetCountryCode = DefaultCountryCode; #endif } /// /// Invoke locale changed event. /// public static void LocaleChanged() { сulture = null; OnLocaleChanged.Invoke(); } /// /// Do nothing. /// static void DoNothing() { } /// /// Get default country code. /// Used to instantiate System.Globalization.CultureInfo. /// /// Country code. public static string DefaultCountryCode() { return null; } /// /// Return the input string without translation. /// /// String to translate. /// Translated string. public static string NoTranslation(string input) { return input; } } }