Skip to content

Commit 049414b

Browse files
authored
---
yaml --- r: 8387 b: refs/heads/vam-google-patch-1 c: a8f4b7c h: refs/heads/master i: 8385: 16114fe 8383: f4bb397
1 parent a97e495 commit 049414b

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
@@ -63,5 +63,5 @@ refs/tags/v0.21.0: f88b200e00e41ba6262ee88a92abba38b1e2417e
6363
refs/tags/v0.21.1: aa88a0bb489633eaf6b5c624076139c8561d51ff
6464
refs/heads/alt: 210973b848c8048591a20f195bc44607a11cd536
6565
refs/tags/v0.22.0: 18b298fe4bfe8ec2f20b0e0bf7ffdcce5cc3c5fe
66-
refs/heads/vam-google-patch-1: b4c823d1d41037a437e09d093c483698a324ef74
66+
refs/heads/vam-google-patch-1: a8f4b7cd8947a9fde7955aa49d7b352169fdc406
6767
refs/heads/vam-google-patch-CODEOWNERS: d2fbf724f4e029313466d18a37ddc6fb91ae6a6c

branches/vam-google-patch-1/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/vam-google-patch-1/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)