Created
July 1, 2020 20:46
-
-
Save markito/e976443fd61a6d7d2278f2acfa7c18d3 to your computer and use it in GitHub Desktop.
scf-blog-step2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public Function<String, String> translate() { | |
return input -> { | |
final String fromLang = "en"; | |
final String toLang = "es"; | |
final String url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + fromLang + "&tl=" | |
+ toLang + "&dt=t&q=" + input; | |
final RestTemplate restTemplate = new RestTemplate(); | |
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | |
String result = response.getBody(); | |
// clean up results | |
int index = result.indexOf(","); | |
result = result.substring(3, index).replace("\"", ""); | |
return result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment