From 1abc120ad6b1f5c450ab512689af862942a92c91 Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Sat, 18 May 2024 08:57:07 +0200 Subject: [PATCH] feat: ai pre-translate --- src/main/java/com/crowdin/cli/commands/Actions.java | 2 +- .../com/crowdin/cli/commands/actions/CliActions.java | 4 ++-- .../cli/commands/actions/PreTranslateAction.java | 3 ++- .../cli/commands/functionality/RequestBuilder.java | 3 ++- .../cli/commands/picocli/PreTranslateSubcommand.java | 11 +++++++++-- src/main/resources/messages/messages.properties | 4 +++- versions.properties | 2 +- 7 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/crowdin/cli/commands/Actions.java b/src/main/java/com/crowdin/cli/commands/Actions.java index 2596f92c..da067043 100644 --- a/src/main/java/com/crowdin/cli/commands/Actions.java +++ b/src/main/java/com/crowdin/cli/commands/Actions.java @@ -110,7 +110,7 @@ NewAction taskAdd( NewAction preTranslate( List languageIds, List files, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations, - Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List labelNames); + Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List labelNames, Long aiPrompt); NewAction branchAdd(String name, String title, String exportPattern, Priority priority, boolean plainView); diff --git a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java index d784a3d5..a1308c6f 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java +++ b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java @@ -223,10 +223,10 @@ public NewAction checkNewVersion() { @Override public NewAction preTranslate( List languageIds, List files, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations, - Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List labelNames + Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List labelNames, Long aiPrompt ) { return new PreTranslateAction(languageIds, files, method, engineId, branchName, autoApproveOption, duplicateTranslations, - translateUntranslatedOnly, translateWithPerfectMatchOnly, noProgress, plainView, labelNames); + translateUntranslatedOnly, translateWithPerfectMatchOnly, noProgress, plainView, labelNames, aiPrompt); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java b/src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java index b56bd9b4..0222b873 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java @@ -40,6 +40,7 @@ class PreTranslateAction implements NewAction labelNames; + private final Long aiPrompt; @Override public void act(Outputter out, PropertiesWithFiles properties, ProjectClient client) { @@ -102,7 +103,7 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli ApplyPreTranslationRequest request = RequestBuilder.applyPreTranslation( languages, fileIds, method, engineId, autoApproveOption, - duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly, labelIds); + duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly, labelIds, aiPrompt); this.applyPreTranslation(out, client, request); if (containsError) { diff --git a/src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java b/src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java index f0e113d7..7b5d2e78 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java @@ -273,7 +273,7 @@ public static List updateExcludedTargetLanguages(List excl public static ApplyPreTranslationRequest applyPreTranslation( List languageIds, List fileIds, Method method, Long engineId, AutoApproveOption autoApproveOption, - Boolean duplicateTranslations, Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, List labelIds + Boolean duplicateTranslations, Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, List labelIds, Long aiPrompt ) { ApplyPreTranslationRequest request = new ApplyPreTranslationRequest(); request.setLanguageIds(languageIds); @@ -285,6 +285,7 @@ public static ApplyPreTranslationRequest applyPreTranslation( request.setTranslateUntranslatedOnly(translateUntranslatedOnly); request.setTranslateWithPerfectMatchOnly(translateWithPerfectMatchOnly); request.setLabelIds(labelIds); + request.setAiPromptId(aiPrompt); return request; } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/PreTranslateSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/PreTranslateSubcommand.java index d6fb7d75..f1fbb6ed 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/PreTranslateSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/PreTranslateSubcommand.java @@ -57,6 +57,9 @@ public class PreTranslateSubcommand extends ActCommandWithFiles { @CommandLine.Option(names = {"--label"}, descriptionKey = "crowdin.pre-translate.label", paramLabel = "...", order = -2) protected List labelNames; + @CommandLine.Option(names = {"--ai-prompt"}, descriptionKey = "crowdin.pre-translate.ai-prompt", paramLabel = "...", order = -2) + protected Long aiPrompt; + private final Map autoApproveOptionWrapper = new HashMap() {{ put("all", AutoApproveOption.ALL); put("except-auto-substituted", AutoApproveOption.EXCEPT_AUTO_SUBSTITUTED); @@ -83,14 +86,15 @@ protected NewAction getAction(Actions action translateWithPerfectMatchOnly, noProgress, plainView, - labelNames + labelNames, + aiPrompt ); } @Override protected List checkOptions() { List errors = new ArrayList<>(); - if ((Method.MT == method) == (engineId == null)) { + if ((Method.MT == method) && (engineId == null)) { errors.add(RESOURCE_BUNDLE.getString("error.pre_translate.engine_id")); } if ((Method.MT == method) && duplicateTranslations != null) { @@ -109,6 +113,9 @@ protected List checkOptions() { if (autoApproveOption != null && !autoApproveOptionWrapper.containsKey(autoApproveOption)) { errors.add(RESOURCE_BUNDLE.getString("error.pre_translate.auto_approve_option")); } + if ((Method.AI == method) && (aiPrompt == null)) { + errors.add(RESOURCE_BUNDLE.getString("error.pre_translate.ai_prompt")); + } if (files != null) { for (int i = 0; i < files.size(); i++) { String normalizedFile = StringUtils.removeStart(Utils.normalizePath(files.get(i)), Utils.PATH_SEPARATOR); diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index c15d16da..beb4cbb4 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -360,8 +360,9 @@ crowdin.pre-translate.usage.description=Pre-translate files via Machine Translat crowdin.pre-translate.usage.customSynopsis=@|fg(green) crowdin pre-translate|@ [CONFIG OPTIONS] [OPTIONS] crowdin.pre-translate.language=Languages to which pre-translation should be applied. Can be specified multiple times. Default: all crowdin.pre-translate.file=Path to the file in the Crowdin project. Can be specified multiple times -crowdin.pre-translate.method=Defines pre-translation method. Supported values: mt, tm +crowdin.pre-translate.method=Defines pre-translation method. Supported values: mt, tm, ai crowdin.pre-translate.engine-id=Machine Translation engine Identifier +crowdin.pre-translate.ai-prompt=AI Prompt Identifier. Required for 'ai' method crowdin.pre-translate.auto-approve-option=Defines which translations added by TM pre-translation should be auto-approved. Supported values: all, except-auto-substituted, perfect-match-only. Default: none crowdin.pre-translate.duplicate-translations=Adds translations even if the same translation already exists crowdin.pre-translate.translate-untranslated-only=Applies pre-translation for untranslated strings only @@ -555,6 +556,7 @@ error.distribution.incorrect_file_command_usage=The '--file' is used only for th error.distribution.incorrect_bundle_id_command_usage=The '--bundle-id' is used only for the 'bundle' export mode error.pre_translate.engine_id=Machine Translation should be used with the '--engine-id' parameter +error.pre_translate.ai_prompt=AI should be used with the '--ai-prompt' parameter error.pre_translate.duplicate_translations='--duplicate-translations' only works with the TM pre-translation method error.pre_translate.translate_untranslated_only='--translate-untranslated-only' only works with the TM pre-translation method error.pre_translate.translate_with_perfect_match_only='--translate-with-perfect-match-only' only works with the TM pre-translation method diff --git a/versions.properties b/versions.properties index 42f3b0bf..01da4e24 100644 --- a/versions.properties +++ b/versions.properties @@ -43,7 +43,7 @@ version.commons-io..commons-io=2.16.1 version.commons-cli..commons-cli=1.7.0 -version.com.github.crowdin..crowdin-api-client-java=1.17.0 +version.com.github.crowdin..crowdin-api-client-java=1.17.1 plugin.org.asciidoctor.jvm.convert=3.3.2