Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5bdc8db

Browse filesBrowse files
authored
Add Translate.translate methods and tests (#1166)
1 parent 21d6bb9 commit 5bdc8db
Copy full SHA for 5bdc8db

4 files changed

+165Lines changed: 165 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎gcloud-java-translate/src/main/java/com/google/cloud/translate/Translate.java‎

Copy file name to clipboardExpand all lines: gcloud-java-translate/src/main/java/com/google/cloud/translate/Translate.java
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ public static LanguageListOption targetLanguage(String targetLanguage) {
5151
}
5252
}
5353

54+
/**
55+
* Class for specifying translate options.
56+
*/
57+
class TranslateOption extends Option {
58+
59+
private static final long serialVersionUID = 1347871763933507106L;
60+
61+
private TranslateOption(TranslateRpc.Option rpcOption, String value) {
62+
super(rpcOption, value);
63+
}
64+
65+
/**
66+
* Returns an option for setting the source language. If not provided, Google Translate will try
67+
* to detect the language of the text to translate.
68+
*
69+
* @param sourceLanguage the source language code
70+
*/
71+
public static TranslateOption sourceLanguage(String sourceLanguage) {
72+
return new TranslateOption(TranslateRpc.Option.SOURCE_LANGUAGE, sourceLanguage);
73+
}
74+
75+
/**
76+
* Returns an option for setting the target language. If this option is not provided, the value
77+
* returned by {@link TranslateOptions#targetLanguage()} is used.
78+
*
79+
* @param targetLanguage the target language code
80+
*/
81+
public static TranslateOption targetLanguage(String targetLanguage) {
82+
return new TranslateOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage);
83+
}
84+
}
85+
5486
/**
5587
* Returns the list of languages supported by Google Translate. If
5688
* {@link LanguageListOption#targetLanguage(String)} is provided, {@link Language#name()} values
@@ -112,4 +144,48 @@ public static LanguageListOption targetLanguage(String targetLanguage) {
112144
* }</pre>
113145
*/
114146
Detection detect(String text);
147+
148+
/**
149+
* Translates the provided texts.
150+
*
151+
* <p>Example of translating some texts:
152+
* <pre> {@code
153+
* List<String> texts = new LinkedList<>();
154+
* texts.add("Hello, World!");
155+
* texts.add("¡Hola Mundo!");
156+
* List<Translation> translations = translate.translate(texts);
157+
* }</pre>
158+
*
159+
* <p>Example of translating some texts, specifying source and target language:
160+
* <pre> {@code
161+
* List<String> texts = new LinkedList<>();
162+
* texts.add("¡Hola Mundo!");
163+
* List<Translation> translations = translate.translate(texts,
164+
* TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));
165+
* }</pre>
166+
*
167+
* @param texts the texts to translate
168+
* @return a list of objects containing information on the language translation, one for each
169+
* provided text, in order.
170+
*/
171+
List<Translation> translate(List<String> texts, TranslateOption... options);
172+
173+
/**
174+
* Translates the provided texts.
175+
*
176+
* <p>Example of translating a text:
177+
* <pre> {@code
178+
* Translation translation = translate.translate("¡Hola Mundo!");
179+
* }</pre>
180+
*
181+
* <p>Example of translating a text, specifying source and target language:
182+
* <pre> {@code
183+
* Translation translation = translate.translate("¡Hola Mundo!",
184+
* TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));
185+
* }</pre>
186+
*
187+
* @param text the text to translate
188+
* @return an object containing information on the language translation
189+
*/
190+
Translation translate(String text, TranslateOption... options);
115191
}
Collapse file

‎gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java‎

Copy file name to clipboardExpand all lines: gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.google.api.services.translate.model.DetectionsResourceItems;
2424
import com.google.api.services.translate.model.LanguagesResource;
25+
import com.google.api.services.translate.model.TranslationsResource;
2526
import com.google.cloud.BaseService;
2627
import com.google.cloud.RetryHelper.RetryHelperException;
2728
import com.google.cloud.translate.spi.TranslateRpc;
@@ -102,6 +103,26 @@ public Detection detect(String text) {
102103
return detect(Collections.singletonList(text)).get(0);
103104
}
104105

106+
@Override
107+
public List<Translation> translate(final List<String> texts, final TranslateOption... options) {
108+
try {
109+
return Lists.transform(runWithRetries(new Callable<List<TranslationsResource>>() {
110+
@Override
111+
public List<TranslationsResource> call() {
112+
return translateRpc.translate(texts, optionMap(options));
113+
}
114+
}, options().retryParams(), EXCEPTION_HANDLER, options().clock()),
115+
Translation.FROM_PB_FUNCTION);
116+
} catch (RetryHelperException e) {
117+
throw TranslateException.translateAndThrow(e);
118+
}
119+
}
120+
121+
@Override
122+
public Translation translate(String text, TranslateOption... options) {
123+
return translate(Collections.singletonList(text), options).get(0);
124+
}
125+
105126
private Map<TranslateRpc.Option, ?> optionMap(Option... options) {
106127
Map<TranslateRpc.Option, Object> optionMap = Maps.newEnumMap(TranslateRpc.Option.class);
107128
for (Option option : options) {
Collapse file

‎gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateOptions.java‎

Copy file name to clipboardExpand all lines: gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateOptions.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
import com.google.cloud.AuthCredentials;
2222
import com.google.cloud.HttpServiceOptions;
23+
import com.google.cloud.translate.Translate.TranslateOption;
2324
import com.google.cloud.translate.spi.DefaultTranslateRpc;
2425
import com.google.cloud.translate.spi.TranslateRpc;
2526
import com.google.cloud.translate.spi.TranslateRpcFactory;
2627
import com.google.common.collect.ImmutableSet;
2728

29+
import java.util.List;
2830
import java.util.Locale;
2931
import java.util.Set;
3032

@@ -96,6 +98,10 @@ public Builder authCredentials(AuthCredentials authCredentials) {
9698

9799
/**
98100
* Sets the code for the default target language. If not set, english ({@code en}) is used.
101+
* {@link Translate#translate(List, TranslateOption...)} and
102+
* {@link Translate#translate(String, TranslateOption...)} calls will use this
103+
* value unless a {@link TranslateOption#targetLanguage(String)} option is explicitly
104+
* provided.
99105
*
100106
* @return the builder
101107
*/
Collapse file

‎gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java‎

Copy file name to clipboardExpand all lines: gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import com.google.api.services.translate.model.DetectionsResourceItems;
2323
import com.google.api.services.translate.model.LanguagesResource;
24+
import com.google.api.services.translate.model.TranslationsResource;
2425
import com.google.cloud.RetryParams;
2526
import com.google.cloud.translate.Translate.LanguageListOption;
27+
import com.google.cloud.translate.Translate.TranslateOption;
2628
import com.google.cloud.translate.spi.TranslateRpc;
2729
import com.google.cloud.translate.spi.TranslateRpcFactory;
2830
import com.google.common.collect.ImmutableList;
@@ -63,6 +65,12 @@ public class TranslateImplTest {
6365
new DetectionsResourceItems().setLanguage("en").setConfidence(0.8F);
6466
private static final Detection DETECTION1 = Detection.fromPb(DETECTION1_PB);
6567
private static final Detection DETECTION2 = Detection.fromPb(DETECTION2_PB);
68+
private static final TranslationsResource TRANSLATION1_PB =
69+
new TranslationsResource().setTranslatedText("Hello World!").setDetectedSourceLanguage("es");
70+
private static final TranslationsResource TRANSLATION2_PB =
71+
new TranslationsResource().setTranslatedText("Hello World!").setDetectedSourceLanguage("de");
72+
private static final Translation TRANSLATION1 = Translation.fromPb(TRANSLATION1_PB);
73+
private static final Translation TRANSLATION2 = Translation.fromPb(TRANSLATION2_PB);
6674

6775
// Empty TranslateRpc options
6876
private static final Map<TranslateRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
@@ -73,6 +81,15 @@ public class TranslateImplTest {
7381
private static final Map<TranslateRpc.Option, ?> LANGUAGE_LIST_OPTIONS = ImmutableMap.of(
7482
TranslateRpc.Option.TARGET_LANGUAGE, LANGUAGE_LIST_OPTION.value());
7583

84+
// Translate options
85+
private static final TranslateOption TARGET_LANGUAGE_OPTION =
86+
TranslateOption.targetLanguage("en");
87+
private static final TranslateOption SOURCE_LANGUAGE_OPTION =
88+
TranslateOption.sourceLanguage("de");
89+
private static final Map<TranslateRpc.Option, ?> TRANSLATE_OPTIONS = ImmutableMap.of(
90+
TranslateRpc.Option.TARGET_LANGUAGE, TARGET_LANGUAGE_OPTION.value(),
91+
TranslateRpc.Option.SOURCE_LANGUAGE, SOURCE_LANGUAGE_OPTION.value());
92+
7693
private TranslateOptions options;
7794
private TranslateRpcFactory rpcFactoryMock;
7895
private TranslateRpc translateRpcMock;
@@ -249,6 +266,51 @@ public void testDetectVarargNoDetection() {
249266
translate.detect(text1, text2);
250267
}
251268

269+
@Test
270+
public void testTranslate() {
271+
String text = "¡Hola Mundo!";
272+
EasyMock.expect(translateRpcMock.translate(ImmutableList.of(text), EMPTY_RPC_OPTIONS))
273+
.andReturn(ImmutableList.of(TRANSLATION1_PB));
274+
EasyMock.replay(translateRpcMock);
275+
initializeService();
276+
assertEquals(TRANSLATION1, translate.translate(text));
277+
}
278+
279+
@Test
280+
public void testTranslateWithOptions() {
281+
String text = "Hallo Welt!";
282+
EasyMock.expect(translateRpcMock.translate(ImmutableList.of(text), TRANSLATE_OPTIONS))
283+
.andReturn(ImmutableList.of(TRANSLATION2_PB));
284+
EasyMock.replay(translateRpcMock);
285+
initializeService();
286+
assertEquals(TRANSLATION2,
287+
translate.translate(text, TARGET_LANGUAGE_OPTION, SOURCE_LANGUAGE_OPTION));
288+
}
289+
290+
@Test
291+
public void testTranslateList() {
292+
String text1 = "¡Hola Mundo!";
293+
String text2 = "Hallo Welt!";
294+
List<String> texts = ImmutableList.of(text1, text2);
295+
EasyMock.expect(translateRpcMock.translate(texts, EMPTY_RPC_OPTIONS))
296+
.andReturn(ImmutableList.of(TRANSLATION1_PB, TRANSLATION2_PB));
297+
EasyMock.replay(translateRpcMock);
298+
initializeService();
299+
assertEquals(ImmutableList.of(TRANSLATION1, TRANSLATION2), translate.translate(texts));
300+
}
301+
302+
@Test
303+
public void testTranslateListWithOptions() {
304+
String text = "Hallo Welt!";
305+
List<String> texts = ImmutableList.of(text);
306+
EasyMock.expect(translateRpcMock.translate(texts, TRANSLATE_OPTIONS))
307+
.andReturn(ImmutableList.of(TRANSLATION2_PB));
308+
EasyMock.replay(translateRpcMock);
309+
initializeService();
310+
assertEquals(ImmutableList.of(TRANSLATION2),
311+
translate.translate(texts, TARGET_LANGUAGE_OPTION, SOURCE_LANGUAGE_OPTION));
312+
}
313+
252314
@Test
253315
public void testRetryableException() {
254316
EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.