17
17
package com .google .cloud .translate .samples ;
18
18
19
19
import java .util .List ;
20
+ import java .io .PrintStream ;
20
21
import com .google .common .collect .ImmutableList ;
21
22
22
23
import com .google .cloud .translate .Detection ;
@@ -32,43 +33,48 @@ public class TranslateText {
32
33
/**
33
34
* Detect the language of input text
34
35
*/
35
- public static void detectLanguage (String sourceText ) {
36
+ public static void detectLanguage (String sourceText , PrintStream out ) {
36
37
List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
37
38
System .out .println ("Language(s) detected:" );
38
39
for (Detection detection : detections ) {
39
- System . out .println ("\t " + detection );
40
+ out .printf ("\t %s \n " , detection );
40
41
}
41
42
}
42
43
43
44
/**
44
45
* Translates the source text in any language to english
45
46
*/
46
- public static void translateText (String sourceText ) {
47
+ public static void translateText (String sourceText , PrintStream out ) {
47
48
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 ());
50
51
}
51
52
52
53
/**
53
54
* Translate the source text from source to target language
54
55
*/
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
+
56
62
TranslateOption srcLang = TranslateOption .sourceLanguage (sourceLang );
57
63
TranslateOption tgtLang = TranslateOption .targetLanguage (targetLang );
58
64
59
65
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 ());
62
68
}
63
69
64
70
/**
65
71
* Displays a list of supported languages (codes).
66
72
*/
67
- public static void displaySupportedLanguages () {
73
+ public static void displaySupportedLanguages (PrintStream out ) {
68
74
List <Language > languages = TRANSLATE .listSupportedLanguages ();
69
75
70
76
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 ());
72
78
}
73
79
}
74
80
@@ -78,20 +84,20 @@ public static void main(String[] args) {
78
84
79
85
if (command .equals ("detect" )) {
80
86
text = args [1 ];
81
- TranslateText .detectLanguage (text );
87
+ TranslateText .detectLanguage (text , System . out );
82
88
}
83
89
else if (command .equals ("translate" )) {
84
90
text = args [1 ];
85
91
try {
86
92
String sourceLang = args [2 ];
87
93
String targetLang = args [3 ];
88
- TranslateText .translateTextWithOptions (text , sourceLang , targetLang );
94
+ TranslateText .translateTextWithOptions (text , sourceLang , targetLang , System . out );
89
95
} catch (ArrayIndexOutOfBoundsException ex ) {
90
- TranslateText .translateText (text );
96
+ TranslateText .translateText (text , System . out );
91
97
}
92
98
}
93
99
else if (command .equals ("langsupport" )) {
94
- TranslateText .displaySupportedLanguages ();
100
+ TranslateText .displaySupportedLanguages (System . out );
95
101
}
96
102
}
97
103
}
0 commit comments