2121
2222import com .google .cloud .translate .Detection ;
2323import com .google .cloud .translate .Translate ;
24+ import com .google .cloud .translate .Translate .TranslateOption ;
25+ import com .google .cloud .translate .Language ;
2426import com .google .cloud .translate .Translation ;
2527import 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 \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+ }
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