コード例 #1
0
        public async Task InitDbFromPoFiles(LocalizationDbContext localizationDbContext)
        {
            Logger.LogInformation("Importing PO files in db");

            var basePath = "Localization";

            IReadOnlyDictionary <string, POCatalog> TextCatalogs = new Dictionary <string, POCatalog>();

            var textCatalogFiles = _environment.ContentRootFileProvider.GetDirectoryContents(basePath)
                                   .Where(fi => !fi.IsDirectory && ".po".Equals(Path.GetExtension(fi.Name)))
                                   .ToArray();

            var textCatalogs = new List <(string FileName, string Culture, POCatalog Catalog)>();

            var parserSettings = new POParserSettings
            {
                SkipComments    = true,
                SkipInfoHeaders = true,
            };

            Parallel.ForEach(textCatalogFiles,
                             () => new POParser(parserSettings),
                             (file, s, p) =>
            {
                try
                {
                    POParseResult result;
                    using (var stream = file.CreateReadStream())
                        result = p.Parse(new StreamReader(stream));

                    if (result.Success)
                    {
                        lock (textCatalogs)
                            textCatalogs.Add((file.Name, result.Catalog.GetCultureName(), result.Catalog));
                    }
コード例 #2
0
        void Initialize()
        {
            var cultures = _env.ContentRootFileProvider.GetDirectoryContents(BasePath)
                           .Where(fi => fi.IsDirectory)
                           .Select(fi => fi.Name)
                           .ToArray();

            AvailableCultures = cultures.Select(c => new CultureInfo(c)).ToArray();

            var textCatalogFiles = cultures.SelectMany(
                c => _env.ContentRootFileProvider.GetDirectoryContents(Path.Combine(BasePath, c))
                .Where(fi => !fi.IsDirectory && ".po".Equals(Path.GetExtension(fi.Name), StringComparison.OrdinalIgnoreCase)),
                (c, f) => (Culture: c, FileInfo: f));

            var textCatalogs = new List <(string FileName, string Culture, POCatalog Catalog)>();

            var parserSettings = new POParserSettings
            {
                SkipComments    = true,
                SkipInfoHeaders = true,
            };

            Parallel.ForEach(textCatalogFiles,
                             () => new POParser(parserSettings),
                             (it, s, p) =>
            {
                POParseResult result;
                using (var stream = it.FileInfo.CreateReadStream())
                    result = p.Parse(new StreamReader(stream));

                if (result.Success)
                {
                    lock (textCatalogs)
                        textCatalogs.Add((it.FileInfo.Name, it.Culture, result.Catalog));
                }
コード例 #3
0
        public async Task InitDbFromPoFiles(LocalizationDbContext localizationDbContext)
        {
            Logger.LogInformation("Importing PO files in db");

            var basePath = "Localization";

            IReadOnlyDictionary <string, POCatalog> TextCatalogs = new Dictionary <string, POCatalog>();

            var cultures = _environment.ContentRootFileProvider.GetDirectoryContents(basePath)
                           .Where(fi => fi.IsDirectory)
                           .Select(fi => fi.Name)
                           .ToArray();

            var textCatalogFiles = cultures.SelectMany(
                c => _environment.ContentRootFileProvider.GetDirectoryContents(Path.Combine(basePath, c))
                .Where(fi => !fi.IsDirectory && ".po".Equals(Path.GetExtension(fi.Name), StringComparison.OrdinalIgnoreCase)),
                (c, f) => (Culture: c, FileInfo: f));

            var textCatalogs = new List <(string FileName, string Culture, POCatalog Catalog)>();

            var parserSettings = new POParserSettings
            {
                SkipComments    = true,
                SkipInfoHeaders = true,
            };

            Parallel.ForEach(textCatalogFiles,
                             () => new POParser(parserSettings),
                             (it, s, p) =>
            {
                POParseResult result;
                using (var stream = it.FileInfo.CreateReadStream())
                    result = p.Parse(new StreamReader(stream));

                if (result.Success)
                {
                    if (result.Catalog.GetCultureName() != it.Culture)
                    {
                        Logger.LogWarning($"Translation file '{Path.Combine(basePath, it.Culture, it.FileInfo.Name)}' language / folder mismatch.");
                    }
                    else
                    {
                        lock (textCatalogs)
                            textCatalogs.Add((it.FileInfo.Name, it.Culture, result.Catalog));
                    }
コード例 #4
0
ファイル: POParserTest.cs プロジェクト: GioviQ/po
        public void ParseWithStringDecodingOptions()
        {
            CheckCatalog(new POStringDecodingOptions {
            }, Environment.NewLine, Environment.NewLine);
            CheckCatalog(new POStringDecodingOptions {
                KeepKeyStringsPlatformIndependent = true
            }, "\n", Environment.NewLine);
            CheckCatalog(new POStringDecodingOptions {
                KeepTranslationStringsPlatformIndependent = true
            }, Environment.NewLine, "\n");
            CheckCatalog(new POStringDecodingOptions {
                KeepKeyStringsPlatformIndependent = true, KeepTranslationStringsPlatformIndependent = true
            }, "\n", "\n");

            void CheckCatalog(POStringDecodingOptions options, string expectedKeyStringNewLine, string expectedTranslationStringNewLine)
            {
                var parserSettings = new POParserSettings
                {
                    StringDecodingOptions = options
                };

                var parser = new POParser(parserSettings);

                POParseResult result = parser.Parse(new MemoryStream(Resources.NewLineTestPO));

                Assert.True(result.Success);

                POCatalog catalog = result.Catalog;

                Assert.Equal(4, catalog.Headers.Count);
                Assert.Equal("en_US", catalog.Headers["Language"]);

                Assert.Equal(1, catalog.Count);

                Assert.Equal(
                    new POKey($"Id of{expectedKeyStringNewLine}a long text", $"Plural id of{expectedKeyStringNewLine}a long text", $"Context id of{expectedKeyStringNewLine}a long text"),
                    catalog[0].Key);

                IPOEntry entry = catalog[0];

                Assert.Equal(2, entry.Count);
                Assert.Equal($"Singular translation of{expectedTranslationStringNewLine}a long text", entry[0]);
                Assert.Equal($"Plural translation of{expectedTranslationStringNewLine}a long text", entry[1]);

                IList <POComment> comments = catalog[0].Comments;

                Assert.Equal(3, comments?.Count ?? 0);

                POComment comment = comments[0];

                Assert.Equal(POCommentKind.PreviousValue, comment.Kind);
                Assert.Equal(POIdKind.ContextId, ((POPreviousValueComment)comment).IdKind);
                Assert.Equal($"Previous context id of{expectedKeyStringNewLine}a long text", ((POPreviousValueComment)comment).Value);

                comment = comments[1];
                Assert.Equal(POCommentKind.PreviousValue, comment.Kind);
                Assert.Equal(POIdKind.Id, ((POPreviousValueComment)comment).IdKind);
                Assert.Equal($"Previous id of{expectedKeyStringNewLine}a long text", ((POPreviousValueComment)comment).Value);

                comment = comments[2];
                Assert.Equal(POCommentKind.PreviousValue, comment.Kind);
                Assert.Equal(POIdKind.PluralId, ((POPreviousValueComment)comment).IdKind);
                Assert.Equal($"Previous plural id of{expectedKeyStringNewLine}a long text", ((POPreviousValueComment)comment).Value);
            }
        }