/// <summary> /// Initializes a new instance of the <see cref="LocalizationFetcher"/> class. /// </summary> /// <param name="system">The system.</param> /// <param name="locale">The locale.</param> /// <param name="prependedLocalizationKey">The prepended localization key.</param> /// <exception cref="System.ArgumentException">The locale [{locale}] is not a valid locale for the LocalizationSystem!</exception> public LocalizationFetcher(LocalizationSystem system, string locale, string prependedLocalizationKey = "") { LocalizationSystem = system; Locale = locale; PrependedLocalizationKey = prependedLocalizationKey; if (!system.HasLocale(locale)) { throw new ArgumentException($"The locale [{locale}] is not a valid locale for the LocalizationSystem!"); } }
/// <summary> /// Gets a localization. /// </summary> /// <param name="key">The key.</param> /// <returns>The localization.</returns> internal string GetLocalization(string key) { if (_cache.TryGetValue(key, out var value)) { return(value); } if (MachineTranslationEnabled) { var defaultLocaleLocalization = LocalizationSystem.GetLocalization(key, DefaultLocale); var translation = LocalizationSystem.Apertium.Translate(defaultLocaleLocalization); _cache.Add(key, translation); return(translation); } return(LocalizationSystem.GetLocalization(key, DefaultLocale)); }
/// <summary> /// Initializes a new instance of the <see cref="Localization" /> class. /// </summary> /// <param name="system">The system.</param> /// <param name="locale">The locale.</param> /// <param name="defaultLocale">The default locale.</param> /// <param name="enableMachineTranslation">Whether to enable machine translations.</param> internal Localization(LocalizationSystem system, string locale, string defaultLocale = null, bool enableMachineTranslation = false) { LocalizationSystem = system; Locale = locale; DefaultLocale = defaultLocale; MachineTranslationEnabled = enableMachineTranslation; _cache = new Dictionary <string, string>(); if (enableMachineTranslation) { // validate that the language pairs are valid! if (!LocalizationSystem.Apertium.IsValidPair(locale, defaultLocale)) { throw new Exception($"Invalid language pair! Valid Pairs: {system.Apertium.GetValidPairsString()}"); } } }
/// <summary> /// Gets the localization. /// </summary> /// <param name="key">The key.</param> /// <returns>The localized text.</returns> public string GetLocalization(string key) { key = $"{PrependedLocalizationKey}{key}"; return(LocalizationSystem.GetLocalization(key, Locale)); }