-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add snippets to Translate's javadoc, TranslateSnippets class and tests (
- Loading branch information
Showing
4 changed files
with
302 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...xamples/src/main/java/com/google/cloud/examples/translate/snippets/TranslateSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in Translate's javadoc. Any change to this file should be reflected in | ||
* Translate's javadoc. | ||
*/ | ||
|
||
package com.google.cloud.examples.translate.snippets; | ||
|
||
import com.google.cloud.translate.Detection; | ||
import com.google.cloud.translate.Language; | ||
import com.google.cloud.translate.Translate; | ||
import com.google.cloud.translate.Translate.LanguageListOption; | ||
import com.google.cloud.translate.Translate.TranslateOption; | ||
import com.google.cloud.translate.TranslateOptions; | ||
import com.google.cloud.translate.Translation; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* This class contains a number of snippets for the {@link Translate} interface. | ||
*/ | ||
public class TranslateSnippets { | ||
|
||
private final Translate translate; | ||
|
||
public TranslateSnippets(Translate translate) { | ||
this.translate = translate; | ||
} | ||
|
||
/** | ||
* Example of listing supported languages, localized according to | ||
* {@link TranslateOptions#targetLanguage()}. | ||
*/ | ||
// [TARGET listSupportedLanguages(LanguageListOption...)] | ||
public List<Language> listSupportedLanguages() { | ||
// [START listSupportedLanguages] | ||
List<Language> languages = translate.listSupportedLanguages(); | ||
// [END listSupportedLanguages] | ||
return languages; | ||
} | ||
|
||
/** | ||
* Example of listing supported languages, localized according to a provided language. | ||
*/ | ||
// [TARGET listSupportedLanguages(LanguageListOption...)] | ||
public List<Language> listSupportedLanguagesWithTarget() { | ||
// [START listSupportedLanguagesWithTarget] | ||
List<Language> languages = translate.listSupportedLanguages( | ||
LanguageListOption.targetLanguage("es")); | ||
// [END listSupportedLanguagesWithTarget] | ||
return languages; | ||
} | ||
|
||
/** | ||
* Example of detecting the language of some texts. | ||
*/ | ||
// [TARGET detect(List)] | ||
public List<Detection> detectLanguageOfTextList() { | ||
// [START detectLanguageOfTextList] | ||
List<String> texts = new LinkedList<>(); | ||
texts.add("Hello, World!"); | ||
texts.add("¡Hola Mundo!"); | ||
List<Detection> detections = translate.detect(texts); | ||
// [END detectLanguageOfTextList] | ||
return detections; | ||
} | ||
|
||
/** | ||
* Example of detecting the language of some texts. | ||
*/ | ||
// [TARGET detect(String...)] | ||
public List<Detection> detectLanguageOfTexts() { | ||
// [START detectLanguageOfTexts] | ||
List<Detection> detections = translate.detect("Hello, World!", "¡Hola Mundo!"); | ||
// [END detectLanguageOfTexts] | ||
return detections; | ||
} | ||
|
||
/** | ||
* Example of detecting the language of a text. | ||
*/ | ||
// [TARGET detect(String)] | ||
public Detection detectLanguageOfText() { | ||
// [START detect] | ||
Detection detection = translate.detect("Hello, World!"); | ||
// [END detect] | ||
return detection; | ||
} | ||
|
||
/** | ||
* Example of translating some texts. | ||
*/ | ||
// [TARGET translate(List, TranslateOption...)] | ||
public List<Translation> translateTexts() { | ||
// [START translateTexts] | ||
List<String> texts = new LinkedList<>(); | ||
texts.add("Hello, World!"); | ||
texts.add("¡Hola Mundo!"); | ||
List<Translation> translations = translate.translate(texts); | ||
// [END translateTexts] | ||
return translations; | ||
} | ||
|
||
/** | ||
* Example of translating some texts, specifying source and target language. | ||
*/ | ||
// [TARGET translate(List, TranslateOption...)] | ||
public List<Translation> translateTextsWithOptions() { | ||
// [START translateTextsWithOptions] | ||
List<String> texts = new LinkedList<>(); | ||
texts.add("¡Hola Mundo!"); | ||
List<Translation> translations = translate.translate(texts, | ||
TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de")); | ||
// [END translateTextsWithOptions] | ||
return translations; | ||
} | ||
|
||
/** | ||
* Example of translating a text. | ||
*/ | ||
// [TARGET translate(String, TranslateOption...)] | ||
public Translation translateText() { | ||
// [START translateText] | ||
Translation translation = translate.translate("¡Hola Mundo!"); | ||
// [END translateText] | ||
return translation; | ||
} | ||
|
||
/** | ||
* Example of translating a text, specifying source and target language. | ||
*/ | ||
// [TARGET translate(String, TranslateOption...)] | ||
public Translation translateTextWithOptions() { | ||
// [START translateTextWithOptions] | ||
Translation translation = translate.translate("¡Hola Mundo!", | ||
TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de")); | ||
// [END translateTextWithOptions] | ||
return translation; | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
...mples/src/test/java/com/google/cloud/examples/translate/snippets/ITTranslateSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.translate.snippets; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.cloud.translate.Detection; | ||
import com.google.cloud.translate.Language; | ||
import com.google.cloud.translate.Translation; | ||
import com.google.cloud.translate.testing.RemoteTranslateHelper; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class ITTranslateSnippets { | ||
|
||
private static TranslateSnippets translateSnippets; | ||
|
||
private static final String[] LANGUAGES = {"af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", | ||
"bg", "ca", "ceb", "ny", "zh-TW", "hr", "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", | ||
"gl", "ka", "de", "el", "gu", "ht", "ha", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", | ||
"it", "ja", "jw", "kn", "kk", "km", "ko", "lo", "la", "lv", "lt", "mk", "mg", "ms", "ml", | ||
"mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", "pt", "ro", "ru", "sr", "st", "si", | ||
"sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", "th", "tr", "uk", "ur", "uz", | ||
"vi", "cy", "yi", "yo", "zu"}; | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
RemoteTranslateHelper helper = RemoteTranslateHelper.create(); | ||
translateSnippets = new TranslateSnippets(helper.options().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()); | ||
} | ||
for (String code : LANGUAGES) { | ||
assertTrue(supportedLanguages.contains(code)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListSupportedLanguagesWithTarget() { | ||
Set<String> supportedLanguages = new HashSet<>(); | ||
List<Language> languages = translateSnippets.listSupportedLanguagesWithTarget(); | ||
for (Language language : languages) { | ||
supportedLanguages.add(language.code()); | ||
assertNotNull(language.name()); | ||
} | ||
for (String code : LANGUAGES) { | ||
assertTrue(supportedLanguages.contains(code)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDetectLanguageOfTexts() { | ||
List<Detection> detections = translateSnippets.detectLanguageOfTexts(); | ||
Detection detection = detections.get(0); | ||
assertEquals("en", detection.language()); | ||
detection = detections.get(1); | ||
assertEquals("es", detection.language()); | ||
} | ||
|
||
@Test | ||
public void testDetectLanguageOfTextList() { | ||
List<Detection> detections = translateSnippets.detectLanguageOfTextList(); | ||
Detection detection = detections.get(0); | ||
assertEquals("en", detection.language()); | ||
detection = detections.get(1); | ||
assertEquals("es", detection.language()); | ||
} | ||
|
||
@Test | ||
public void testDetectLanguageOfText() { | ||
Detection detection = translateSnippets.detectLanguageOfText(); | ||
assertEquals("en", detection.language()); | ||
} | ||
|
||
@Test | ||
public void testTranslateTextList() { | ||
List<Translation> translations = translateSnippets.translateTexts(); | ||
Translation translation = translations.get(0); | ||
assertEquals("Hello, World!", translation.translatedText()); | ||
assertEquals("en", translation.sourceLanguage()); | ||
translation = translations.get(1); | ||
assertEquals("Hello World!", translation.translatedText()); | ||
assertEquals("es", translation.sourceLanguage()); | ||
} | ||
|
||
@Test | ||
public void testTranslateText() { | ||
Translation translation = translateSnippets.translateText(); | ||
assertEquals("Hello World!", translation.translatedText()); | ||
assertEquals("es", translation.sourceLanguage()); | ||
} | ||
|
||
@Test | ||
public void testTranslateTextWithOptions() { | ||
Translation translation = translateSnippets.translateTextWithOptions(); | ||
assertEquals("Hallo Welt!", translation.translatedText()); | ||
assertEquals("es", translation.sourceLanguage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters