Skip to content

Commit 704264f

Browse files
committed
added function without and with model
1 parent a3dcbaf commit 704264f

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ public static void translateText(String sourceText, PrintStream out) {
6161

6262
/**
6363
* Translate the source text from source to target language.
64+
* Make sure that your project is whitelisted.
6465
*
6566
* @param sourceText source text to be translated
6667
* @param sourceLang source language of the text
6768
* @param targetLang target language of translated text
6869
* @param out print stream
6970
*/
70-
public static void translateTextWithOptions(
71+
public static void translateTextWithOptionsAndModel(
7172
String sourceText,
7273
String sourceLang,
7374
String targetLang,
@@ -85,6 +86,30 @@ public static void translateTextWithOptions(
8586
out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.translatedText());
8687
}
8788

89+
90+
/**
91+
* Translate the source text from source to target language.
92+
*
93+
* @param sourceText source text to be translated
94+
* @param sourceLang source language of the text
95+
* @param targetLang target language of translated text
96+
* @param out print stream
97+
*/
98+
public static void translateTextWithOptions(
99+
String sourceText,
100+
String sourceLang,
101+
String targetLang,
102+
PrintStream out) {
103+
104+
Translate translate = createTranslateService();
105+
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
106+
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
107+
108+
Translation translation = translate.translate(sourceText, srcLang, tgtLang);
109+
out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
110+
out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.translatedText());
111+
}
112+
88113
/**
89114
* Displays a list of supported languages and codes.
90115
*

0 commit comments

Comments
 (0)