Skip to content

Commit b221dc9

Browse files
committed
Add snippets to Translate's javadoc, TranslateSnippets class and tests
1 parent afc4a2a commit b221dc9

4 files changed

Lines changed: 302 additions & 9 deletions

File tree

gcloud-java-examples/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
<artifactId>maven-assembly-plugin</artifactId>
3333
<version>2.5.4</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.12</version>
39+
<scope>test</scope>
40+
</dependency>
3541
</dependencies>
3642
<build>
3743
<plugins>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Translate's javadoc. Any change to this file should be reflected in
20+
* Translate's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.translate.snippets;
24+
25+
import com.google.cloud.translate.Detection;
26+
import com.google.cloud.translate.Language;
27+
import com.google.cloud.translate.Translate;
28+
import com.google.cloud.translate.Translate.LanguageListOption;
29+
import com.google.cloud.translate.Translate.TranslateOption;
30+
import com.google.cloud.translate.TranslateOptions;
31+
import com.google.cloud.translate.Translation;
32+
33+
import java.util.LinkedList;
34+
import java.util.List;
35+
36+
/**
37+
* This class contains a number of snippets for the {@link Translate} interface.
38+
*/
39+
public class TranslateSnippets {
40+
41+
private final Translate translate;
42+
43+
public TranslateSnippets(Translate translate) {
44+
this.translate = translate;
45+
}
46+
47+
/**
48+
* Example of listing supported languages, localized according to
49+
* {@link TranslateOptions#targetLanguage()}.
50+
*/
51+
// [TARGET listSupportedLanguages(LanguageListOption...)]
52+
public List<Language> listSupportedLanguages() {
53+
// [START listSupportedLanguages]
54+
List<Language> languages = translate.listSupportedLanguages();
55+
// [END listSupportedLanguages]
56+
return languages;
57+
}
58+
59+
/**
60+
* Example of listing supported languages, localized according to a provided language.
61+
*/
62+
// [TARGET listSupportedLanguages(LanguageListOption...)]
63+
public List<Language> listSupportedLanguagesWithTarget() {
64+
// [START listSupportedLanguagesWithTarget]
65+
List<Language> languages = translate.listSupportedLanguages(
66+
LanguageListOption.targetLanguage("es"));
67+
// [END listSupportedLanguagesWithTarget]
68+
return languages;
69+
}
70+
71+
/**
72+
* Example of detecting the language of some texts.
73+
*/
74+
// [TARGET detect(List)]
75+
public List<Detection> detectLanguageOfTextList() {
76+
// [START detectLanguageOfTextList]
77+
List<String> texts = new LinkedList<>();
78+
texts.add("Hello, World!");
79+
texts.add("¡Hola Mundo!");
80+
List<Detection> detections = translate.detect(texts);
81+
// [END detectLanguageOfTextList]
82+
return detections;
83+
}
84+
85+
/**
86+
* Example of detecting the language of some texts.
87+
*/
88+
// [TARGET detect(String...)]
89+
public List<Detection> detectLanguageOfTexts() {
90+
// [START detectLanguageOfTexts]
91+
List<Detection> detections = translate.detect("Hello, World!", "¡Hola Mundo!");
92+
// [END detectLanguageOfTexts]
93+
return detections;
94+
}
95+
96+
/**
97+
* Example of detecting the language of a text.
98+
*/
99+
// [TARGET detect(String)]
100+
public Detection detectLanguageOfText() {
101+
// [START detect]
102+
Detection detection = translate.detect("Hello, World!");
103+
// [END detect]
104+
return detection;
105+
}
106+
107+
/**
108+
* Example of translating some texts.
109+
*/
110+
// [TARGET translate(List, TranslateOption...)]
111+
public List<Translation> translateTexts() {
112+
// [START translateTexts]
113+
List<String> texts = new LinkedList<>();
114+
texts.add("Hello, World!");
115+
texts.add("¡Hola Mundo!");
116+
List<Translation> translations = translate.translate(texts);
117+
// [END translateTexts]
118+
return translations;
119+
}
120+
121+
/**
122+
* Example of translating some texts, specifying source and target language.
123+
*/
124+
// [TARGET translate(List, TranslateOption...)]
125+
public List<Translation> translateTextsWithOptions() {
126+
// [START translateTextsWithOptions]
127+
List<String> texts = new LinkedList<>();
128+
texts.add("¡Hola Mundo!");
129+
List<Translation> translations = translate.translate(texts,
130+
TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));
131+
// [END translateTextsWithOptions]
132+
return translations;
133+
}
134+
135+
/**
136+
* Example of translating a text.
137+
*/
138+
// [TARGET translate(String, TranslateOption...)]
139+
public Translation translateText() {
140+
// [START translateText]
141+
Translation translation = translate.translate("¡Hola Mundo!");
142+
// [END translateText]
143+
return translation;
144+
}
145+
146+
/**
147+
* Example of translating a text, specifying source and target language.
148+
*/
149+
// [TARGET translate(String, TranslateOption...)]
150+
public Translation translateTextWithOptions() {
151+
// [START translateTextWithOptions]
152+
Translation translation = translate.translate("¡Hola Mundo!",
153+
TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));
154+
// [END translateTextWithOptions]
155+
return translation;
156+
}
157+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.translate.snippets;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import com.google.cloud.translate.Detection;
24+
import com.google.cloud.translate.Language;
25+
import com.google.cloud.translate.Translation;
26+
import com.google.cloud.translate.testing.RemoteTranslateHelper;
27+
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
import java.util.HashSet;
32+
import java.util.List;
33+
import java.util.Set;
34+
35+
public class ITTranslateSnippets {
36+
37+
private static TranslateSnippets translateSnippets;
38+
39+
private static final String[] LANGUAGES = {"af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs",
40+
"bg", "ca", "ceb", "ny", "zh-TW", "hr", "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr",
41+
"gl", "ka", "de", "el", "gu", "ht", "ha", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga",
42+
"it", "ja", "jw", "kn", "kk", "km", "ko", "lo", "la", "lv", "lt", "mk", "mg", "ms", "ml",
43+
"mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", "pt", "ro", "ru", "sr", "st", "si",
44+
"sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", "th", "tr", "uk", "ur", "uz",
45+
"vi", "cy", "yi", "yo", "zu"};
46+
47+
@BeforeClass
48+
public static void beforeClass() {
49+
RemoteTranslateHelper helper = RemoteTranslateHelper.create();
50+
translateSnippets = new TranslateSnippets(helper.options().service());
51+
}
52+
53+
@Test
54+
public void testListSupportedLanguages() {
55+
Set<String> supportedLanguages = new HashSet<>();
56+
List<Language> languages = translateSnippets.listSupportedLanguages();
57+
for (Language language : languages) {
58+
supportedLanguages.add(language.code());
59+
assertNotNull(language.name());
60+
}
61+
for (String code : LANGUAGES) {
62+
assertTrue(supportedLanguages.contains(code));
63+
}
64+
}
65+
66+
@Test
67+
public void testListSupportedLanguagesWithTarget() {
68+
Set<String> supportedLanguages = new HashSet<>();
69+
List<Language> languages = translateSnippets.listSupportedLanguagesWithTarget();
70+
for (Language language : languages) {
71+
supportedLanguages.add(language.code());
72+
assertNotNull(language.name());
73+
}
74+
for (String code : LANGUAGES) {
75+
assertTrue(supportedLanguages.contains(code));
76+
}
77+
}
78+
79+
@Test
80+
public void testDetectLanguageOfTexts() {
81+
List<Detection> detections = translateSnippets.detectLanguageOfTexts();
82+
Detection detection = detections.get(0);
83+
assertEquals("en", detection.language());
84+
detection = detections.get(1);
85+
assertEquals("es", detection.language());
86+
}
87+
88+
@Test
89+
public void testDetectLanguageOfTextList() {
90+
List<Detection> detections = translateSnippets.detectLanguageOfTextList();
91+
Detection detection = detections.get(0);
92+
assertEquals("en", detection.language());
93+
detection = detections.get(1);
94+
assertEquals("es", detection.language());
95+
}
96+
97+
@Test
98+
public void testDetectLanguageOfText() {
99+
Detection detection = translateSnippets.detectLanguageOfText();
100+
assertEquals("en", detection.language());
101+
}
102+
103+
@Test
104+
public void testTranslateTextList() {
105+
List<Translation> translations = translateSnippets.translateTexts();
106+
Translation translation = translations.get(0);
107+
assertEquals("Hello, World!", translation.translatedText());
108+
assertEquals("en", translation.sourceLanguage());
109+
translation = translations.get(1);
110+
assertEquals("Hello World!", translation.translatedText());
111+
assertEquals("es", translation.sourceLanguage());
112+
}
113+
114+
@Test
115+
public void testTranslateText() {
116+
Translation translation = translateSnippets.translateText();
117+
assertEquals("Hello World!", translation.translatedText());
118+
assertEquals("es", translation.sourceLanguage());
119+
}
120+
121+
@Test
122+
public void testTranslateTextWithOptions() {
123+
Translation translation = translateSnippets.translateTextWithOptions();
124+
assertEquals("Hallo Welt!", translation.translatedText());
125+
assertEquals("es", translation.sourceLanguage());
126+
}
127+
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,24 @@ public static TranslateOption targetLanguage(String targetLanguage) {
9191
* {@link TranslateOptions#targetLanguage()}.
9292
*
9393
* <p>Example of listing supported languages, localized according to
94-
* {@link TranslateOptions#targetLanguage()}:
94+
* {@link TranslateOptions#targetLanguage()}.
9595
* <pre> {@code
9696
* List<Language> languages = translate.listSupportedLanguages();
9797
* }</pre>
98-
* Or according to another target language:
98+
*
99+
* <p>Example of listing supported languages, localized according to a provided language.
99100
* <pre> {@code
100101
* List<Language> languages = translate.listSupportedLanguages(
101102
* LanguageListOption.targetLanguage("es"));
102103
* }</pre>
104+
*
103105
*/
104106
List<Language> listSupportedLanguages(LanguageListOption... options);
105107

106108
/**
107109
* Detects the language of the provided texts.
108110
*
109-
* <p>Example of detecting the language of some texts:
111+
* <p>Example of detecting the language of some texts.
110112
* <pre> {@code
111113
* List<String> texts = new LinkedList<>();
112114
* texts.add("Hello, World!");
@@ -123,7 +125,7 @@ public static TranslateOption targetLanguage(String targetLanguage) {
123125
/**
124126
* Detects the language of the provided texts.
125127
*
126-
* <p>Example of detecting the language of some texts:
128+
* <p>Example of detecting the language of some texts.
127129
* <pre> {@code
128130
* List<Detection> detections = translate.detect("Hello, World!", "¡Hola Mundo!");
129131
* }</pre>
@@ -138,25 +140,26 @@ public static TranslateOption targetLanguage(String targetLanguage) {
138140
* Detects the language of the provided text. Returns an object containing information on the
139141
* language detection.
140142
*
141-
* <p>Example of detecting the language a text:
143+
* <p>Example of detecting the language of a text.
142144
* <pre> {@code
143145
* Detection detection = translate.detect("Hello, World!");
144146
* }</pre>
147+
*
145148
*/
146149
Detection detect(String text);
147150

148151
/**
149152
* Translates the provided texts.
150153
*
151-
* <p>Example of translating some texts:
154+
* <p>Example of translating some texts.
152155
* <pre> {@code
153156
* List<String> texts = new LinkedList<>();
154157
* texts.add("Hello, World!");
155158
* texts.add("¡Hola Mundo!");
156159
* List<Translation> translations = translate.translate(texts);
157160
* }</pre>
158161
*
159-
* <p>Example of translating some texts, specifying source and target language:
162+
* <p>Example of translating some texts, specifying source and target language.
160163
* <pre> {@code
161164
* List<String> texts = new LinkedList<>();
162165
* texts.add("¡Hola Mundo!");
@@ -173,12 +176,12 @@ public static TranslateOption targetLanguage(String targetLanguage) {
173176
/**
174177
* Translates the provided texts.
175178
*
176-
* <p>Example of translating a text:
179+
* <p>Example of translating a text.
177180
* <pre> {@code
178181
* Translation translation = translate.translate("¡Hola Mundo!");
179182
* }</pre>
180183
*
181-
* <p>Example of translating a text, specifying source and target language:
184+
* <p>Example of translating a text, specifying source and target language.
182185
* <pre> {@code
183186
* Translation translation = translate.translate("¡Hola Mundo!",
184187
* TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));

0 commit comments

Comments
 (0)