Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,14 @@ import com.google.cloud.translate.Translation;
Translate translate = TranslateOptions.defaultInstance().service();

Detection detection = translate.detect("Hola");
String detectedLanguage = detection.language();
String detectedLanguage = detection.getLanguage();

Translation translation = translate.translate(
"World",
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(detectedLanguage));

System.out.printf("Hola %s%n", translation.translatedText());
System.out.printf("Hola %s%n", translation.getTranslatedText());
```

Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ key.
that uses the `RemoteTranslateHelper` to list supported languages.
```java
RemoteTranslateHelper translateHelper = RemoteTranslateHelper.create(PROJECT_ID, API_KEY);
Translate translate = translateHelper.options().service();
Translate translate = translateHelper.getOptions().service();
List<Language> languages = translate.listSupportedLanguages();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public static void main(String... args) {

// Detect the language of some text
Detection detection = translate.detect("Hola");
String detectedLanguage = detection.language();
String detectedLanguage = detection.getLanguage();

// Translate some text
Translation translation = translate.translate(
"World",
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(detectedLanguage));

System.out.printf("Hola %s%n", translation.translatedText());
System.out.printf("Hola %s%n", translation.getTranslatedText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ public class ITTranslateSnippets {
@BeforeClass
public static void beforeClass() {
RemoteTranslateHelper helper = RemoteTranslateHelper.create();
translateSnippets = new TranslateSnippets(helper.options().service());
translateSnippets = new TranslateSnippets(helper.getOptions().service());
}

@Test
public void testListSupportedLanguages() {
Set<String> supportedLanguages = new HashSet<>();
List<Language> languages = translateSnippets.listSupportedLanguages();
for (Language language : languages) {
supportedLanguages.add(language.code());
assertNotNull(language.name());
supportedLanguages.add(language.getCode());
assertNotNull(language.getName());
}
for (String code : LANGUAGES) {
assertTrue(supportedLanguages.contains(code));
Expand All @@ -68,8 +68,8 @@ public void testListSupportedLanguagesWithTarget() {
Set<String> supportedLanguages = new HashSet<>();
List<Language> languages = translateSnippets.listSupportedLanguagesWithTarget();
for (Language language : languages) {
supportedLanguages.add(language.code());
assertNotNull(language.name());
supportedLanguages.add(language.getCode());
assertNotNull(language.getName());
}
for (String code : LANGUAGES) {
assertTrue(supportedLanguages.contains(code));
Expand All @@ -80,48 +80,48 @@ public void testListSupportedLanguagesWithTarget() {
public void testDetectLanguageOfTexts() {
List<Detection> detections = translateSnippets.detectLanguageOfTexts();
Detection detection = detections.get(0);
assertEquals("en", detection.language());
assertEquals("en", detection.getLanguage());
detection = detections.get(1);
assertEquals("es", detection.language());
assertEquals("es", detection.getLanguage());
}

@Test
public void testDetectLanguageOfTextList() {
List<Detection> detections = translateSnippets.detectLanguageOfTextList();
Detection detection = detections.get(0);
assertEquals("en", detection.language());
assertEquals("en", detection.getLanguage());
detection = detections.get(1);
assertEquals("es", detection.language());
assertEquals("es", detection.getLanguage());
}

@Test
public void testDetectLanguageOfText() {
Detection detection = translateSnippets.detectLanguageOfText();
assertEquals("en", detection.language());
assertEquals("en", detection.getLanguage());
}

@Test
public void testTranslateTextList() {
List<Translation> translations = translateSnippets.translateTexts();
Translation translation = translations.get(0);
assertEquals("Hello, World!", translation.translatedText());
assertEquals("en", translation.sourceLanguage());
assertEquals("Hello, World!", translation.getTranslatedText());
assertEquals("en", translation.getSourceLanguage());
translation = translations.get(1);
assertEquals("Hello World!", translation.translatedText());
assertEquals("es", translation.sourceLanguage());
assertEquals("Hello World!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}

@Test
public void testTranslateText() {
Translation translation = translateSnippets.translateText();
assertEquals("Hello World!", translation.translatedText());
assertEquals("es", translation.sourceLanguage());
assertEquals("Hello World!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}

@Test
public void testTranslateTextWithOptions() {
Translation translation = translateSnippets.translateTextWithOptions();
assertEquals("Hallo Welt!", translation.translatedText());
assertEquals("es", translation.sourceLanguage());
assertEquals("Hallo Welt!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
}
2 changes: 1 addition & 1 deletion google-cloud-translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import com.google.cloud.translate.Detection;
Then add the following code to detect the text's language:

```java
String detectedLanguage = detection.language();
String detectedLanguage = detection.getLanguage();
```
#### Translating text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ private Detection(String language, Float confidence) {
* @see <a href="https://cloud.google.com/translate/v2/translate-reference#supported_languages">
* Supported Languages</a>
*/
@Deprecated
public String language() {
return getLanguage();
}

/**
* Returns the code of the detected language.
*
* @see <a href="https://cloud.google.com/translate/v2/translate-reference#supported_languages">
* Supported Languages</a>
*/
public String getLanguage() {
return language;
}

Expand All @@ -56,7 +67,17 @@ public String language() {
* higher the confidence level for the language detection. Note that this value is not always
* available.
*/
@Deprecated
public float confidence() {
return getConfidence();
}

/**
* Returns an optional confidence value in the interval [0,1]. The closer this value is to 1, the
* higher the confidence level for the language detection. Note that this value is not always
* available.
*/
public float getConfidence() {
return confidence;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,30 @@ private Language(String code, String name) {
/**
* Returns the code of the language.
*/
@Deprecated
public String code() {
return getCode();
}

/**
* Returns the code of the language.
*/
public String getCode() {
return code;
}

/**
* Returns the name of the language.
*/
@Deprecated
public String name() {
return getName();
}

/**
* Returns the name of the language.
*/
public String getName() {
return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ abstract class Option implements Serializable {
this.value = value;
}

TranslateRpc.Option rpcOption() {
TranslateRpc.Option getRpcOption() {
return rpcOption;
}

Object value() {
Object getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public static TranslateOption targetLanguage(String targetLanguage) {

/**
* Returns the list of languages supported by Google Translate. If
* {@link LanguageListOption#targetLanguage(String)} is provided, {@link Language#name()} values
* are localized according to the provided target language. If no such option is passed,
* {@link Language#name()} values are localized according to
* {@link LanguageListOption#targetLanguage(String)} is provided, {@link Language#getName()}
* values are localized according to the provided target language. If no such option is passed,
* {@link Language#getName()} values are localized according to
* {@link TranslateOptions#targetLanguage()}.
*
* <p>Example of listing supported languages, localized according to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Translation translate(String text, TranslateOption... options) {
private Map<TranslateRpc.Option, ?> optionMap(Option... options) {
Map<TranslateRpc.Option, Object> optionMap = Maps.newEnumMap(TranslateRpc.Option.class);
for (Option option : options) {
Object prev = optionMap.put(option.rpcOption(), option.value());
Object prev = optionMap.put(option.getRpcOption(), option.getValue());
checkArgument(prev == null, "Duplicate option %s", option);
}
return optionMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,32 @@ private Translation(String translatedText, String sourceLanguage) {
/**
* Returns the translated text.
*/
@Deprecated
public String translatedText() {
return translatedText;
}

/**
* Returns the translated text.
*/
public String getTranslatedText() {
return translatedText;
}

/**
* Returns the language code of the source text. If no source language was provided this value is
* the source language as detected by the Google Translate service.
*/
@Deprecated
public String sourceLanguage() {
return getSourceLanguage();
}

/**
* Returns the language code of the source text. If no source language was provided this value is
* the source language as detected by the Google Translate service.
*/
public String getSourceLanguage() {
return sourceLanguage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
* Translate translate = TranslateOptions.defaultInstance().service();
*
* Detection detection = translate.detect("Hola");
* String detectedLanguage = detection.language();
* String detectedLanguage = detection.getLanguage();
*
* Translation translation = translate.translate(
* "World",
* TranslateOption.sourceLanguage("en"),
* TranslateOption.targetLanguage(detectedLanguage));
*
* System.out.printf("Hola %s%n", translation.translatedText());
* System.out.printf("Hola %s%n", translation.getTranslatedText());
* }</pre>
*/
package com.google.cloud.translate;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* Utility to create a remote translate configuration for testing. Translate options can be obtained
* via the {@link #options()} method. Returned options have custom
* via the {@link #getOptions()} method. Returned options have custom
* {@link TranslateOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10},
* {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()}
* is {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
Expand All @@ -40,7 +40,15 @@ private RemoteTranslateHelper(TranslateOptions options) {
/**
* Returns a {@link TranslateOptions} object to be used for testing.
*/
@Deprecated
public TranslateOptions options() {
return getOptions();
}

/**
* Returns a {@link TranslateOptions} object to be used for testing.
*/
public TranslateOptions getOptions() {
return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* <p>Before the test:
* <pre> {@code
* RemoteTranslateHelper helper = RemoteTranslateHelper.create();
* Translate translate = helper.options().service();
* Translate translate = helper.getOptions().service();
* } </pre>
*
* @see <a href="https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/TESTING.md#testing-code-that-uses-translate">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ public class DetectionTest {

@Test
public void testFromPb() {
assertEquals(LANGUAGE, DETECTION.getLanguage());
assertEquals(CONFIDENCE, DETECTION.getConfidence(), 0);
compareDetection(DETECTION, Detection.fromPb(DETECTION_PB));
}

@Test
public void testFromPbDeprecated() {
assertEquals(LANGUAGE, DETECTION.language());
assertEquals(CONFIDENCE, DETECTION.confidence(), 0);
compareDetection(DETECTION, Detection.fromPb(DETECTION_PB));
}

private void compareDetection(Detection expected, Detection value) {
assertEquals(expected, value);
assertEquals(expected.language(), value.language());
assertEquals(expected.confidence(), value.confidence(), 0);
assertEquals(expected.getLanguage(), value.getLanguage());
assertEquals(expected.getConfidence(), value.getConfidence(), 0);
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public class LanguageTest {

@Test
public void testFromPb() {
assertEquals(CODE, LANGUAGE.getCode());
assertEquals(NAME, LANGUAGE.getName());
Language language = Language.fromPb(new LanguagesResource().setLanguage(CODE));
assertEquals(CODE, language.getCode());
assertNull(language.getName());
compareLanguage(LANGUAGE, Language.fromPb(LANGUAGE_PB));
}

@Test
public void testFromPbDeprecated() {
assertEquals(CODE, LANGUAGE.code());
assertEquals(NAME, LANGUAGE.name());
Language language = Language.fromPb(new LanguagesResource().setLanguage(CODE));
Expand All @@ -43,8 +53,8 @@ public void testFromPb() {

private void compareLanguage(Language expected, Language value) {
assertEquals(expected, value);
assertEquals(expected.name(), value.name());
assertEquals(expected.code(), value.code());
assertEquals(expected.getName(), value.getName());
assertEquals(expected.getCode(), value.getCode());
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
}
Expand Down
Loading