Example #1
0
        private string GetTranslation(string name, string context, int?count)
        {
            var key = CultureDictionaryRecord.GetKey(name, context);

            try
            {
                var translation = _dictionary[key, count];

                if (translation == null && _parentCultureDictionary != null)
                {
                    translation = _parentCultureDictionary[key, count]; // fallback to the parent culture
                }

                if (translation == null && context != null)
                {
                    translation = GetTranslation(name, null, count); // fallback to the translation without context
                }

                return(translation);
            }
            catch (PluralFormNotFoundException ex)
            {
                _logger.LogWarning(ex.Message);
            }

            return(null);
        }
        private string GetTranslation(string name, string context, CultureInfo culture, int?count)
        {
            var key = CultureDictionaryRecord.GetKey(name, context);

            try
            {
                var dictionary = _localizationManager.GetDictionary(culture);

                var translation = dictionary[key, count];

                if (translation == null && culture.Parent != null && culture.Parent != culture)
                {
                    dictionary = _localizationManager.GetDictionary(culture.Parent);

                    if (dictionary != null)
                    {
                        translation = dictionary[key, count]; // fallback to the parent culture
                    }
                }

                if (translation == null && context != null)
                {
                    translation = GetTranslation(name, null, culture, count); // fallback to the translation without context
                }

                return(translation);
            }
            catch (PluralFormNotFoundException ex)
            {
                _logger.LogWarning(ex.Message);
            }

            return(null);
        }
        public void MergeAddsRecordToEmptyDictionary()
        {
            var dictionary = new CultureDictionary("cs", _csPluralRule);
            var record     = new CultureDictionaryRecord("ball", "míč", "míče", "míčů");

            dictionary.MergeTranslations(new[] { record });

            Assert.Equal(dictionary.Translations[record.Key], record.Translations);
        }
Example #4
0
        public void IntexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntExist()
        {
            var dictionary = new CultureDictionary("cs", _csPluralRule);
            var record     = new CultureDictionaryRecord("ball", null, new[] { "míč", "míče" });

            dictionary.MergeTranslations(new[] { record });

            Assert.Throws <PluralFormNotFoundException>(() => dictionary["ball", 5]);
        }
        public void MergeOverwritesTranslationsForSameKeys()
        {
            var dictionary = new CultureDictionary("cs", _csPluralRule);
            var record     = new CultureDictionaryRecord("ball", "míč", "míče", "míčů");
            var record2    = new CultureDictionaryRecord("ball", "balón", "balóny", "balónů");

            dictionary.MergeTranslations(new[] { record });
            dictionary.MergeTranslations(new[] { record2 });

            Assert.Equal(dictionary.Translations[record.Key], record2.Translations);
        }
Example #6
0
        public void GetDictionaryReturnsDictionaryWithTranslationsFromProvider()
        {
            var dictionaryRecord = new CultureDictionaryRecord("ball", "míč", "míče", "míčů");

            _translationProvider
            .Setup(o => o.LoadTranslations(It.Is <string>(culture => culture == "cs"), It.IsAny <CultureDictionary>()))
            .Callback <string, CultureDictionary>((culture, dictioanry) => dictioanry.MergeTranslations(new[] { dictionaryRecord }));
            var manager = new LocalizationManager(new[] { _pluralRuleProvider.Object }, _translationProvider.Object, _memoryCache);

            var dictionary = manager.GetDictionary(new CultureInfo("cs"));

            Assert.Equal(dictionary.Translations["ball"], dictionaryRecord.Translations);
        }
Example #7
0
        private string GetTranslation(string name, string context, CultureInfo culture, int?count)
        {
            string translation = null;

            try
            {
                if (_fallBackToParentCulture)
                {
                    do
                    {
                        if (ExtractTranslation() != null)
                        {
                            break;
                        }

                        culture = culture.Parent;
                    }while (culture != CultureInfo.InvariantCulture);
                }
                else
                {
                    ExtractTranslation();
                }

                string ExtractTranslation()
                {
                    var dictionary = _localizationManager.GetDictionary(culture);

                    if (dictionary != null)
                    {
                        // Extract translation with context
                        var key = CultureDictionaryRecord.GetKey(name, context);
                        translation = dictionary[key, count];

                        if (context != null && translation == null)
                        {
                            // Extract translation without context
                            key         = CultureDictionaryRecord.GetKey(name, null);
                            translation = dictionary[key, count];
                        }
                    }

                    return(translation);
                }
            }
            catch (PluralFormNotFoundException ex)
            {
                _logger.LogWarning(ex.Message);
            }

            return(translation);
        }
        public void IndexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntExist()
        {
            // Arrange
            var dictionary = new CultureDictionary("cs", _csPluralRule);
            var record     = new CultureDictionaryRecord("ball", "míč", "míče");

            dictionary.MergeTranslations(new[] { record });

            Assert.Throws <PluralFormNotFoundException>(() =>
            {
                var key = new CultureDictionaryRecordKey("ball");

                return(dictionary[key, 5]);
            });
        }
Example #9
0
            public CultureDictionaryRecord BuildRecordAndReset()
            {
                if (!IsValid)
                {
                    return(null);
                }

                var result = new CultureDictionaryRecord(MessageId, MessageContext, _validValues.ToArray());

                MessageId      = null;
                MessageContext = null;
                _values.Clear();

                return(result);
            }
Example #10
0
        public void IndexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntExist()
        {
            // Arrange
            var dictionary = new CultureDictionary("cs", _csPluralRule);
            var record     = new CultureDictionaryRecord("ball", "míč", "míče");

            dictionary.MergeTranslations(new[] { record });

            // Act & Assert
            var exception = Assert.Throws <PluralFormNotFoundException>(() => dictionary["ball", 5]);

            Assert.Equal("ball", exception.Form.Key);
            Assert.Equal(_csPluralRule(5), exception.Form.Form);
            Assert.Equal("cs", exception.Form.Culture.Name);
        }