1717package com .google .cloud .translate .samples ;
1818
1919import java .util .List ;
20+ import java .io .PrintStream ;
2021import com .google .common .collect .ImmutableList ;
2122
2223import com .google .cloud .translate .Detection ;
@@ -32,43 +33,48 @@ public class TranslateText {
3233 /**
3334 * Detect the language of input text
3435 */
35- public static void detectLanguage (String sourceText ) {
36+ public static void detectLanguage (String sourceText , PrintStream out ) {
3637 List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
3738 System .out .println ("Language(s) detected:" );
3839 for (Detection detection : detections ) {
39- System . out .println ("\t " + detection );
40+ out .printf ("\t %s \n " , detection );
4041 }
4142 }
4243
4344 /**
4445 * Translates the source text in any language to english
4546 */
46- public static void translateText (String sourceText ) {
47+ public static void translateText (String sourceText , PrintStream out ) {
4748 Translation translation = TRANSLATE .translate (sourceText );
48- System . out .println ("Source Text:\n \t " + sourceText );
49- System . out .println ("Translated Text:\n \t " + translation .translatedText ());
49+ out .printf ("Source Text:\n \t %s \n " , sourceText );
50+ out .printf ("Translated Text:\n \t %s \n " , translation .translatedText ());
5051 }
5152
5253 /**
5354 * Translate the source text from source to target language
5455 */
55- public static void translateTextWithOptions (String sourceText , String sourceLang , String targetLang ) {
56+ public static void translateTextWithOptions (
57+ String sourceText ,
58+ String sourceLang ,
59+ String targetLang ,
60+ PrintStream out ) {
61+
5662 TranslateOption srcLang = TranslateOption .sourceLanguage (sourceLang );
5763 TranslateOption tgtLang = TranslateOption .targetLanguage (targetLang );
5864
5965 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 ());
66+ out .printf ("Source Text:\n \t Lang: %s, %s \n " , sourceLang , sourceText );
67+ out .printf ("TranslatedText:\n \t Lang: %s, %s \n " , targetLang , translation .translatedText ());
6268 }
6369
6470 /**
6571 * Displays a list of supported languages (codes).
6672 */
67- public static void displaySupportedLanguages () {
73+ public static void displaySupportedLanguages (PrintStream out ) {
6874 List <Language > languages = TRANSLATE .listSupportedLanguages ();
6975
7076 for (Language language : languages ) {
71- System . out .println ("Name: " + language .name () + ", Code: " + language .code ());
77+ out .printf ("Name: %s, Code: %s \n " , language .name (), language .code ());
7278 }
7379 }
7480
@@ -78,20 +84,20 @@ public static void main(String[] args) {
7884
7985 if (command .equals ("detect" )) {
8086 text = args [1 ];
81- TranslateText .detectLanguage (text );
87+ TranslateText .detectLanguage (text , System . out );
8288 }
8389 else if (command .equals ("translate" )) {
8490 text = args [1 ];
8591 try {
8692 String sourceLang = args [2 ];
8793 String targetLang = args [3 ];
88- TranslateText .translateTextWithOptions (text , sourceLang , targetLang );
94+ TranslateText .translateTextWithOptions (text , sourceLang , targetLang , System . out );
8995 } catch (ArrayIndexOutOfBoundsException ex ) {
90- TranslateText .translateText (text );
96+ TranslateText .translateText (text , System . out );
9197 }
9298 }
9399 else if (command .equals ("langsupport" )) {
94- TranslateText .displaySupportedLanguages ();
100+ TranslateText .displaySupportedLanguages (System . out );
95101 }
96102 }
97103}
0 commit comments