Beispiel #1
0
        public async Task <ExportResult> ExportLocalizationFilesAsync(string templateJsonPath, ExportOptions options, CancellationToken cancellationToken = default)
        {
            JsonDocumentOptions jsonOptions = new JsonDocumentOptions()
            {
                CommentHandling     = JsonCommentHandling.Skip,
                AllowTrailingCommas = true,
            };

            try
            {
                using FileStream fileStream     = new FileStream(templateJsonPath, FileMode.Open, FileAccess.Read);
                using JsonDocument jsonDocument = await JsonDocument.ParseAsync(fileStream, jsonOptions, cancellationToken).ConfigureAwait(false);

                TemplateStringExtractor        stringExtractor     = new TemplateStringExtractor(jsonDocument, _loggerFactory);
                IReadOnlyList <TemplateString> templateJsonStrings = stringExtractor.ExtractStrings(out string templateJsonLanguage);

                string targetDirectory = options.TargetDirectory ?? Path.Combine(Path.GetDirectoryName(templateJsonPath) ?? string.Empty, "localize");

                await TemplateStringUpdater.UpdateStringsAsync(
                    templateJsonStrings,
                    templateJsonLanguage,
                    options.Languages ?? ExportOptions.DefaultLanguages,
                    targetDirectory,
                    options.DryRun,
                    _logger,
                    cancellationToken).ConfigureAwait(false);

                return(new ExportResult(templateJsonPath));
            }
            catch (Exception exception)
            {
                return(new ExportResult(templateJsonPath, null, exception));
            }
        }
Beispiel #2
0
        public async Task <ExportResult> ExportLocalizationFilesAsync(string templateJsonPath, ExportOptions options, CancellationToken cancellationToken = default)
        {
            JsonDocumentOptions jsonOptions = new JsonDocumentOptions()
            {
                CommentHandling     = JsonCommentHandling.Skip,
                AllowTrailingCommas = true,
            };

            try
            {
                using FileStream fileStream     = new FileStream(templateJsonPath, FileMode.Open, FileAccess.Read);
                using JsonDocument jsonDocument = await JsonDocument.ParseAsync(fileStream, jsonOptions, cancellationToken).ConfigureAwait(false);

                TemplateStringExtractor        stringExtractor     = new TemplateStringExtractor(jsonDocument, _loggerFactory);
                IReadOnlyList <TemplateString> templateJsonStrings = stringExtractor.ExtractStrings(out string templateJsonLanguage);

                string targetDirectory         = options.TargetDirectory ?? Path.Combine(Path.GetDirectoryName(templateJsonPath) ?? string.Empty, "localize");
                IEnumerable <string> languages = ExportOptions.DefaultLanguages;
                if (options.Languages?.Any() ?? false)
                {
                    languages = options.Languages;
                }

                await TemplateStringUpdater.UpdateStringsAsync(
                    templateJsonStrings,
                    templateJsonLanguage,
                    languages,
                    targetDirectory,
                    options.DryRun,
                    _logger,
                    cancellationToken).ConfigureAwait(false);

                return(new ExportResult(templateJsonPath));
            }
            catch (Exception exception)
                when(exception is JsonMemberMissingException || exception is LocalizationKeyIsNotUniqueException)
                {
                    // Output a more friendly text without stack trace for known errors.
                    return(new ExportResult(templateJsonPath, exception.Message));
                }
            catch (Exception exception)
            {
                return(new ExportResult(templateJsonPath, null, exception));
            }
        }