Skip to content

Commit de8c21d

Browse files
committed
Switch back to GET for translate and update docs
1 parent adeda0f commit de8c21d

3 files changed

Lines changed: 18 additions & 31 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static TranslateOption targetLanguage(String targetLanguage) {
8787
* Machine Translation. Possible values are {@code base} and {@code nmt}. Google Translate could
8888
* use a different model to translate your text, use {@link Translation#getModel()} to know
8989
* which model was used for translation. Please notice that you must be whitelisted to use this
90-
* option, otherwise it will be ignored.
90+
* option, otherwise translation will fail.
9191
*
9292
* @param model the language translation model
9393
*/
@@ -183,6 +183,8 @@ public static TranslateOption model(String model) {
183183
* @param texts the texts to translate
184184
* @return a list of objects containing information on the language translation, one for each
185185
* provided text, in order.
186+
* @throws TranslateException upon failure or if {@link TranslateOption#model(String)} is used by
187+
* a non-whitelisted user
186188
*/
187189
List<Translation> translate(List<String> texts, TranslateOption... options);
188190

@@ -202,6 +204,8 @@ public static TranslateOption model(String model) {
202204
*
203205
* @param text the text to translate
204206
* @return an object containing information on the language translation
207+
* @throws TranslateException upon failure or if {@link TranslateOption#model(String)} is used by
208+
* a non-whitelisted user
205209
*/
206210
Translation translate(String text, TranslateOption... options);
207211
}

google-cloud-translate/src/main/java/com/google/cloud/translate/Translation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public String getSourceLanguage() {
9090
* {@link Translate#translate(List, Translate.TranslateOption...)}.
9191
*
9292
* <p>Please notice that you must be whitelisted to use the
93-
* {@link Translate.TranslateOption#model(String)} option, otherwise it will be ignored.
93+
* {@link Translate.TranslateOption#model(String)} option, otherwise translation will fail.
9494
*/
9595
public String getModel() {
9696
return model;

google-cloud-translate/src/main/java/com/google/cloud/translate/spi/DefaultTranslateRpc.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,35 +118,18 @@ public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap)
118118
@Override
119119
public List<TranslationsResource> translate(List<String> texts, Map<Option, ?> optionMap) {
120120
try {
121-
final String sourceLanguage = SOURCE_LANGUAGE.getString(optionMap);
122-
String model = MODEL.getString(optionMap);
123-
ImmutableMap.Builder<String, Object> contentBuilder = ImmutableMap.builder();
124-
contentBuilder.put("target",
125-
firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()));
126-
contentBuilder.put("q", texts);
127-
if (sourceLanguage != null) {
128-
contentBuilder.put("source", sourceLanguage);
129-
}
130-
if (model != null) {
131-
contentBuilder.put("model", model);
132-
}
133-
HttpRequest httpRequest = translate.getRequestFactory()
134-
.buildPostRequest(buildTargetUrl(""),
135-
new JsonHttpContent(translate.getJsonFactory(), contentBuilder.build()))
136-
.setParser(translate.getObjectParser());
137-
List<TranslationsResource> translations =
138-
httpRequest.execute().parseAs(TranslationsListResponse.class).getTranslations();
139-
// TODO use REST apiary as soon as it supports POST
140-
// String targetLanguage =
141-
// firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage());
142-
// final String sourceLanguage = SOURCE_LANGUAGE.getString(optionMap);
143-
// List<TranslationsResource> translations =
144-
// translate.translations()
145-
// .list(texts, targetLanguage)
146-
// .setSource(sourceLanguage)
147-
// .setKey(options.getApiKey())
148-
// .execute()
149-
// .getTranslations();
121+
// TODO use POST as soon as usage of "model" causes error in non-whitelisted projects
122+
String targetLanguage =
123+
firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage());
124+
final String sourceLanguage = SOURCE_LANGUAGE.getString(optionMap);
125+
List<TranslationsResource> translations =
126+
translate.translations()
127+
.list(texts, targetLanguage)
128+
.setSource(sourceLanguage)
129+
.setKey(options.getApiKey())
130+
.set("model", MODEL.getString(optionMap))
131+
.execute()
132+
.getTranslations();
150133
return Lists.transform(
151134
translations != null ? translations : ImmutableList.<TranslationsResource>of(),
152135
new Function<TranslationsResource, TranslationsResource>() {

0 commit comments

Comments
 (0)