use of com.google.cloud.translate.Translate.TranslateOption in project google-cloud-java by GoogleCloudPlatform.
the class TranslateTest method testTranslateOptions.
@Test
public void testTranslateOptions() {
// target language
TranslateOption translateOption = TranslateOption.targetLanguage(LANGUAGE);
assertEquals(TranslateRpc.Option.TARGET_LANGUAGE, translateOption.getRpcOption());
assertEquals(LANGUAGE, translateOption.getValue());
// source language
translateOption = TranslateOption.sourceLanguage(LANGUAGE);
assertEquals(TranslateRpc.Option.SOURCE_LANGUAGE, translateOption.getRpcOption());
assertEquals(LANGUAGE, translateOption.getValue());
}
use of com.google.cloud.translate.Translate.TranslateOption in project java-docs-samples by GoogleCloudPlatform.
the class TranslateText method translateTextWithOptions.
/**
* 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
* @param out print stream
*/
public static void translateTextWithOptions(String sourceText, String sourceLang, String targetLang, PrintStream out) {
Translate translate = createTranslateService();
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
Translation translation = translate.translate(sourceText, srcLang, tgtLang);
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.Translate.TranslateOption 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.Translate.TranslateOption 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());
}
Aggregations