Skip to content

Commit 8f444e9

Browse files
committed
added source and target lang function
1 parent a33ff9a commit 8f444e9

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

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

+41-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.google.cloud.translate.Detection;
2323
import com.google.cloud.translate.Translate;
24+
import com.google.cloud.translate.Translate.TranslateOption;
25+
import com.google.cloud.translate.Language;
2426
import com.google.cloud.translate.Translation;
2527
import com.google.cloud.translate.testing.RemoteTranslateHelper;
2628

@@ -34,7 +36,7 @@ public static void detectLanguage(String sourceText) {
3436
List<Detection> detections = TRANSLATE.detect(ImmutableList.of(sourceText));
3537
System.out.println("Language(s) detected:");
3638
for(Detection detection : detections) {
37-
System.out.println("\t"+detection);
39+
System.out.println("\t" + detection);
3840
}
3941
}
4042

@@ -43,19 +45,53 @@ public static void detectLanguage(String sourceText) {
4345
*/
4446
public static void translateText(String sourceText) {
4547
Translation translation = TRANSLATE.translate(sourceText);
46-
System.out.println("Source Text:\n\t"+sourceText);
47-
System.out.println("Translated Text:\n\t"+translation.translatedText());
48+
System.out.println("Source Text:\n\t" + sourceText);
49+
System.out.println("Translated Text:\n\t" + translation.translatedText());
50+
}
51+
52+
/**
53+
* Translate the source text from source to target language
54+
*/
55+
public static void translateTextWithOptions(String sourceText, String sourceLang, String targetLang) {
56+
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
57+
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
58+
59+
Translation translation = TRANSLATE.translate(sourceText, srcLang, tgtLang);
60+
System.out.println("Source Text:\n\tLang: " + sourceLang + ", " + sourceText);
61+
System.out.println("TranslatedText:\n\tLang: " + targetLang + ", " + translation.translatedText());
62+
}
63+
64+
/**
65+
* Displays a list of supported languages (codes).
66+
*/
67+
public static void displaySupportedLanguages() {
68+
List<Language> languages = TRANSLATE.listSupportedLanguages();
69+
70+
for(Language language : languages) {
71+
System.out.println("Name: " + language.name() + ", Code: " + language.code());
72+
}
4873
}
4974

5075
public static void main(String[] args) {
5176
String command = args[0];
52-
String text = args[1];
77+
String text;
5378

5479
if(command.equals("detect")) {
80+
text = args[1];
5581
TranslateText.detectLanguage(text);
5682
}
5783
else if(command.equals("translate")) {
58-
TranslateText.translateText(text);
84+
text = args[1];
85+
try {
86+
String sourceLang = args[2];
87+
String targetLang = args[3];
88+
TranslateText.translateTextWithOptions(text, sourceLang, targetLang);
89+
} catch(ArrayIndexOutOfBoundsException ex) {
90+
TranslateText.translateText(text);
91+
}
92+
}
93+
else if(command.equals("langsupport")) {
94+
TranslateText.displaySupportedLanguages();
5995
}
6096
}
6197
}

0 commit comments

Comments
 (0)