use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testTranslateTextWithOptions.
@Test
public void testTranslateTextWithOptions() {
Translation translation = translateSnippets.translateTextWithOptions();
assertEquals("Hallo Welt!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
use of com.google.cloud.translate.Translation in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartSample method main.
public static void main(String... args) throws Exception {
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
// The text to translate
String text = "Hello, world!";
// Translates some text into Russian
Translation translation = translate.translate(text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
use of com.google.cloud.translate.Translation 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.Translation in project java-docs-samples by GoogleCloudPlatform.
the class TranslateText method translateText.
/**
* Translates the source text in any language to English.
*
* @param sourceText source text to be translated
* @param out print stream
*/
public static void translateText(String sourceText, PrintStream out) {
Translate translate = createTranslateService();
Translation translation = translate.translate(sourceText);
out.printf("Source Text:\n\t%s\n", sourceText);
out.printf("Translated Text:\n\t%s\n", translation.getTranslatedText());
}
use of com.google.cloud.translate.Translation in project structr by structr.
the class TranslateFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
if (arrayHasLengthAndAllElementsNotNull(sources, 3)) {
try {
final String gctAPIKey = TranslationModule.TranslationAPIKey.getValue();
if (gctAPIKey == null) {
logger.error("Google Cloud Translation API Key not configured in structr.conf");
return "";
}
final String text = sources[0].toString();
final String sourceLanguage = sources[1].toString();
final String targetLanguage = sources[2].toString();
final Translate translate = TranslateOptions.builder().apiKey(gctAPIKey).build().service();
Translation translation = translate.translate(text, TranslateOption.sourceLanguage(sourceLanguage), TranslateOption.targetLanguage(targetLanguage));
return translation.translatedText();
} catch (TranslateException te) {
logger.error("TranslateException: {}", te.getLocalizedMessage());
} catch (Throwable t) {
logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
}
return "";
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return usage(ctx.isJavaScriptContext());
}
Aggregations