|  | 
|  | 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 | +} | 
0 commit comments