21
21
22
22
import com .google .cloud .translate .Detection ;
23
23
import com .google .cloud .translate .Translate ;
24
+ import com .google .cloud .translate .Translate .TranslateOption ;
25
+ import com .google .cloud .translate .Language ;
24
26
import com .google .cloud .translate .Translation ;
25
27
import com .google .cloud .translate .testing .RemoteTranslateHelper ;
26
28
@@ -34,7 +36,7 @@ public static void detectLanguage(String sourceText) {
34
36
List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
35
37
System .out .println ("Language(s) detected:" );
36
38
for (Detection detection : detections ) {
37
- System .out .println ("\t " + detection );
39
+ System .out .println ("\t " + detection );
38
40
}
39
41
}
40
42
@@ -43,19 +45,53 @@ public static void detectLanguage(String sourceText) {
43
45
*/
44
46
public static void translateText (String sourceText ) {
45
47
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 \t Lang: " + sourceLang + ", " + sourceText );
61
+ System .out .println ("TranslatedText:\n \t Lang: " + 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
+ }
48
73
}
49
74
50
75
public static void main (String [] args ) {
51
76
String command = args [0 ];
52
- String text = args [ 1 ] ;
77
+ String text ;
53
78
54
79
if (command .equals ("detect" )) {
80
+ text = args [1 ];
55
81
TranslateText .detectLanguage (text );
56
82
}
57
83
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 ();
59
95
}
60
96
}
61
97
}
0 commit comments