Skip to content

Commit 461a162

Browse files
authored
Merge pull request #348 from GoogleCloudPlatform/translate_sample
Translate sample
2 parents 445a3e8 + a28e440 commit 461a162

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

translate/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Google Cloud Translate Sample
2+
3+
This sample demonstrates the use of [Google Cloud Translate
4+
API][Translate-Docs] for translating and detecting language text.
5+
6+
[Translate-Docs]: https://cloud.google.com/translate/docs/
7+
8+
## Java Version
9+
10+
This sample requires you to have
11+
[Java8](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html).
12+
13+
## Download Maven
14+
15+
This sample uses the [Apache Maven][maven] build system. Before getting started,
16+
be
17+
sure to [download][maven-download] and [install][maven-install] it. When you use
18+
Maven as described here, it will automatically download the needed client
19+
libraries.
20+
21+
[maven]: https://maven.apache.org
22+
[maven-download]: https://maven.apache.org/download.cgi
23+
[maven-install]: https://maven.apache.org/install.html
24+
25+
## Run the sample
26+
27+
To build the sample, we use Maven.
28+
29+
```bash
30+
mvn clean compile assembly:single
31+
```
32+
33+
We can then run the assembled JAR file with the `java` command. The variable
34+
$COMMAND takes three values `langsupport`, `detect` and `translate`.
35+
36+
```
37+
JAR_FILE=target/translate-1.0-SNAPSHOT-jar-with-dependencies.jar
38+
java -jar $JAR_FILE <detect|translate|langsupport> <text>
39+
<optional_source> <optional_target>
40+
```
41+
42+
Example Usage:
43+
44+
```
45+
INPUT="A quick brown fox jumped over a lazy dog."
46+
SOURCE_LANG="en"
47+
TARGET_LANG="fr"
48+
```
49+
50+
Translate API Features:
51+
52+
* List the languages supported by the API
53+
```
54+
java -jar $JAR_FILE langsupport
55+
```
56+
57+
* Detect input text language
58+
```
59+
java -jar $JAR_FILE detect "$INPUT"
60+
```
61+
62+
* Translate input text (with options)
63+
```
64+
java -jar $JAR_FILE translate "$INPUT"
65+
java -jar $JAR_FILE translate "$INPUT" $SOURCE_LANG $TARGET_LANG
66+
```

translate/pom.xml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.google.cloud.translate.samples</groupId>
19+
<artifactId>translate</artifactId>
20+
<version>1.0-SNAPSHOT</version>
21+
<name>translate</name>
22+
<url>http://maven.apache.org</url>
23+
<properties>
24+
<maven.compiler.target>1.8</maven.compiler.target>
25+
<maven.compiler.source>1.8</maven.compiler.source>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
</properties>
28+
<parent>
29+
<groupId>com.google.cloud</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.0</version>
32+
<relativePath>../java-repo-tools</relativePath>
33+
</parent>
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.google.cloud</groupId>
37+
<artifactId>google-cloud-translate</artifactId>
38+
<version>0.3.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.12</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.google.truth</groupId>
48+
<artifactId>truth</artifactId>
49+
<version>0.30</version>
50+
</dependency>
51+
</dependencies>
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<artifactId>maven-assembly-plugin</artifactId>
56+
<configuration>
57+
<archive>
58+
<manifest>
59+
<mainClass>com.google.cloud.translate.samples.TranslateText</mainClass>
60+
</manifest>
61+
</archive>
62+
<descriptorRefs>
63+
<descriptorRef>jar-with-dependencies</descriptorRef>
64+
</descriptorRefs>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.translate.samples;
18+
19+
import com.google.cloud.RetryParams;
20+
import com.google.cloud.translate.Detection;
21+
import com.google.cloud.translate.Language;
22+
import com.google.cloud.translate.Translate;
23+
import com.google.cloud.translate.Translate.TranslateOption;
24+
import com.google.cloud.translate.TranslateOptions;
25+
import com.google.cloud.translate.Translation;
26+
import com.google.common.collect.ImmutableList;
27+
28+
import java.io.PrintStream;
29+
import java.util.List;
30+
31+
public class TranslateText {
32+
/**
33+
* Detect the language of input text.
34+
*
35+
* @param sourceText source text to be detected for language
36+
* @param out print stream
37+
*/
38+
public static void detectLanguage(String sourceText, PrintStream out) {
39+
Translate translate = createTranslateService();
40+
List<Detection> detections = translate.detect(ImmutableList.of(sourceText));
41+
System.out.println("Language(s) detected:");
42+
for (Detection detection : detections) {
43+
out.printf("\t%s\n", detection);
44+
}
45+
}
46+
47+
/**
48+
* Translates the source text in any language to english.
49+
*
50+
* @param sourceText source text to be translated
51+
* @param out print stream
52+
*/
53+
public static void translateText(String sourceText, PrintStream out) {
54+
Translate translate = createTranslateService();
55+
Translation translation = translate.translate(sourceText);
56+
out.printf("Source Text:\n\t%s\n", sourceText);
57+
out.printf("Translated Text:\n\t%s\n", translation.translatedText());
58+
}
59+
60+
/**
61+
* Translate the source text from source to target language.
62+
*
63+
* @param sourceText source text to be translated
64+
* @param sourceLang source language of the text
65+
* @param targetLang target language of translated text
66+
* @param out print stream
67+
*/
68+
public static void translateTextWithOptions(
69+
String sourceText,
70+
String sourceLang,
71+
String targetLang,
72+
PrintStream out) {
73+
74+
Translate translate = createTranslateService();
75+
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
76+
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
77+
78+
Translation translation = translate.translate(sourceText, srcLang, tgtLang);
79+
out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
80+
out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.translatedText());
81+
}
82+
83+
/**
84+
* Displays a list of supported languages and codes.
85+
*
86+
* @param out print stream
87+
*/
88+
public static void displaySupportedLanguages(PrintStream out) {
89+
Translate translate = createTranslateService();
90+
List<Language> languages = translate.listSupportedLanguages();
91+
92+
for (Language language : languages) {
93+
out.printf("Name: %s, Code: %s\n", language.name(), language.code());
94+
}
95+
}
96+
97+
/**
98+
* Create Google Translate API Service.
99+
*
100+
* @return Google Translate Service
101+
*/
102+
public static Translate createTranslateService() {
103+
TranslateOptions translateOption = TranslateOptions.builder()
104+
.retryParams(retryParams())
105+
.connectTimeout(60000)
106+
.readTimeout(60000)
107+
.build();
108+
return translateOption.service();
109+
}
110+
111+
/**
112+
* Retry params for the Translate API.
113+
*/
114+
private static RetryParams retryParams() {
115+
return RetryParams.builder()
116+
.retryMaxAttempts(3)
117+
.maxRetryDelayMillis(30000)
118+
.totalRetryPeriodMillis(120000)
119+
.initialRetryDelayMillis(250)
120+
.build();
121+
}
122+
123+
public static void main(String[] args) {
124+
String command = args[0];
125+
String text;
126+
127+
if (command.equals("detect")) {
128+
text = args[1];
129+
TranslateText.detectLanguage(text, System.out);
130+
} else if (command.equals("translate")) {
131+
text = args[1];
132+
try {
133+
String sourceLang = args[2];
134+
String targetLang = args[3];
135+
TranslateText.translateTextWithOptions(text, sourceLang, targetLang, System.out);
136+
} catch (ArrayIndexOutOfBoundsException ex) {
137+
TranslateText.translateText(text, System.out);
138+
}
139+
} else if (command.equals("langsupport")) {
140+
TranslateText.displaySupportedLanguages(System.out);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)