Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support of passing labels to pre translate command #649

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
this.debug = debug;
this.verbose = verbose;
this.plainView = plainView;
this.labelNames = labelNames;

Check warning on line 68 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L68

Added line #L68 was not covered by tests
}

@Override
Expand All @@ -71,14 +75,15 @@

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

Check warning on line 78 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L78

Added line #L78 was not covered by tests

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 @@
.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 -> {

Check warning on line 155 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L151-L155

Added lines #L151 - L155 were not covered by tests
if (!labels.containsKey(labelName)) {
out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.pre_translate.missing_label"), labelName)));

Check warning on line 157 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L157

Added line #L157 was not covered by tests
}
}

Check warning on line 159 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L159

Added line #L159 was not covered by tests
);
return labelNames.stream()
.map(labels::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());

Check warning on line 164 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L161-L164

Added lines #L161 - L164 were not covered by tests
} else {
return null;

Check warning on line 166 in src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/PreTranslateAction.java#L166

Added line #L166 was not covered by tests
}
}

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 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 @@
request.setDuplicateTranslations(duplicateTranslations);
request.setTranslateUntranslatedOnly(translateUntranslatedOnly);
request.setTranslateWithPerfectMatchOnly(translateWithPerfectMatchOnly);
request.setLabelIds(labelIds);

Check warning on line 284 in src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/functionality/RequestBuilder.java#L284

Added line #L284 was not covered by tests
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=Label '%s' passed is invalid. Skipping it
debanjanc01 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading