Skip to content

Commit bb3f49e

Browse files
jmdobrytswast
authored andcommitted
Add Translate API quickstart sample.
1 parent cb65218 commit bb3f49e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

translate/cloud-client/pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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>come.example.translate</groupId>
19+
<artifactId>translate-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-translate</artifactId>
40+
<version>0.4.0</version>
41+
</dependency>
42+
</dependencies>
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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.example.translate;
18+
19+
// [START translate_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.translate.Translate;
22+
import com.google.cloud.translate.Translate.TranslateOption;
23+
import com.google.cloud.translate.TranslateOptions;
24+
import com.google.cloud.translate.Translation;
25+
26+
public class QuickstartSample {
27+
public static void main(String... args) throws Exception {
28+
// Instantiates a client
29+
Translate translate = TranslateOptions.builder().apiKey("YOUR_API_KEY").build().service();
30+
// The text to translate
31+
String text = "Hello, world!";
32+
// Translates some text into Russian
33+
Translation translation = translate.translate(
34+
text,
35+
TranslateOption.sourceLanguage("en"),
36+
TranslateOption.targetLanguage("ru")
37+
);
38+
System.out.printf("Text: %s%n", text);
39+
System.out.printf("Translation: %s%n", translation.translatedText());
40+
}
41+
}
42+
// [END translate_quickstart]

0 commit comments

Comments
 (0)