use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateTest method testTranslateTextListWithModel.
@Test
public void testTranslateTextListWithModel() {
List<Translation> translations = TRANSLATE.translate(ImmutableList.of("Hola", "Hallo"), TranslateOption.model("nmt"));
Translation translation = translations.get(0);
assertEquals("Hello", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
assertEquals("nmt", translation.getModel());
translation = translations.get(1);
assertEquals("Hello", translation.getTranslatedText());
assertEquals("de", translation.getSourceLanguage());
assertEquals("nmt", translation.getModel());
}
use of com.google.cloud.translate.Translation in project java-docs-samples by GoogleCloudPlatform.
the class Translate method translateText.
/**
* Translate the source text from source to target language.
*
* @param sourceText source text to be translated
* @param sourceLang source language of the text
* @param targetLang target language of translated text
* @return source text translated into target language.
*/
public static String translateText(String sourceText, String sourceLang, String targetLang) {
if (Strings.isNullOrEmpty(sourceLang) || Strings.isNullOrEmpty(targetLang) || sourceLang.equals(targetLang)) {
return sourceText;
}
com.google.cloud.translate.Translate translate = createTranslateService();
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
Translation translation = translate.translate(sourceText, srcLang, tgtLang);
return translation.getTranslatedText();
}
use of com.google.cloud.translate.Translation in project java-docs-samples by GoogleCloudPlatform.
the class TranslateText method translateTextWithOptionsAndModel.
/**
* Translate the source text from source to target language.
* Make sure that your project is whitelisted.
*
* @param sourceText source text to be translated
* @param sourceLang source language of the text
* @param targetLang target language of translated text
* @param out print stream
*/
public static void translateTextWithOptionsAndModel(String sourceText, String sourceLang, String targetLang, PrintStream out) {
Translate translate = createTranslateService();
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
// Use translate `model` parameter with `base` and `nmt` options.
TranslateOption model = TranslateOption.model("nmt");
Translation translation = translate.translate(sourceText, srcLang, tgtLang, model);
out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.getTranslatedText());
}
use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testTranslateTextWithModel.
@Test
public void testTranslateTextWithModel() {
// [START translate_text_with_model]
Translation translation = translate.translate("Hola Mundo!", Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de"), // Use "base" for standard edition, "nmt" for the premium model.
Translate.TranslateOption.model("nmt"));
System.out.printf("TranslatedText:\nText: %s\n", translation.getTranslatedText());
// [END translate_text_with_model]
assertEquals("Hallo Welt!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testTranslateTextListWithOptions.
@Test
public void testTranslateTextListWithOptions() {
// SNIPPET translateTextsWithOptions
List<String> texts = new LinkedList<>();
texts.add("¡Hola Mundo!");
List<Translation> translations = translate.translate(texts, Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de"));
// SNIPPET translateTextsWithOptions
Translation translation = translations.get(0);
assertEquals("Hallo Welt!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
Aggregations