From 3f229609a7fa2188966995b1cada3a7a727b55dd Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Mon, 25 May 2020 21:48:22 +0800 Subject: [PATCH] Apply translate. --- .../Volo/Abp/Cli/Commands/TranslateCommand.cs | 245 +++++++++++++++--- 1 file changed, 202 insertions(+), 43 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs index 14d5493ad7..31f8480c40 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs @@ -19,54 +19,69 @@ namespace Volo.Abp.Cli.Commands public Task ExecuteAsync(CommandLineArgs commandLineArgs) { - var currentDirectory = @"D:\Github\volo\abp\modules\identity"; //Directory.GetCurrentDirectory(); + var currentDirectory = Directory.GetCurrentDirectory(); - var targetCulture = commandLineArgs.Options.GetOrNull(Options.Culture.Short, Options.Culture.Long); - if (targetCulture == null) + var apply = commandLineArgs.Options.ContainsKey(Options.Apply.Short) || commandLineArgs.Options.ContainsKey(Options.Apply.Long); + if (apply) { - throw new CliUsageException( - "Target culture is missing!" + - Environment.NewLine + Environment.NewLine + - GetUsageInfo() - ); - } + var inputFile = Path.Combine(currentDirectory, + commandLineArgs.Options.GetOrNull(Options.File.Short, Options.File.Long) + ?? "abp-translation.json"); - var referenceCulture = commandLineArgs.Options.GetOrNull(Options.ReferenceCulture.Short, Options.ReferenceCulture.Long); - if (referenceCulture == null) - { - throw new CliUsageException( - "reference culture is missing!" + - Environment.NewLine + Environment.NewLine + - GetUsageInfo() - ); + Logger.LogInformation("Abp translate apply..."); + Logger.LogInformation("Input file: " + inputFile); + + ApplyAbpTranslateInfo(currentDirectory, inputFile); } + else + { + var targetCulture = commandLineArgs.Options.GetOrNull(Options.Culture.Short, Options.Culture.Long); + if (targetCulture == null) + { + throw new CliUsageException( + "Target culture is missing!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + var referenceCulture = commandLineArgs.Options.GetOrNull(Options.ReferenceCulture.Short, Options.ReferenceCulture.Long); + if (referenceCulture == null) + { + throw new CliUsageException( + "reference culture is missing!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } - var outputFile = Path.Combine(currentDirectory, - commandLineArgs.Options.GetOrNull(Options.Output.Short, Options.Output.Long) - ?? "abp-translation.json"); + var outputFile = Path.Combine(currentDirectory, + commandLineArgs.Options.GetOrNull(Options.Output.Short, Options.Output.Long) + ?? "abp-translation.json"); - var allValues = Convert.ToBoolean(commandLineArgs.Options.GetOrNull(Options.AllValues.Short, Options.AllValues.Long) - ?? "false"); + var allValues = Convert.ToBoolean(commandLineArgs.Options.GetOrNull(Options.AllValues.Short, Options.AllValues.Long) + ?? "false"); - Logger.LogInformation("Abp Translate..."); - Logger.LogInformation("Target culture: " + targetCulture); - Logger.LogInformation("Reference culture: " + referenceCulture); - Logger.LogInformation("Output file: " + outputFile); - if (allValues) - { - Logger.LogInformation("Include all keys"); - } + Logger.LogInformation("Abp translateA..."); + Logger.LogInformation("Target culture: " + targetCulture); + Logger.LogInformation("Reference culture: " + referenceCulture); + Logger.LogInformation("Output file: " + outputFile); + if (allValues) + { + Logger.LogInformation("Include all keys"); + } - var translateInfo = GetAbpTranslateInfo(currentDirectory, targetCulture, referenceCulture, allValues); + var translateInfo = GetAbpTranslateInfo(currentDirectory, targetCulture, referenceCulture, allValues); - File.WriteAllText(outputFile, JsonConvert.SerializeObject(translateInfo, Formatting.Indented)); + File.WriteAllText(outputFile, JsonConvert.SerializeObject(translateInfo, Formatting.Indented)); - Logger.LogInformation($"The translation file has been created to {outputFile}."); + Logger.LogInformation($"The translation file has been created."); + } return Task.CompletedTask; } - private static AbpTranslateInfo GetAbpTranslateInfo(string directory, string targetCultureName, string referenceCultureName, bool allValues) + private AbpTranslateInfo GetAbpTranslateInfo(string directory, string targetCultureName, string referenceCultureName, bool allValues) { var translateInfo = new AbpTranslateInfo { @@ -129,6 +144,85 @@ namespace Volo.Abp.Cli.Commands return translateInfo; } + private void ApplyAbpTranslateInfo(string directory, string filename) + { + var translateJsonPath = Path.Combine(directory, filename); + if (!File.Exists(translateJsonPath)) + { + throw new CliUsageException( + $"{translateJsonPath} file does not exist.." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + var translateInfo = GetAbpTranslateInfo(translateJsonPath); + foreach (var resource in translateInfo.Resources) + { + var targetFile = Path.Combine(resource.ResourcePath, translateInfo.TargetCulture + ".json"); + var targetLocalizationInfo = File.Exists(targetFile) + ? GetAbpLocalizationInfoOrNull(targetFile) + : new AbpLocalizationInfo() + { + Culture = translateInfo.TargetCulture, + Texts = new List() + }; + + if (targetLocalizationInfo == null) + { + throw new CliUsageException( + $"Failed to get localization information from {targetFile} file." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + var referenceFile = Path.Combine(resource.ResourcePath, translateInfo.ReferenceCulture + ".json"); + if (!File.Exists(referenceFile)) + { + throw new CliUsageException( + $"{referenceFile} file does not exist.." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + var referenceLocalizationInfo = GetAbpLocalizationInfoOrNull(referenceFile); + if (referenceLocalizationInfo == null) + { + throw new CliUsageException( + $"Failed to get localization information from {referenceFile} file." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + foreach (var text in resource.Texts) + { + var targetText = targetLocalizationInfo.Texts.FirstOrDefault(x => x.Name == text.LocalizationKey); + if (targetText != null) + { + Logger.LogInformation($"Update translation: {targetText.Name}=>" + text.Target); + targetText.Value = text.Target; + } + else + { + Logger.LogInformation($"Create translation: {text.LocalizationKey}=>" + text.Target); + targetLocalizationInfo.Texts.Add(new NameValue(text.LocalizationKey, text.Target)); + } + } + + Logger.LogInformation($"Write translation json to {targetFile}."); + + // sort keys + targetLocalizationInfo = SortLocalizedKeys(targetLocalizationInfo, referenceLocalizationInfo); + File.WriteAllText(targetFile, AbpLocalizationInfoToJsonFile(targetLocalizationInfo)); + + // remove translate json file(abp-translation.json) + File.Delete(translateJsonPath); + Logger.LogInformation($"Delete the {translateJsonPath} file, if you need to translate again, please re-run the [abp translate] command."); + } + } + private static IEnumerable GetCultureJsonFiles(string directory,string cultureName) { var excludeDirectory = new List() @@ -145,8 +239,17 @@ namespace Volo.Abp.Cli.Commands .Where(jsonFile => allCultureInfos.Any(culture => jsonFile.EndsWith($"{cultureName}.json", StringComparison.OrdinalIgnoreCase))); } - private static AbpLocalizationInfo GetAbpLocalizationInfoOrNull(string path) + private AbpLocalizationInfo GetAbpLocalizationInfoOrNull(string path) { + if (!File.Exists(path)) + { + throw new CliUsageException( + $"File {path} does not exist!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + var json = File.ReadAllText(path); JObject jObject; try @@ -171,18 +274,74 @@ namespace Volo.Abp.Cli.Commands Texts = new List() }; - if (texts.Any()) + foreach (var text in texts) { - foreach (var text in texts) - { - var property = (text as JProperty); - localizationInfo.Texts.Add(new NameValue(property?.Name, property?.Value.Value())); - } + var property = (text as JProperty); + localizationInfo.Texts.Add(new NameValue(property?.Name, property?.Value.Value())); } return localizationInfo; } + private static string AbpLocalizationInfoToJsonFile(AbpLocalizationInfo localizationInfo) + { + var jObject = new JObject {{"culture", localizationInfo.Culture}}; + var value = new JObject(); + foreach (var text in localizationInfo.Texts) + { + value.Add(text.Name, text.Value); + } + jObject.Add("texts", value); + return jObject.ToString(); + } + + private AbpTranslateInfo GetAbpTranslateInfo(string path) + { + if (!File.Exists(path)) + { + throw new CliUsageException( + $"File {path} does not exist!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + AbpTranslateInfo translateInfo; + var json = File.ReadAllText(path); + try + { + translateInfo = JsonConvert.DeserializeObject(json); + } + catch (Exception e) + { + throw new CliUsageException( + e.Message + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + return translateInfo; + } + + private static AbpLocalizationInfo SortLocalizedKeys(AbpLocalizationInfo targetLocalizationInfo, AbpLocalizationInfo referenceLocalizationInfo) + { + var sortedLocalizationInfo = new AbpLocalizationInfo + { + Culture = targetLocalizationInfo.Culture, + Texts = new List() + }; + + foreach (var targetText in referenceLocalizationInfo.Texts.Select(text => + targetLocalizationInfo.Texts.FirstOrDefault(x => x.Name == text.Name)) + .Where(targetText => targetText != null)) + { + sortedLocalizationInfo.Texts.Add(targetText); + } + + return sortedLocalizationInfo; + } + public string GetUsageInfo() { var sb = new StringBuilder(); @@ -205,8 +364,8 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine(""); sb.AppendLine(" abp translate -c zh-Hans -r en"); sb.AppendLine(" abp translate -c zh-Hans -r en -a"); - sb.AppendLine(" abp translate apply"); - sb.AppendLine(" abp translate apply -f my-translation.json"); + sb.AppendLine(" abp translate --apply"); + sb.AppendLine(" abp translate -a -f my-translation.json"); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI");