Google translation Apis Code-
1. Please follow the steps mentioned in this link to enable access to google translation Apis.
Reference Documentation - https://cloud.google.com/translate/docs/reference/libraries
We need to follow above steps to generate Json File, That will be used in our code for authentication. You can activate the trial account to run the POC for this.
2. Create a maven j2ee project.
Maven Dependency for google translation API:-
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
<version>1.14.0</version>
</dependency>
JAVA CODE:-
Contoller Code:- Below Method accepts the json key value data and write the translated text into a file.
@RequestMapping(value = "/translateText", method = RequestMethod.POST)
public void getTranslatedText(@RequestBody LinkedHashMap<String, String> data, HttpServletRequest request,
@RequestParam("lang") String lang) throws Exception {
googleAuthTranslationService.translateText(data, lang);
}
Service Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;com.google.guava
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.inject.Named;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
@Named
public class GoogleAuthTranslationService {
private Translate translationServiceClient;
/** This Method accepts key value pair of data that needs translation, key will be same. it will translate the value into targetLanguage and put it into a new map. Then it will write the Key Value Data into .txt file.
**/
public void translateText(LinkedHashMap<String, String> data, String targetLang) throws Exception {
// Instantiates a client
Translate translate = getTranslationServiceClient();
// The text to translate
LinkedHashMap<String, String> translatedMap = new LinkedHashMap<String, String>();
for (Map.Entry<String, String> values : data.entrySet()) {
String text = values.getValue();
// Translates some text into target language
Translation translation = translate.translate(text, TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(targetLang));
String translatedText = translation.getTranslatedText();
translatedMap.put(values.getKey(), translatedText);
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translatedText);
}
write(translatedMap, targetLang);
}
protected Translate getTranslationServiceClient() throws IOException {
if (translationServiceClient == null) {
synchronized (this) {
if (translationServiceClient == null) {
/**you need to provide the path of google translation key downloaded from developer console. **/
try (InputStream is = new FileInputStream(
new File("/home/Certification-googletranslation.json"))) {
final GoogleCredentials myCredentials = GoogleCredentials.fromStream(is);
translationServiceClient = TranslateOptions.newBuilder().setCredentials(myCredentials).build()
.getService();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
return translationServiceClient;
}
/** This methods writes the translated code into a txt file.
**/
public void write(Map<String, String> data, String lang) {
String FILENAME = "/home/translation/translated_" + lang + "_" +
".txt";
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
for (Map.Entry<String, String> values : data.entrySet()) {
bw.write(values.getKey() + "=" + values.getValue() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Notes:- If any exception comes running this code. Check the other "com.google.guava" dependencies version in your project. Version of this jar may be conflicting Try excluding or removing this jar from other transitive dependencies if any.
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
Happy Google Translation API Coding :)
No comments:
Post a Comment