Skip to content

Commit

Permalink
feat: plural strings (#731)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Bodnar <andrii.bodnar@crowdin.com>
  • Loading branch information
katerina20 and andrii-bodnar authored Mar 6, 2024
1 parent c280608 commit 503a974
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 48 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,27 @@ public SourceString addSourceString(AddSourceStringRequest request) {
.getData());
}

@Override
public SourceString addSourcePluralString(AddSourcePluralStringRequest request) {
return executeRequest(() -> this.client.getSourceStringsApi()
.addSourcePluralString(this.projectId, request)
.getData());
}

@Override
public SourceString addSourceStringStringsBased(AddSourceStringStringsBasedRequest request) {
return executeRequest(() -> this.client.getSourceStringsApi()
.addSourceStringStringsBased(this.projectId, request)
.getData());
}

@Override
public SourceString addSourcePluralStringStringsBased(AddSourcePluralStringStringsBasedRequest request) {
return executeRequest(() -> this.client.getSourceStringsApi()
.addSourcePluralStringStringsBased(this.projectId, request)
.getData());
}

@Override
public List<SourceString> listSourceString(Long fileId, Long branchId, String labelIds, String filter, String croql) {
return executeRequestFullList((limit, offset) -> this.client.getSourceStringsApi()
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ default CrowdinProjectFull downloadFullProject() {

SourceString addSourceString(AddSourceStringRequest request);

SourceString addSourcePluralString(AddSourcePluralStringRequest request);

SourceString addSourceStringStringsBased(AddSourceStringStringsBasedRequest request);

SourceString addSourcePluralStringStringsBased(AddSourcePluralStringStringsBasedRequest request);

List<SourceString> listSourceString(Long fileId, Long branchId, String labelIds, String filter, String croql);

void deleteSourceString(Long id);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ NewAction<ProjectProperties, ProjectClient> status(
boolean noProgress, String branchName, String languageId, String file, String directory, boolean isVerbose, boolean showTranslated, boolean showApproved, boolean failIfIncomplete);

NewAction<ProjectProperties, ProjectClient> stringAdd(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, String branch, Boolean hidden);
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, String branch, Boolean hidden,
String one, String two, String few, String many, String zero);

NewAction<ProjectProperties, ProjectClient> stringComment(boolean plainView,
boolean noProgress, String text, String stringId, String language, String type, String issueType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public NewAction<ProjectProperties, ProjectClient> status(

@Override
public NewAction<ProjectProperties, ProjectClient> stringAdd(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, String branch, Boolean hidden
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, String branch, Boolean hidden,
String one, String two, String few, String many, String zero
) {
return new StringAddAction(noProgress, text, identifier, maxLength, context, files, labelNames, branch, hidden);
return new StringAddAction(noProgress, text, identifier, maxLength, context, files, labelNames, branch, hidden, one, two, few, many, zero);
}
@Override
public NewAction<ProjectProperties, ProjectClient> stringComment(boolean plainView,
Expand Down
63 changes: 34 additions & 29 deletions src/main/java/com/crowdin/cli/commands/actions/StringAddAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import com.crowdin.client.projectsgroups.model.Type;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.FileInfo;
import com.crowdin.client.sourcestrings.model.AddSourcePluralStringRequest;
import com.crowdin.client.sourcestrings.model.AddSourcePluralStringStringsBasedRequest;
import com.crowdin.client.sourcestrings.model.AddSourceStringRequest;
import com.crowdin.client.sourcestrings.model.AddSourceStringStringsBasedRequest;
import lombok.Data;

import java.util.List;
import java.util.Map;
Expand All @@ -25,6 +28,7 @@
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING;

@Data
class StringAddAction implements NewAction<ProjectProperties, ProjectClient> {

private final boolean noProgress;
Expand All @@ -36,45 +40,40 @@ class StringAddAction implements NewAction<ProjectProperties, ProjectClient> {
private final List<String> labelNames;
private final String branchName;
private final Boolean hidden;

public StringAddAction(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, String branchName, Boolean hidden
) {
this.noProgress = noProgress;
this.text = text;
this.identifier = identifier;
this.maxLength = maxLength;
this.context = context;
this.files = files;
this.labelNames = labelNames;
this.branchName = branchName;
this.hidden = hidden;
}
private final String one;
private final String two;
private final String few;
private final String many;
private final String zero;

@Override
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, false, client::downloadFullProject);
boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED);
boolean isPluralString = one != null || two != null || few != null || many != null || zero != null;

List<Long> labelIds = (labelNames != null && !labelNames.isEmpty()) ? this.prepareLabelIds(client) : null;

if (files == null || files.isEmpty()) {
if (isStringsBasedProject) {
Branch branch = BranchUtils.getOrCreateBranch(out, branchName, client, project, false);
if (Objects.isNull(branch)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project"));
}
if (isStringsBasedProject) {
if (files != null && !files.isEmpty()) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.no_file_string_project"));
}
Branch branch = BranchUtils.getOrCreateBranch(out, branchName, client, project, false);
if (Objects.isNull(branch)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project"));
}
if (isPluralString) {
AddSourcePluralStringStringsBasedRequest request = RequestBuilder.addPluralStringStringsBased(
this.text, this.identifier, this.maxLength, this.context, branch.getId(), this.hidden, labelIds, one, two, few, many, zero);
client.addSourcePluralStringStringsBased(request);
} else {
AddSourceStringStringsBasedRequest request = RequestBuilder.addStringStringsBased(this.text, this.identifier, this.maxLength, this.context, branch.getId(), this.hidden, labelIds);
client.addSourceStringStringsBased(request);
} else {
AddSourceStringRequest request = RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, null, this.hidden, labelIds);
client.addSourceString(request);
}
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.source_string_uploaded")));
} else {
if (isStringsBasedProject) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.no_file_string_project"));
if (files == null || files.isEmpty()) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.file_required"));
}
Map<String, FileInfo> paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos());
boolean containsError = false;
Expand All @@ -90,9 +89,15 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
}
Long fileId = paths.get(file).getId();

AddSourceStringRequest request =
RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, fileId, this.hidden, labelIds);
client.addSourceString(request);
if (isPluralString) {
AddSourcePluralStringRequest request = RequestBuilder.addPluralString(
this.text, this.identifier, this.maxLength, this.context, fileId, this.hidden, labelIds, one, two, few, many, zero);
client.addSourcePluralString(request);
} else {
AddSourceStringRequest request =
RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, fileId, this.hidden, labelIds);
client.addSourceString(request);
}
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.source_string_for_file_uploaded"), file)));
}
if (containsError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -94,7 +95,19 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
String labelsString = (ss.getLabelIds() != null)
? ss.getLabelIds().stream().map(labelsMap::get).map(s -> String.format("[@|cyan %s|@]", s)).collect(Collectors.joining(" "))
: "";
out.println(String.format(RESOURCE_BUNDLE.getString("message.source_string_list_text"), ss.getId(), ss.getText(), labelsString));
StringBuilder text = new StringBuilder();
if (ss.getText() instanceof HashMap<?, ?>) {
HashMap<?, ?> map = (HashMap<?, ?>) ss.getText();
for (Map.Entry<?, ?> entry : map.entrySet()) {
text.append(entry.getKey()).append(": ").append(entry.getValue()).append(" | ");
}
if (text.length() > 0) {
text.delete(text.length() - 3, text.length());
}
} else {
text.append((String) ss.getText());
}
out.println(String.format(RESOURCE_BUNDLE.getString("message.source_string_list_text"), ss.getId(), text, labelsString));
if (isVerbose) {
if (ss.getIdentifier() != null) {
out.println(String.format("\t- @|bold identifier|@: '%s'", ss.getIdentifier()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import com.crowdin.client.glossaries.model.ImportGlossaryRequest;
import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.sourcefiles.model.AddBranchRequest;
import com.crowdin.client.sourcestrings.model.AddSourceStringRequest;
import com.crowdin.client.sourcestrings.model.AddSourceStringStringsBasedRequest;
import com.crowdin.client.sourcestrings.model.*;
import com.crowdin.client.stringcomments.model.AddStringCommentRequest;
import com.crowdin.client.stringcomments.model.Type;
import com.crowdin.client.tasks.model.CreateTaskRequest;
Expand Down Expand Up @@ -42,6 +41,20 @@ public static AddSourceStringRequest addString(String text, String identifier, I
return request;
}

public static AddSourcePluralStringRequest addPluralString(String text, String identifier, Integer maxLength, String context, Long fileId, Boolean hidden, List<Long> labelIds,
String one, String two, String few, String many, String zero) {
AddSourcePluralStringRequest request = new AddSourcePluralStringRequest();
PluralText pluralText = buildPluralText(text, one, two, few, many, zero);
request.setText(pluralText);
request.setIdentifier(identifier);
request.setMaxLength(maxLength);
request.setContext(context);
request.setFileId(fileId);
request.setIsHidden(hidden);
request.setLabelIds(labelIds);
return request;
}

public static AddSourceStringStringsBasedRequest addStringStringsBased(String text, String identifier, Integer maxLength, String context, Long branchId, Boolean hidden, List<Long> labelIds) {
AddSourceStringStringsBasedRequest request = new AddSourceStringStringsBasedRequest();
request.setText(text);
Expand All @@ -54,6 +67,20 @@ public static AddSourceStringStringsBasedRequest addStringStringsBased(String te
return request;
}

public static AddSourcePluralStringStringsBasedRequest addPluralStringStringsBased(String text, String identifier, Integer maxLength, String context, Long branchId, Boolean hidden, List<Long> labelIds,
String one, String two, String few, String many, String zero) {
AddSourcePluralStringStringsBasedRequest request = new AddSourcePluralStringStringsBasedRequest();
PluralText pluralText = buildPluralText(text, one, two, few, many, zero);
request.setText(pluralText);
request.setIdentifier(identifier);
request.setMaxLength(maxLength);
request.setContext(context);
request.setBranchId(branchId);
request.setIsHidden(hidden);
request.setLabelIds(labelIds);
return request;
}

public static AddStringCommentRequest addComment(String text, String type, String language, String issueType,
String stringId) {
AddStringCommentRequest request = new AddStringCommentRequest();
Expand Down Expand Up @@ -326,4 +353,14 @@ public static AddBranchRequest addBranch(String name, String title, String expor
return request;
}

private static PluralText buildPluralText(String text, String one, String two, String few, String many, String zero) {
PluralText pluralText = new PluralText();
Optional.ofNullable(text).ifPresent(pluralText::setOther);
Optional.ofNullable(one).ifPresent(pluralText::setOne);
Optional.ofNullable(two).ifPresent(pluralText::setTwo);
Optional.ofNullable(few).ifPresent(pluralText::setFew);
Optional.ofNullable(many).ifPresent(pluralText::setMany);
Optional.ofNullable(zero).ifPresent(pluralText::setZero);
return pluralText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ class StringAddSubcommand extends ActCommandProject {
@CommandLine.Option(names = {"--hidden"}, order = -2)
protected Boolean isHidden;

@CommandLine.Option(names = {"--one"}, descriptionKey = "crowdin.string.add.one", paramLabel = "...", order = -2)
protected String one;

@CommandLine.Option(names = {"--two"}, descriptionKey = "crowdin.string.add.two", paramLabel = "...", order = -2)
protected String two;

@CommandLine.Option(names = {"--few"}, descriptionKey = "crowdin.string.add.few", paramLabel = "...", order = -2)
protected String few;

@CommandLine.Option(names = {"--many"}, descriptionKey = "crowdin.string.add.many", paramLabel = "...", order = -2)
protected String many;

@CommandLine.Option(names = {"--zero"}, descriptionKey = "crowdin.string.add.zero", paramLabel = "...", order = -2)
protected String zero;

@Override
protected List<String> checkOptions() {
List<String> errors = new ArrayList<>();
Expand All @@ -58,6 +73,7 @@ protected List<String> checkOptions() {

@Override
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) {
return actions.stringAdd(noProgress, text, identifier, maxLength, context, files, labelNames, branch, isHidden);
return actions.stringAdd(noProgress, text, identifier, maxLength, context, files, labelNames, branch, isHidden,
one, two, few, many, zero);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ crowdin.string.add.max-length=Set a max. length of the translated text for the n
crowdin.string.add.context=Add a context for the new source string
crowdin.string.add.file=Specify a file the new source string should be added to (multiple files could be specified)
crowdin.string.add.hidden=Choose whether or not the added strings should be hidden in your Crowdin project
crowdin.string.add.one=Plural form one (singular)
crowdin.string.add.two=Plural form two (dual)
crowdin.string.add.few=Plural form few (paucal)
crowdin.string.add.many=Plural form many
crowdin.string.add.zero=Plural form zero

# CROWDIN STRING COMMENT COMMAND
crowdin.string.comment.usage.description=Add a comment to the given string
Expand Down Expand Up @@ -465,6 +470,7 @@ error.while_checking_base_path=Failed to check the base path. Try to run the app
error.skip_untranslated_both_strings_and_files=You cannot skip strings and files at the same time. Please use one of these parameters instead.
error.file_not_exists=Project doesn't contain the '%s' file
error.file_id_not_exists=Project doesn't contain file with id '%s'
error.file_required=File is required
error.dir_not_exists=Project doesn't contain the '%s' directory
error.label_not_exists=Project doesn't contain the '%s' label
error.branch_not_exists=Project doesn't contain the '%s' branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testStatus() {

@Test
public void testStringAdd() {
assertNotNull(actions.stringAdd(false, null, null, null, null, null, null, null, null));
assertNotNull(actions.stringAdd(false, null, null, null, null, null, null, null, null, null, null, null, null, null));
}

@Test
Expand Down
Loading

0 comments on commit 503a974

Please sign in to comment.