Skip to content

Commit

Permalink
feat: ai pre-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed May 18, 2024
1 parent 206ca40 commit 1abc120
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ NewAction<ProjectProperties, ClientTask> taskAdd(

NewAction<PropertiesWithFiles, ProjectClient> preTranslate(
List<String> languageIds, List<String> files, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations,
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List<String> labelNames);
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List<String> labelNames, Long aiPrompt);

NewAction<ProjectProperties, ProjectClient> branchAdd(String name, String title, String exportPattern, Priority priority, boolean plainView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ public NewAction<NoProperties, NoClient> checkNewVersion() {
@Override
public NewAction<PropertiesWithFiles, ProjectClient> preTranslate(
List<String> languageIds, List<String> files, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations,
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List<String> labelNames
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean plainView, List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PreTranslateAction implements NewAction<PropertiesWithFiles, ProjectClient
private final boolean noProgress;
private final boolean plainView;
private final List<String> labelNames;
private final Long aiPrompt;

@Override
public void act(Outputter out, PropertiesWithFiles properties, ProjectClient client) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static List<PatchRequest> updateExcludedTargetLanguages(List<String> excl

public static ApplyPreTranslationRequest applyPreTranslation(
List<String> languageIds, List<Long> fileIds, Method method, Long engineId, AutoApproveOption autoApproveOption,
Boolean duplicateTranslations, Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, List<Long> labelIds
Boolean duplicateTranslations, Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, List<Long> labelIds, Long aiPrompt
) {
ApplyPreTranslationRequest request = new ApplyPreTranslationRequest();
request.setLanguageIds(languageIds);
Expand All @@ -285,6 +285,7 @@ public static ApplyPreTranslationRequest applyPreTranslation(
request.setTranslateUntranslatedOnly(translateUntranslatedOnly);
request.setTranslateWithPerfectMatchOnly(translateWithPerfectMatchOnly);
request.setLabelIds(labelIds);
request.setAiPromptId(aiPrompt);
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class PreTranslateSubcommand extends ActCommandWithFiles {
@CommandLine.Option(names = {"--label"}, descriptionKey = "crowdin.pre-translate.label", paramLabel = "...", order = -2)
protected List<String> labelNames;

@CommandLine.Option(names = {"--ai-prompt"}, descriptionKey = "crowdin.pre-translate.ai-prompt", paramLabel = "...", order = -2)
protected Long aiPrompt;

private final Map<String, AutoApproveOption> autoApproveOptionWrapper = new HashMap<String, AutoApproveOption>() {{
put("all", AutoApproveOption.ALL);
put("except-auto-substituted", AutoApproveOption.EXCEPT_AUTO_SUBSTITUTED);
Expand All @@ -83,14 +86,15 @@ protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions action
translateWithPerfectMatchOnly,
noProgress,
plainView,
labelNames
labelNames,
aiPrompt
);
}

@Override
protected List<String> checkOptions() {
List<String> 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) {
Expand All @@ -109,6 +113,9 @@ protected List<String> 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);
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1abc120

Please sign in to comment.