Skip to content

Commit 5e10020

Browse files
committed
Add command line to translate repository using the AiTranslateService
1 parent 90a91e4 commit 5e10020

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.box.l10n.mojito.cli.command;
2+
3+
import com.beust.jcommander.Parameter;
4+
import com.beust.jcommander.Parameters;
5+
import com.box.l10n.mojito.cli.command.param.Param;
6+
import com.box.l10n.mojito.cli.console.ConsoleWriter;
7+
import com.box.l10n.mojito.rest.client.RepositoryAiTranslateClient;
8+
import com.box.l10n.mojito.rest.client.RepositoryAiTranslateClient.ProtoAiTranslateResponse;
9+
import com.box.l10n.mojito.rest.entity.PollableTask;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import org.fusesource.jansi.Ansi.Color;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.context.annotation.Scope;
17+
import org.springframework.stereotype.Component;
18+
19+
/**
20+
* Command to machine translate untranslated strings in a repository.
21+
*
22+
* @author jaurambault
23+
*/
24+
@Component
25+
@Scope("prototype")
26+
@Parameters(
27+
commandNames = {"repository-ai-translate"},
28+
commandDescription = "Ai translate untranslated and rejected strings in a repository")
29+
public class RepositoryAiTranslationCommand extends Command {
30+
31+
/** logger */
32+
static Logger logger = LoggerFactory.getLogger(RepositoryAiTranslationCommand.class);
33+
34+
@Autowired ConsoleWriter consoleWriter;
35+
36+
@Parameter(
37+
names = {Param.REPOSITORY_LONG, Param.REPOSITORY_SHORT},
38+
arity = 1,
39+
required = true,
40+
description = Param.REPOSITORY_DESCRIPTION)
41+
String repositoryParam;
42+
43+
@Parameter(
44+
names = {Param.REPOSITORY_LOCALES_LONG, Param.REPOSITORY_LOCALES_SHORT},
45+
variableArity = true,
46+
required = true,
47+
description = "List of locales (bcp47 tags) to machine translate")
48+
List<String> locales;
49+
50+
@Parameter(
51+
names = {"--source-text-max-count"},
52+
arity = 1,
53+
description =
54+
"Source text max count per locale sent to MT (this param is used to avoid "
55+
+ "sending too many strings to MT)")
56+
int sourceTextMaxCount = 100;
57+
58+
@Autowired CommandHelper commandHelper;
59+
60+
@Autowired RepositoryAiTranslateClient repositoryAiTranslateClient;
61+
62+
@Override
63+
public boolean shouldShowInCommandList() {
64+
return false;
65+
}
66+
67+
@Override
68+
public void execute() throws CommandException {
69+
70+
consoleWriter
71+
.newLine()
72+
.a("Ai translate repository: ")
73+
.fg(Color.CYAN)
74+
.a(repositoryParam)
75+
.reset()
76+
.a(" for locales: ")
77+
.fg(Color.CYAN)
78+
.a(locales.stream().collect(Collectors.joining(", ", "[", "]")))
79+
.println(2);
80+
81+
ProtoAiTranslateResponse protoAiTranslateResponse =
82+
repositoryAiTranslateClient.translateRepository(
83+
new RepositoryAiTranslateClient.ProtoAiTranslateRequest(
84+
repositoryParam, locales, sourceTextMaxCount));
85+
86+
PollableTask pollableTask = protoAiTranslateResponse.pollableTask();
87+
commandHelper.waitForPollableTask(pollableTask.getId());
88+
}
89+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.box.l10n.mojito.rest.client;
2+
3+
import com.box.l10n.mojito.rest.entity.PollableTask;
4+
import java.util.List;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* @author jaurambault
11+
*/
12+
@Component
13+
public class RepositoryAiTranslateClient extends BaseClient {
14+
15+
/** logger */
16+
static Logger logger = LoggerFactory.getLogger(RepositoryAiTranslateClient.class);
17+
18+
@Override
19+
public String getEntityName() {
20+
return "proto-ai-translate";
21+
}
22+
23+
/** Ai translate untranslated and rejected strings in a repository for a given list of locales */
24+
public ProtoAiTranslateResponse translateRepository(
25+
ProtoAiTranslateRequest protoAiTranslateRequest) {
26+
27+
return authenticatedRestTemplate.postForObject(
28+
getBasePathForEntity(), protoAiTranslateRequest, ProtoAiTranslateResponse.class);
29+
}
30+
31+
public record ProtoAiTranslateRequest(
32+
String repositoryName, List<String> targetBcp47tags, int sourceTextMaxCountPerLocale) {}
33+
34+
public record ProtoAiTranslateResponse(PollableTask pollableTask) {}
35+
}

0 commit comments

Comments
 (0)