I'm using the google translate api to translate a list of strings, in this case - candies and their respective colors. I do not have a problem translating, however, the processing time is very slow - somewhere around 20 seconds. I am looking to improve the speed of this. Any suggestions are appreciated, thanks. My code is below:
This is from the service layer of my Spring application. After processing, I pass a List<String>
to my controller layer in my REST service.
The dataset I am processing is about 100 elements.
public List<String> getSortedCandies() {
List<Candy> candyList = new ArrayList<>(this.candyList);
//sort candies
candyList = candyList.stream()
.sorted((o1, o2) -> {
int nameResult = o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
if (nameResult == 0) {
return o1.getColor().toUpperCase().compareTo(o2.getColor().toUpperCase());
}
return nameResult;
}).collect(Collectors.toList());
Translate translate = TranslateOptions.getDefaultInstance().getService();
List<String> translatedList = new ArrayList<>();
//translate from english to spanish
for (Candy candy : candyList) {
Detection detection = translate.detect(candy.toString());
String detectedLanguage = detection.getLanguage();
Translation translation = translate.translate(
candy.toString(),
Translate.TranslateOption.sourceLanguage(detectedLanguage),
Translate.TranslateOption.targetLanguage("es")
);
translatedList.add(translation.getTranslatedText());
}
return translatedList;
}