Compartilhando minha implementação do serviço de tradução:
Depois de aproveitar várias ideias daqui, como a do Rodrigo e a do Mateus, e também depois de implementar minha própria solução que acredito ter ficado bem concisa e já estar aplicando o Single Responsibility Principle, queria compartilhar o que fiz:
Dentro de services e dentro de translate:
package br.com.floresdev.screenmatch.service.translate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record TranslationData(TranslationResponseData responseData) {
}
package br.com.floresdev.screenmatch.service.translate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record TranslationResponseData(String translatedText) {
}
package br.com.floresdev.screenmatch.service.translate;
import br.com.floresdev.screenmatch.service.DataConverterService;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class TranslationService {
// Método recebe o texto + a linguagem a ser traduzida (seguindo o padrão ISO 639-1)
public static String getTranslation(String text, String language) {
text = URLEncoder.encode(text, StandardCharsets.UTF_8);
String langpair = URLEncoder.encode("autodetect|" + language, StandardCharsets.UTF_8);
String url = "https://api.mymemory.translated.net/get?q=" + text + "&langpair=" + langpair;
return DataConverterService.convertData(url, TranslationData.class).responseData().translatedText();
}
}
Dentro de application, UserInterface.java:
package br.com.floresdev.screenmatch.application;
import br.com.floresdev.screenmatch.model.EpisodeModel;
import br.com.floresdev.screenmatch.model.SeasonDataModel;
import br.com.floresdev.screenmatch.model.SeriesModel;
import br.com.floresdev.screenmatch.service.DisplayService;
import br.com.floresdev.screenmatch.service.EpisodeService;
import br.com.floresdev.screenmatch.service.SeasonService;
import br.com.floresdev.screenmatch.service.SeriesService;
import java.util.List;
public class UserInterface {
private final UserInteraction userInteraction;
private final SeriesService seriesService;
private final SeasonService seasonService;
private final EpisodeService episodeService;
private final DisplayService displayService;
public UserInterface(UserInteraction userInteraction, SeriesService seriesService,
SeasonService seasonService, EpisodeService episodeService, DisplayService displayService) {
this.userInteraction = userInteraction;
this.seriesService = seriesService;
this.seasonService = seasonService;
this.displayService = displayService;
this.episodeService = episodeService;
}
public void start() {
String repeat = "y";
while (repeat.equals("y")) {
displayService.showMenu();
String seriesName = userInteraction.getSeriesName();
SeriesModel series = seriesService.getSeriesBySeriesData(seriesService.getSeriesDataByName(seriesName));
String fullAddress = seriesService.getFullAddress(seriesName);
int chosenOption = userInteraction.getChosenOption();
switch (chosenOption) {
// código omitido
case 9:
// No meu caso, a opção 9 é a correspondente pra uma opção de traduzir a sinopse pra linguagem escolhida, daí criei um novo construtor em SeriesModel pra receber outro objeto SeriesModel, reaproveitar os membros e repassar a linguagem escolhida pra traduzir
displayService.showSeries(new SeriesModel(series, userInteraction.getTranslationLanguage()));
break;
default:
System.out.println("Invalid chosen option! Please, follow the correct pattern of choice and " +
"try again.");
start();
}
repeat = userInteraction.getRepetitionValue();
}
}
}