Skip to content

Commit

Permalink
feat: added support of passing labels to pre translate command (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
debanjanc01 committed Oct 6, 2023
1 parent 201eb58 commit 619ffbe
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 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 @@ -114,7 +114,7 @@ NewAction<PropertiesWithTargets, ProjectClient> downloadTargets(

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ public NewAction<PropertiesWithTargets, ProjectClient> downloadTargets(
@Override
public NewAction<PropertiesWithFiles, ProjectClient> preTranslate(
List<String> languageIds, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations,
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean debug, boolean verbose, boolean plainView
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean debug, boolean verbose, boolean plainView, List<String> labelNames
) {
return new PreTranslateAction(languageIds, method, engineId, branchName, autoApproveOption, duplicateTranslations,
translateUntranslatedOnly, translateWithPerfectMatchOnly, noProgress, debug, verbose, plainView);
translateUntranslatedOnly, translateWithPerfectMatchOnly, noProgress, debug, verbose, plainView, labelNames);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.crowdin.cli.utils.PlaceholderUtil;
import com.crowdin.cli.utils.Utils;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.languages.model.Language;
import com.crowdin.client.sourcefiles.model.FileInfo;
import com.crowdin.client.translations.model.ApplyPreTranslationRequest;
Expand All @@ -26,6 +27,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
Expand All @@ -45,10 +47,11 @@ class PreTranslateAction implements NewAction<PropertiesWithFiles, ProjectClient
private boolean debug;
private boolean verbose;
private boolean plainView;
private List<String> labelNames;

public PreTranslateAction(
List<String> languageIds, Method method, Long engineId, String branchName, AutoApproveOption autoApproveOption, Boolean duplicateTranslations,
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean debug, boolean verbose, boolean plainView
Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, boolean noProgress, boolean debug, boolean verbose, boolean plainView, List<String> labelNames
) {
this.languageIds = languageIds;
this.method = method;
Expand All @@ -62,6 +65,7 @@ public PreTranslateAction(
this.debug = debug;
this.verbose = verbose;
this.plainView = plainView;
this.labelNames = labelNames;
}

@Override
Expand All @@ -71,14 +75,15 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli

List<String> languages = this.prepareLanguageIds(project);
List<Long> fileIds = this.prepareFileIds(out, properties, project);
List<Long> labelIds = this.prepareLabelIds(out, client);

if (fileIds == null || fileIds.isEmpty()) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.no_files_found_for_pre_translate")));
}

ApplyPreTranslationRequest request = RequestBuilder.applyPreTranslation(
languages, fileIds, method, engineId, autoApproveOption,
duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly);
duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly, labelIds);

this.applyPreTranslation(out, client, request);
}
Expand Down Expand Up @@ -141,6 +146,27 @@ private List<Long> prepareFileIds(Outputter out, PropertiesWithFiles pb, Crowdin
.collect(Collectors.toList());
}

private List<Long> prepareLabelIds(Outputter out, ProjectClient client) {
if (labelNames != null && !labelNames.isEmpty()) {
Map<String, Long> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
labelNames.stream()
.distinct()
.forEach(labelName -> {
if (!labels.containsKey(labelName)) {
out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.pre_translate.missing_label"), labelName)));
}
}
);
return labelNames.stream()
.map(labels::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
} else {
return null;
}
}

private void applyPreTranslation(Outputter out, ProjectClient client, ApplyPreTranslationRequest request) {
ConsoleSpinner.execute(
out,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,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
Boolean duplicateTranslations, Boolean translateUntranslatedOnly, Boolean translateWithPerfectMatchOnly, List<Long> labelIds
) {
ApplyPreTranslationRequest request = new ApplyPreTranslationRequest();
request.setLanguageIds(languageIds);
Expand All @@ -281,6 +281,7 @@ public static ApplyPreTranslationRequest applyPreTranslation(
request.setDuplicateTranslations(duplicateTranslations);
request.setTranslateUntranslatedOnly(translateUntranslatedOnly);
request.setTranslateWithPerfectMatchOnly(translateWithPerfectMatchOnly);
request.setLabelIds(labelIds);
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class PreTranslateSubcommand extends ActCommandWithFiles {
@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain")
protected boolean plainView;

@CommandLine.Option(names = {"--label"}, descriptionKey = "crowdin.pre-translate.label", paramLabel = "...", order = -2)
protected List<String> labelNames;

private final Map<String, AutoApproveOption> autoApproveOptionWrapper = new HashMap<String, AutoApproveOption>() {{
put("all", AutoApproveOption.ALL);
put("except-auto-substituted", AutoApproveOption.EXCEPT_AUTO_SUBSTITUTED);
Expand All @@ -75,7 +78,8 @@ protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions action
noProgress,
debug,
isVerbose,
plainView
plainView,
labelNames
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ crowdin.pre-translate.auto-approve-option=Defines which translations added by TM
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
crowdin.pre-translate.translate-with-perfect-match-only=Applies pre-translation only for the strings with perfect match
crowdin.pre-translate.label=Label identifier. Could be specified multiple times

# CROWDIN DISTRIBUTION COMMAND
crowdin.distribution.usage.description=Manage distributions in a Crowdin Project
Expand Down Expand Up @@ -671,6 +672,7 @@ message.comment.added=@|green Comment|@ @|yellow #%s|@ @|green,bold '%s'|@ @|gre

message.pre_translate.local_files_message=Out of %d files, only %d of them were found locally. Use '--verbose' to list them
message.pre_translate.local_files_message_verbose=Out of %d files, %d of them were found locally:
message.pre_translate.missing_label=The '%s' label is missing in the Crowdin project

message.delete_obsolete.obsolete_file_delete=Obsolete file @|bold '%s'|@ was deleted
message.delete_obsolete.obsolete_directory_delete=No obsolete files were found
Expand Down
2 changes: 1 addition & 1 deletion versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ version.com.h3xstream.findsecbugs..findsecbugs-plugin=1.12.0

version.com.github.fge..json-patch=1.13

version.com.github.crowdin..crowdin-api-client-java=1.11.3
version.com.github.crowdin..crowdin-api-client-java=1.11.5

plugin.org.asciidoctor.jvm.convert=3.3.2

Expand Down

0 comments on commit 619ffbe

Please sign in to comment.