16
16
17
17
package com .google .cloud .translate .samples ;
18
18
19
+ import com .google .cloud .RetryParams ;
19
20
import com .google .cloud .translate .Detection ;
20
21
import com .google .cloud .translate .Language ;
21
22
import com .google .cloud .translate .Translate ;
22
23
import com .google .cloud .translate .Translate .TranslateOption ;
24
+ import com .google .cloud .translate .TranslateOptions ;
23
25
import com .google .cloud .translate .Translation ;
24
- import com .google .cloud .translate .testing .RemoteTranslateHelper ;
25
26
import com .google .common .collect .ImmutableList ;
26
27
27
28
import java .io .PrintStream ;
28
29
import java .util .List ;
29
30
30
31
public class TranslateText {
31
- private static final Translate TRANSLATE = RemoteTranslateHelper .create ().options ().service ();
32
-
33
32
/**
34
33
* Detect the language of input text.
35
34
*
36
35
* @param sourceText source text to be detected for language
37
36
* @param out print stream
38
37
*/
39
38
public static void detectLanguage (String sourceText , PrintStream out ) {
40
- List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
39
+ Translate translate = createTranslateService ();
40
+ List <Detection > detections = translate .detect (ImmutableList .of (sourceText ));
41
41
System .out .println ("Language(s) detected:" );
42
42
for (Detection detection : detections ) {
43
43
out .printf ("\t %s\n " , detection );
@@ -51,7 +51,8 @@ public static void detectLanguage(String sourceText, PrintStream out) {
51
51
* @param out print stream
52
52
*/
53
53
public static void translateText (String sourceText , PrintStream out ) {
54
- Translation translation = TRANSLATE .translate (sourceText );
54
+ Translate translate = createTranslateService ();
55
+ Translation translation = translate .translate (sourceText );
55
56
out .printf ("Source Text:\n \t %s\n " , sourceText );
56
57
out .printf ("Translated Text:\n \t %s\n " , translation .translatedText ());
57
58
}
@@ -70,10 +71,11 @@ public static void translateTextWithOptions(
70
71
String targetLang ,
71
72
PrintStream out ) {
72
73
74
+ Translate translate = createTranslateService ();
73
75
TranslateOption srcLang = TranslateOption .sourceLanguage (sourceLang );
74
76
TranslateOption tgtLang = TranslateOption .targetLanguage (targetLang );
75
77
76
- Translation translation = TRANSLATE .translate (sourceText , srcLang , tgtLang );
78
+ Translation translation = translate .translate (sourceText , srcLang , tgtLang );
77
79
out .printf ("Source Text:\n \t Lang: %s, Text: %s\n " , sourceLang , sourceText );
78
80
out .printf ("TranslatedText:\n \t Lang: %s, Text: %s\n " , targetLang , translation .translatedText ());
79
81
}
@@ -84,13 +86,40 @@ public static void translateTextWithOptions(
84
86
* @param out print stream
85
87
*/
86
88
public static void displaySupportedLanguages (PrintStream out ) {
87
- List <Language > languages = TRANSLATE .listSupportedLanguages ();
89
+ Translate translate = createTranslateService ();
90
+ List <Language > languages = translate .listSupportedLanguages ();
88
91
89
92
for (Language language : languages ) {
90
93
out .printf ("Name: %s, Code: %s\n " , language .name (), language .code ());
91
94
}
92
95
}
93
96
97
+ /**
98
+ * Create Google Translate API Service.
99
+ *
100
+ * @return Google Translate Service
101
+ */
102
+ public static Translate createTranslateService () {
103
+ TranslateOptions translateOption = TranslateOptions .builder ()
104
+ .retryParams (retryParams ())
105
+ .connectTimeout (60000 )
106
+ .readTimeout (60000 )
107
+ .build ();
108
+ return translateOption .service ();
109
+ }
110
+
111
+ /**
112
+ * Retry params for the Translate API.
113
+ */
114
+ private static RetryParams retryParams () {
115
+ return RetryParams .builder ()
116
+ .retryMaxAttempts (3 )
117
+ .maxRetryDelayMillis (30000 )
118
+ .totalRetryPeriodMillis (120000 )
119
+ .initialRetryDelayMillis (250 )
120
+ .build ();
121
+ }
122
+
94
123
public static void main (String [] args ) {
95
124
String command = args [0 ];
96
125
String text ;
0 commit comments