Skip to content

Commit 8bf7ac0

Browse files
authored
---
yaml --- r: 8377 b: refs/heads/snehashah-snippets c: a8f4b7c h: refs/heads/master i: 8375: c14defd
1 parent af8aa4e commit 8bf7ac0

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5656
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5757
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5858
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
59-
refs/heads/snehashah-snippets: b4c823d1d41037a437e09d093c483698a324ef74
59+
refs/heads/snehashah-snippets: a8f4b7cd8947a9fde7955aa49d7b352169fdc406
6060
refs/tags/v0.20.2: 5a53aa06f268b74dc192204f4f83e1a04d40f65d
6161
refs/tags/v0.20.3: 269025fdc14af0b68df214a4518be5379b2fe569
6262
refs/tags/v0.21.0: f88b200e00e41ba6262ee88a92abba38b1e2417e

branches/snehashah-snippets/google-cloud-examples/src/main/java/com/google/cloud/examples/translate/snippets/DetectLanguageAndTranslate.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ public static void main(String... args) {
4242
// environment variable
4343
Translate translate = TranslateOptions.getDefaultInstance().getService();
4444

45-
// Detect the language of some text
46-
Detection detection = translate.detect("Hola");
45+
// Text of an "unknown" language to detect and then translate into English
46+
final String mysteriousText = "Hola Mundo";
47+
48+
// Detect the language of the mysterious text
49+
Detection detection = translate.detect(mysteriousText);
4750
String detectedLanguage = detection.getLanguage();
4851

49-
// Translate some text
52+
// Translate the mysterious text to English
5053
Translation translation = translate.translate(
51-
"World",
52-
TranslateOption.sourceLanguage("en"),
53-
TranslateOption.targetLanguage(detectedLanguage));
54+
mysteriousText,
55+
TranslateOption.sourceLanguage(detectedLanguage),
56+
TranslateOption.targetLanguage("en"));
5457

55-
System.out.printf("Hola %s%n", translation.getTranslatedText());
58+
System.out.println(translation.getTranslatedText());
5659
}
5760
}

branches/snehashah-snippets/google-cloud-translate/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,16 @@ Add the following import at the top of your file:
108108
import com.google.cloud.translate.Detection;
109109
```
110110

111+
Then pick a text sample:
112+
113+
```java
114+
final String mysteriousText = "Hola Mundo";
115+
```
116+
111117
Then add the following code to detect the text's language:
112118

113119
```java
114-
Detection detection = translate.detect("Hello, World!");
120+
Detection detection = translate.detect(mysteriousText);
115121
String detectedLanguage = detection.getLanguage();
116122
```
117123
#### Translating text
@@ -126,13 +132,13 @@ import com.google.cloud.translate.Translate.TranslateOption;
126132
import com.google.cloud.translate.Translation;
127133
```
128134

129-
Then add the following code to translate a text (specifying its source language):
135+
Then add the following code to translate the text, specifying the previously detected language (`detectedLanguage`) as its source language and English as the target language (providing the source language is optional, if it is not specified the service will try to detect it automatically):
130136

131137
```java
132138
Translation translation = translate.translate(
133-
"World",
134-
TranslateOption.sourceLanguage("en"),
135-
TranslateOption.targetLanguage(detectedLanguage));
139+
mysteriousText,
140+
TranslateOption.sourceLanguage(detectedLanguage),
141+
TranslateOption.targetLanguage("en"));
136142
```
137143

138144
#### Complete source code

0 commit comments

Comments
 (0)