コード例 #1
0
        /// <summary>
        /// Set any translation from 'Translations' property
        /// </summary>
        /// <param name="translation"></param>
        public static void SetCulture(TranslationInfo translation)
        {
            if (!isInit)
            {
                throw new NotInitializedException(typeof(LocalizationManager));
            }

            if (!Translations.Contains(translation))
            {
                throw new CultureNotFoundException($"Invalid translation: '{translation.TranslationName}', with culture: '{translation.CultureInfo.Name}'");
            }

            if (CurrentTranslation == translation)
            {
                Debug.LogWarning("You are trying to switch to the current culture.");
                return;
            }

            CurrentTranslation = translation;
            IsDefaultCulture   = false;

            PluralForm = PluralFormGenerator.CreateForm(CurrentTranslation.CultureInfo);

            var translationDictionary = translationDatabase.GetTranslationDictionary(CurrentTranslation);

            LocalizationCache.Init(translationDictionary);

            OnCultureChange?.Invoke();
        }
コード例 #2
0
        /// <summary>
        /// Returns translated into the selected language.
        /// </summary>
        /// <returns>Translated strings.</returns>
        public virtual string GetString()
        {
            if (LocalizationManager.IsDefaultCulture)
            {
                return(this._defaultStrings[0]);
            }

            string[] translations = LocalizationCache.GetStringsById(this._id);

            return(translations?[0] ?? this._defaultStrings[0]);
        }
コード例 #3
0
        /// <summary>
        /// Returns the plural form for <paramref name="n"/> of the translation.
        /// </summary>
        /// <param name="n">Value that determines the plural form.</param>
        /// <returns>Translated strings.</returns>
        public virtual string GetPString(int n)
        {
            int pluralIndex = LocalizationManager.PluralForm.EvaluatePlural(n);

            if (pluralIndex < 0 || pluralIndex >= LocalizationManager.PluralForm.PluralsCount)
            {
                throw new IndexOutOfRangeException(
                          $"Calculated plural form index ({pluralIndex}) is out of allowed range (0~{LocalizationManager.PluralForm.PluralsCount - 1}).");
            }

            if (LocalizationManager.IsDefaultCulture)
            {
                return(this._defaultStrings[pluralIndex]);
            }

            string[] translations = LocalizationCache.GetStringsById(this._id);

            if (translations == null || translations.Length <= pluralIndex)
            {
                return((n == 1) ? this._defaultStrings[0] : this._defaultStrings[1]);
            }

            return(translations[pluralIndex]);
        }