Skip to content

Commit 796c52e

Browse files
jmdobrychingor13
authored andcommitted
samples: Add Translate API quickstart sample.
1 parent f000cf3 commit 796c52e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
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)