コード例 #1
0
        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);
        }
コード例 #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}.");
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #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]);
        }
コード例 #6
0
 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));
             }
         }
     }
 }
コード例 #7
0
 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));
             }
         }
     }
 }
コード例 #8
0
        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]);
            });
        }
コード例 #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);
        }
コード例 #10
0
        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());
        }