예제 #1
0
        public void AddTranslation(string language, string @namespace, string key, string value)
        {
            if (string.IsNullOrWhiteSpace(language))
            {
                throw new ArgumentException("Language cannot be null, empty or whitespace string.", nameof(language));
            }
            if (string.IsNullOrWhiteSpace(@namespace))
            {
                throw new ArgumentException("Namespace cannot be null, empty or whitespace string.", nameof(@namespace));
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Key cannot be null, empty or whitespace string.", nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var treeKey = language + "_" + @namespace;

            if (!_namespaces.TryGetValue(treeKey, out var nsDict))
            {
                nsDict = new DictionaryTranslationTree(@namespace);
                _namespaces.Add(treeKey, nsDict);
            }

            nsDict[key] = value;
        }
예제 #2
0
        /// <inheritdoc />
        public Task <ITranslationTree> LoadNamespaceAsync(string language, string @namespace)
        {
            var translationTree = new DictionaryTranslationTree(@namespace);

            var path = FindFile(language, @namespace);

            if (path == null)
            {
                return(null);
            }

            using (Stream moFileStream = File.OpenRead(path))
            {
                var catalog = new Catalog(moFileStream, CultureInfo.GetCultureInfo(language));

                foreach (var translation in catalog.Translations)
                {
                    for (var i = 0; i < translation.Value.Length; i++)
                    {
                        var key          = translation.Key;
                        var value        = translation.Value[i];
                        var pluralNumber = i + 1;

                        if (UseSimplePluralSuffix)
                        {
                            if (i == 1)
                            {
                                key += PluralSeparator + "plural";
                            }
                            else if (i > 1)
                            {
                                key += PluralSeparator + pluralNumber;
                            }
                        }
                        else
                        {
                            key += PluralSeparator + pluralNumber;
                        }

                        translationTree.AddValue(key, value);
                    }
                }
            }

            return(Task.FromResult((ITranslationTree)translationTree));
        }