Skip to content

Commit 96b4a85

Browse files
rangothedogRango Meadows
andauthored
Text Translation Samples: Updating README and java samples (Azure#36346)
Co-authored-by: Rango Meadows <[email protected]>
1 parent 1c28cc0 commit 96b4a85

File tree

8 files changed

+27
-12
lines changed

8 files changed

+27
-12
lines changed

sdk/translation/azure-ai-translation-text/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ update the API key without creating a new client.
6262

6363
With the value of the endpoint, `AzureKeyCredential` and a `Region`, you can create the [TextTranslationClient][translator_client_class]:
6464

65-
```Java
65+
```java createTextTranslationRestClient
6666
String apiKey = System.getenv("TEXT_TRANSLATOR_API_KEY");
6767
String region = System.getenv("TEXT_TRANSLATOR_API_REGION");
6868
AzureKeyCredential credential = new AzureKeyCredential(apiKey);
6969

7070
TextTranslationClient client = new TextTranslationClientBuilder()
71-
.credential(credential)
72-
.region(region)
73-
.buildClient();
71+
.credential(credential)
72+
.region(region)
73+
.endpoint("https://api.cognitive.microsofttranslator.com")
74+
.buildClient();
7475
```
7576

7677
## Key concepts
@@ -94,7 +95,7 @@ The following section provides several code snippets using the `client` [created
9495

9596
Gets the set of languages currently supported by other operations of the Translator.
9697

97-
```Java
98+
```java getTextTranslationLanguages
9899
GetLanguagesResult languages = client.getLanguages();
99100

100101
System.out.println("Number of supported languages for translate operation: " + languages.getTranslation().size() + ".");
@@ -123,7 +124,7 @@ Please refer to the service documentation for a conceptual discussion of [langua
123124

124125
Renders single source-language text to multiple target-language texts with a single request.
125126

126-
```Java
127+
```java getTextTranslationMultiple
127128
String from = "en";
128129
List<String> targetLanguages = new ArrayList<>();
129130
targetLanguages.add("cs");
@@ -145,7 +146,7 @@ Please refer to the service documentation for a conceptual discussion of [transl
145146

146147
Converts characters or letters of a source language to the corresponding characters or letters of a target language.
147148

148-
```Java
149+
```java getTextTranslationTransliterate
149150
String language = "zh-Hans";
150151
String fromScript = "Hans";
151152
String toScript = "Latn";
@@ -165,7 +166,7 @@ Please refer to the service documentation for a conceptual discussion of [transl
165166

166167
Identifies the positioning of sentence boundaries in a piece of text.
167168

168-
```Java
169+
```java getTextTranslationSentenceBoundaries
169170
String sourceLanguage = "zh-Hans";
170171
String sourceScript = "Latn";
171172
List<InputTextItem> content = new ArrayList<>();
@@ -184,7 +185,7 @@ Please refer to the service documentation for a conceptual discussion of [break
184185

185186
Returns equivalent words for the source term in the target language.
186187

187-
```Java
188+
```java getTextTranslationDictionaryLookup
188189
String sourceLanguage = "en";
189190
String targetLanguage = "es";
190191
List<InputTextItem> content = new ArrayList<>();
@@ -204,7 +205,7 @@ Please refer to the service documentation for a conceptual discussion of [dictio
204205

205206
Returns grammatical structure and context examples for the source term and target term pair.
206207

207-
```Java
208+
```java getTextTranslationDictionaryExamples
208209
String sourceLanguage = "en";
209210
String targetLanguage = "es";
210211
List<DictionaryExampleTextItem> content = new ArrayList<>();

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/BreakSentence.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static void main(final String[] args) {
2828
.region(region)
2929
.endpoint("https://api.cognitive.microsofttranslator.com")
3030
.buildClient();
31-
31+
32+
// BEGIN: getTextTranslationSentenceBoundaries
3233
String sourceLanguage = "zh-Hans";
3334
String sourceScript = "Latn";
3435
List<InputTextItem> content = new ArrayList<>();
@@ -39,5 +40,6 @@ public static void main(final String[] args) {
3940
for (BreakSentenceItem breakSentence : breakSentences) {
4041
System.out.println("The detected sentence boundaries: " + breakSentence.getSentLen());
4142
}
43+
// END: getTextTranslationSentenceBoundaries
4244
}
4345
}

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/DictionaryExamples.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static void main(final String[] args) {
2828
.region(region)
2929
.endpoint("https://api.cognitive.microsofttranslator.com")
3030
.buildClient();
31-
31+
32+
// BEGIN: getTextTranslationDictionaryExamples
3233
String sourceLanguage = "en";
3334
String targetLanguage = "es";
3435
List<DictionaryExampleTextItem> content = new ArrayList<>();
@@ -40,5 +41,6 @@ public static void main(final String[] args) {
4041
System.out.println("For the given input " + dictionaryEntry.getExamples().size() + " entries were found in the dictionary.");
4142
System.out.println("Example: '" + dictionaryEntry.getExamples().get(0).getTargetPrefix() + dictionaryEntry.getExamples().get(0).getTargetTerm() + dictionaryEntry.getExamples().get(0).getTargetSuffix());
4243
}
44+
// END: getTextTranslationDictionaryExamples
4345
}
4446
}

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/DictionaryLookup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static void main(final String[] args) {
2929
.endpoint("https://api.cognitive.microsofttranslator.com")
3030
.buildClient();
3131

32+
// BEGIN: getTextTranslationDictionaryLookup
3233
String sourceLanguage = "en";
3334
String targetLanguage = "es";
3435
List<InputTextItem> content = new ArrayList<>();
@@ -40,5 +41,6 @@ public static void main(final String[] args) {
4041
System.out.println("For the given input " + dictionaryEntry.getTranslations().size() + " entries were found in the dictionary.");
4142
System.out.println("First entry: '" + dictionaryEntry.getTranslations().get(0).getDisplayTarget() + "', confidence: " + dictionaryEntry.getTranslations().get(0).getConfidence());
4243
}
44+
// END: getTextTranslationDictionaryLookup
4345
}
4446
}

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/GetLanguages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static void main(final String[] args) {
2323
.endpoint("https://api.cognitive.microsofttranslator.com")
2424
.buildClient();
2525

26+
// BEGIN: getTextTranslationLanguages
2627
GetLanguagesResult languages = client.getLanguages();
2728

2829
System.out.println("Number of supported languages for translate operation: " + languages.getTranslation().size() + ".");
@@ -43,5 +44,6 @@ public static void main(final String[] args) {
4344
for (Map.Entry<String, SourceDictionaryLanguage> dictionaryLanguage : languages.getDictionary().entrySet()) {
4445
System.out.println(dictionaryLanguage.getKey() + " -- name: " + dictionaryLanguage.getValue().getName() + ", supported target languages count: " + dictionaryLanguage.getValue().getTranslations().size());
4546
}
47+
// END: getTextTranslationLanguages
4648
}
4749
}

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/ReadmeSamples.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void createClientAnonymous() {
2020
* Sample for creating text translator client using Text Translator API Key and Region.
2121
*/
2222
public void createClient() {
23+
// BEGIN: createTextTranslationRestClient
2324
String apiKey = System.getenv("TEXT_TRANSLATOR_API_KEY");
2425
String region = System.getenv("TEXT_TRANSLATOR_API_REGION");
2526
AzureKeyCredential credential = new AzureKeyCredential(apiKey);
@@ -29,6 +30,7 @@ public void createClient() {
2930
.region(region)
3031
.endpoint("https://api.cognitive.microsofttranslator.com")
3132
.buildClient();
33+
// END: createTextTranslationRestClient
3234
}
3335

3436
/**

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/Translate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void main(final String[] args) {
3333
.endpoint("https://api.cognitive.microsofttranslator.com")
3434
.buildClient();
3535

36+
// BEGIN: getTextTranslationMultiple
3637
String from = "en";
3738
List<String> targetLanguages = new ArrayList<>();
3839
targetLanguages.add("cs");
@@ -46,5 +47,6 @@ public static void main(final String[] args) {
4647
System.out.println("Text was translated to: '" + textTranslation.getTo() + "' and the result is: '" + textTranslation.getText() + "'.");
4748
}
4849
}
50+
// END: getTextTranslationMultiple
4951
}
5052
}

sdk/translation/azure-ai-translation-text/src/samples/java/com/azure/ai/translation/text/Transliterate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void main(final String[] args) {
3030
.endpoint("https://api.cognitive.microsofttranslator.com")
3131
.buildClient();
3232

33+
// BEGIN: getTextTranslationTransliterate
3334
String language = "zh-Hans";
3435
String fromScript = "Hans";
3536
String toScript = "Latn";
@@ -41,5 +42,6 @@ public static void main(final String[] args) {
4142
for (TransliteratedText transliteration : transliterations) {
4243
System.out.println("Input text was transliterated to '" + transliteration.getScript() + "' script. Transliterated text: '" + transliteration.getText() + "'.");
4344
}
45+
// END: getTextTranslationTransliterate
4446
}
4547
}

0 commit comments

Comments
 (0)