Browse Source

Add `verify` feature to `translate` command.

pull/18656/head
maliming 2 years ago
parent
commit
153d8870c4
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 1
      framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj
  2. 4
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  3. 60
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs

1
framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj

@ -32,6 +32,7 @@
<ProjectReference Include="..\Volo.Abp.Http\Volo.Abp.Http.csproj" />
<ProjectReference Include="..\Volo.Abp.IdentityModel\Volo.Abp.IdentityModel.csproj" />
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
<ProjectReference Include="..\Volo.Abp.Localization\Volo.Abp.Localization.csproj" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
</Project>

4
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs

@ -10,6 +10,7 @@ using Volo.Abp.Domain;
using Volo.Abp.Http;
using Volo.Abp.IdentityModel;
using Volo.Abp.Json;
using Volo.Abp.Localization;
using Volo.Abp.Minify;
using Volo.Abp.Modularity;
@ -20,7 +21,8 @@ namespace Volo.Abp.Cli;
typeof(AbpJsonModule),
typeof(AbpIdentityModelModule),
typeof(AbpMinifyModule),
typeof(AbpHttpModule)
typeof(AbpHttpModule),
typeof(AbpLocalizationModule)
)]
public class AbpCliCoreModule : AbpModule
{

60
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/TranslateCommand.cs

@ -11,6 +11,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Volo.Abp.Cli.Args;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization.Json;
namespace Volo.Abp.Cli.Commands;
@ -23,6 +24,14 @@ public class TranslateCommand : IConsoleCommand, ITransientDependency
public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
var currentDirectory = Directory.GetCurrentDirectory();
// Verify
if (commandLineArgs.Options.ContainsKey(Options.Verify.Long))
{
await VerifyJsonAsync(currentDirectory);
return;
}
var referenceCulture = commandLineArgs.Options.GetOrNull(Options.ReferenceCulture.Short, Options.ReferenceCulture.Long) ?? "en";
var allValues = commandLineArgs.Options.ContainsKey(Options.AllValues.Short) || commandLineArgs.Options.ContainsKey(Options.AllValues.Long);
@ -368,20 +377,22 @@ public class TranslateCommand : IConsoleCommand, ITransientDependency
return Task.CompletedTask;
}
private static IEnumerable<string> GetCultureJsonFiles(string path, string cultureName)
private static IEnumerable<string> GetCultureJsonFiles(string path, string cultureName = null)
{
var excludeDirectory = new List<string>()
{
"node_modules",
Path.Combine("bin", "debug"),
Path.Combine("obj", "debug")
};
var allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures);
{
"node_modules",
"wwwroot",
".git",
"bin",
"obj"
};
var allCultureNames = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(x => !x.Name.IsNullOrWhiteSpace()).Select(x => x.Name).ToList();
return Directory.GetFiles(path, "*.json", SearchOption.AllDirectories)
.Where(file => excludeDirectory.All(x => file.IndexOf(x, StringComparison.OrdinalIgnoreCase) == -1))
.Where(jsonFile => allCultureInfos.Any(culture => jsonFile.EndsWith($"{cultureName}.json", StringComparison.OrdinalIgnoreCase)));
.Where(file => allCultureNames.Any(x => Path.GetFileName(file).Equals($"{x}.json", StringComparison.OrdinalIgnoreCase)))
.WhereIf(!cultureName.IsNullOrWhiteSpace(), jsonFile => Path.GetFileName(jsonFile).Equals($"{cultureName}.json", StringComparison.OrdinalIgnoreCase));
}
private AbpLocalizationInfo GetAbpLocalizationInfoOrNull(string path)
@ -468,6 +479,29 @@ public class TranslateCommand : IConsoleCommand, ITransientDependency
return translateInfo;
}
private Task VerifyJsonAsync(string currentDirectory)
{
var jsonFiles = GetCultureJsonFiles(currentDirectory);
var hasInvalidJsonFile = false;
foreach (var jsonFile in jsonFiles)
{
try
{
var jsonString = File.ReadAllText(jsonFile);
_ = JsonLocalizationDictionaryBuilder.BuildFromJsonString(jsonString);
}
catch (Exception e)
{
Logger.LogError($"Invalid json file: {jsonFile}");
hasInvalidJsonFile = true;
}
}
Logger.LogInformation(!hasInvalidJsonFile ? "All json files are valid." : "Some json files are invalid.");
return Task.CompletedTask;
}
private static AbpLocalizationInfo SortLocalizedKeys(AbpLocalizationInfo targetLocalizationInfo, AbpLocalizationInfo referenceLocalizationInfo)
{
var sortedLocalizationInfo = new AbpLocalizationInfo
@ -503,8 +537,9 @@ public class TranslateCommand : IConsoleCommand, ITransientDependency
sb.AppendLine("--all-values|-all Include all keys. Default false");
sb.AppendLine("--apply|-a Creates or updates the file for the translated culture.");
sb.AppendLine("--file|-f <file-name> Default: abp-translation.json");
sb.AppendLine("--online|-o Translate online.");
sb.AppendLine("--online Translate online.");
sb.AppendLine("--deepl-auth-key <auth-key> DeepL auth key for online translation.");
sb.AppendLine("--verify Verify that all localized files are correct JSON format.");
sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine("");
@ -572,6 +607,11 @@ public class TranslateCommand : IConsoleCommand, ITransientDependency
{
public const string Short = "deepl-auth-key";
}
public static class Verify
{
public const string Long = "verify";
}
}
public class AbpTranslateInfo

Loading…
Cancel
Save