16
16
17
17
package com .google .cloud .translate .samples ;
18
18
19
- import java .util .List ;
20
- import java .io .PrintStream ;
21
- import com .google .common .collect .ImmutableList ;
22
-
23
19
import com .google .cloud .translate .Detection ;
20
+ import com .google .cloud .translate .Language ;
24
21
import com .google .cloud .translate .Translate ;
25
22
import com .google .cloud .translate .Translate .TranslateOption ;
26
- import com .google .cloud .translate .Language ;
27
23
import com .google .cloud .translate .Translation ;
28
24
import com .google .cloud .translate .testing .RemoteTranslateHelper ;
25
+ import com .google .common .collect .ImmutableList ;
26
+
27
+ import java .io .PrintStream ;
28
+ import java .util .List ;
29
29
30
30
public class TranslateText {
31
31
private static final Translate TRANSLATE = RemoteTranslateHelper .create ().options ().service ();
32
32
33
33
/**
34
- * Detect the language of input text
34
+ * Detect the language of input text.
35
35
*/
36
36
public static void detectLanguage (String sourceText , PrintStream out ) {
37
37
List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
38
38
System .out .println ("Language(s) detected:" );
39
- for (Detection detection : detections ) {
39
+ for (Detection detection : detections ) {
40
40
out .printf ("\t %s\n " , detection );
41
41
}
42
42
}
43
43
44
44
/**
45
- * Translates the source text in any language to english
45
+ * Translates the source text in any language to english.
46
46
*/
47
47
public static void translateText (String sourceText , PrintStream out ) {
48
48
Translation translation = TRANSLATE .translate (sourceText );
@@ -51,7 +51,7 @@ public static void translateText(String sourceText, PrintStream out) {
51
51
}
52
52
53
53
/**
54
- * Translate the source text from source to target language
54
+ * Translate the source text from source to target language.
55
55
*/
56
56
public static void translateTextWithOptions (
57
57
String sourceText ,
@@ -73,7 +73,7 @@ public static void translateTextWithOptions(
73
73
public static void displaySupportedLanguages (PrintStream out ) {
74
74
List <Language > languages = TRANSLATE .listSupportedLanguages ();
75
75
76
- for (Language language : languages ) {
76
+ for (Language language : languages ) {
77
77
out .printf ("Name: %s, Code: %s\n " , language .name (), language .code ());
78
78
}
79
79
}
@@ -82,21 +82,19 @@ public static void main(String[] args) {
82
82
String command = args [0 ];
83
83
String text ;
84
84
85
- if (command .equals ("detect" )) {
85
+ if (command .equals ("detect" )) {
86
86
text = args [1 ];
87
87
TranslateText .detectLanguage (text , System .out );
88
- }
89
- else if (command .equals ("translate" )) {
88
+ } else if (command .equals ("translate" )) {
90
89
text = args [1 ];
91
90
try {
92
91
String sourceLang = args [2 ];
93
92
String targetLang = args [3 ];
94
93
TranslateText .translateTextWithOptions (text , sourceLang , targetLang , System .out );
95
- } catch (ArrayIndexOutOfBoundsException ex ) {
94
+ } catch (ArrayIndexOutOfBoundsException ex ) {
96
95
TranslateText .translateText (text , System .out );
97
96
}
98
- }
99
- else if (command .equals ("langsupport" )) {
97
+ } else if (command .equals ("langsupport" )) {
100
98
TranslateText .displaySupportedLanguages (System .out );
101
99
}
102
100
}
0 commit comments