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);
        }
Esempio n. 2
0
        /// <inheritdocs />
        public void LoadTranslations([JetBrains.Annotations.NotNull] CultureInfo cultureInfo, [JetBrains.Annotations.NotNull] CultureDictionary dictionary)
        {
            if (cultureInfo == null)
            {
                throw new ArgumentNullException(nameof(cultureInfo));
            }
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            var fileInfos   = new List <IFileInfo>();
            var cultureName = cultureInfo.Name;

            fileInfos.AddRange(_poFilesLocationProvider.GetLocations(cultureName));

            foreach (var fileInfo in fileInfos.Where(fileInfo => !fileInfo.IsDirectory))
            {
                if (fileInfo.Exists)
                {
                    using var stream = fileInfo.CreateReadStream();
                    using var reader = new StreamReader(stream);
                    dictionary.MergeTranslations(_parser.Parse(reader));

                    _logger?.LogDebug($"Translations for culture found: {cultureName}. Translations available: {dictionary.Translations.Count}. Path: {fileInfo.PhysicalPath}.");
                    break;
                }

                _logger?.LogWarning($"Translation for culture was not found: {cultureName}. Path: {fileInfo.PhysicalPath}.");
            }
        }
        private void SetupDictionary(string cultureName, IEnumerable <CultureDictionaryRecord> records, PluralizationRuleDelegate pluralRule)
        {
            var dictionary = new CultureDictionary(cultureName, pluralRule);

            dictionary.MergeTranslations(records);

            _localizationManager.Setup(o => o.GetDictionary(It.Is <CultureInfo>(c => c.Name == cultureName))).Returns(dictionary);
        }
        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);
        }
Esempio n. 5
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]);
        }
 private void LoadFileToDictionary(string path, CultureDictionary dictionary)
 {
     if (File.Exists(path))
     {
         using (var stream = File.OpenRead(path))
         {
             using (var reader = new StreamReader(stream))
             {
                 dictionary.MergeTranslations(_parser.Parse(reader));
             }
         }
     }
 }
 private void LoadFileToDictionary(IFileInfo fileInfo, CultureDictionary dictionary)
 {
     if (fileInfo.Exists && !fileInfo.IsDirectory)
     {
         using (var stream = fileInfo.CreateReadStream())
         {
             using (var reader = new StreamReader(stream))
             {
                 dictionary.MergeTranslations(_parser.Parse(reader));
             }
         }
     }
 }
        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]);
            });
        }
Esempio n. 9
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);
        }
        public void EnumerateCultureDictionary()
        {
            // Arrange
            var dictionary = new CultureDictionary("ar", _arPluralRule);

            dictionary.MergeTranslations(new List <CultureDictionaryRecord>
            {
                new CultureDictionaryRecord("Hello", "مرحبا"),
                new CultureDictionaryRecord("Bye", "مع السلامة")
            });

            // Act & Assert
            Assert.NotEmpty(dictionary);

            foreach (var record in dictionary)
            {
                Assert.Single(record.Translations);
            }

            Assert.Equal(2, dictionary.Count());
        }