Skip to content

Commit a28e440

Browse files
committed
added function to create translate service
1 parent 001f400 commit a28e440

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java

+36-7
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@
1616

1717
package com.google.cloud.translate.samples;
1818

19+
import com.google.cloud.RetryParams;
1920
import com.google.cloud.translate.Detection;
2021
import com.google.cloud.translate.Language;
2122
import com.google.cloud.translate.Translate;
2223
import com.google.cloud.translate.Translate.TranslateOption;
24+
import com.google.cloud.translate.TranslateOptions;
2325
import com.google.cloud.translate.Translation;
24-
import com.google.cloud.translate.testing.RemoteTranslateHelper;
2526
import com.google.common.collect.ImmutableList;
2627

2728
import java.io.PrintStream;
2829
import java.util.List;
2930

3031
public class TranslateText {
31-
private static final Translate TRANSLATE = RemoteTranslateHelper.create().options().service();
32-
3332
/**
3433
* Detect the language of input text.
3534
*
3635
* @param sourceText source text to be detected for language
3736
* @param out print stream
3837
*/
3938
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));
4141
System.out.println("Language(s) detected:");
4242
for (Detection detection : detections) {
4343
out.printf("\t%s\n", detection);
@@ -51,7 +51,8 @@ public static void detectLanguage(String sourceText, PrintStream out) {
5151
* @param out print stream
5252
*/
5353
public static void translateText(String sourceText, PrintStream out) {
54-
Translation translation = TRANSLATE.translate(sourceText);
54+
Translate translate = createTranslateService();
55+
Translation translation = translate.translate(sourceText);
5556
out.printf("Source Text:\n\t%s\n", sourceText);
5657
out.printf("Translated Text:\n\t%s\n", translation.translatedText());
5758
}
@@ -70,10 +71,11 @@ public static void translateTextWithOptions(
7071
String targetLang,
7172
PrintStream out) {
7273

74+
Translate translate = createTranslateService();
7375
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
7476
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
7577

76-
Translation translation = TRANSLATE.translate(sourceText, srcLang, tgtLang);
78+
Translation translation = translate.translate(sourceText, srcLang, tgtLang);
7779
out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
7880
out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.translatedText());
7981
}
@@ -84,13 +86,40 @@ public static void translateTextWithOptions(
8486
* @param out print stream
8587
*/
8688
public static void displaySupportedLanguages(PrintStream out) {
87-
List<Language> languages = TRANSLATE.listSupportedLanguages();
89+
Translate translate = createTranslateService();
90+
List<Language> languages = translate.listSupportedLanguages();
8891

8992
for (Language language : languages) {
9093
out.printf("Name: %s, Code: %s\n", language.name(), language.code());
9194
}
9295
}
9396

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+
94123
public static void main(String[] args) {
95124
String command = args[0];
96125
String text;

0 commit comments

Comments
 (0)