本文整理汇总了Java中com.google.cloud.translate.Translate.TranslateOption.targetLanguage方法的典型用法代码示例。如果您正苦于以下问题:Java TranslateOption.targetLanguage方法的具体用法?Java TranslateOption.targetLanguage怎么用?Java TranslateOption.targetLanguage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.cloud.translate.Translate.TranslateOption
的用法示例。
在下文中一共展示了TranslateOption.targetLanguage方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: translateText
import com.google.cloud.translate.Translate.TranslateOption; //导入方法依赖的package包/类
/**
* 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();
}
示例2: translateTextWithOptionsAndModel
import com.google.cloud.translate.Translate.TranslateOption; //导入方法依赖的package包/类
/**
* 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());
}
示例3: translateTextWithOptions
import com.google.cloud.translate.Translate.TranslateOption; //导入方法依赖的package包/类
/**
* 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());
}