Skip to content

Commit

Permalink
[Text Translation] Replace async examples with synchronous code. (Azu…
Browse files Browse the repository at this point in the history
…re#38386)

* Update Readme.md snippets to be populated by tool, added SampleSnippets.cs file as source.

* Fixing missing double quote on region identifier.

* Updating sample C# snippets and code (#2)

* Updating sample C# snippets and code.

* Use explicit type instead of var, remove blank lines from #if

* Finished change to use explicit type instead of var

---------

Co-authored-by: Rango Meadows <v-rameadows@microsoft.com>

* Replace async samples with synchronous code

* Corrected reference to async samples in README.md

---------

Co-authored-by: Rango Meadows <v-rameadows@microsoft.com>
Co-authored-by: Michal Materna <michal.materna@hotmail.com>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 2298521 commit e340b46
Show file tree
Hide file tree
Showing 8 changed files with 792 additions and 118 deletions.
30 changes: 15 additions & 15 deletions sdk/translation/Azure.AI.Translation.Text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ We guarantee that all client instance methods are thread-safe and independent of

## Examples

The following section provides several code snippets using the `client` [created above](#create-a-texttranslationclient-using-an-api-key-and-region-credential), and covers the main features present in this client library. Although most of the snippets below make use of asynchronous service calls, keep in mind that the `Azure.AI.Translation.Text` package supports both synchronous and asynchronous APIs.
The following section provides several code snippets using the `client` [created above](#create-a-texttranslationclient-using-an-api-key-and-region-credential), and covers the main features present in this client library. Although the snippets below make use of synchronous service calls, keep in mind that the `Azure.AI.Translation.Text` package supports both synchronous and asynchronous APIs.

### Get Supported Languages

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

```C# Snippet:GetTextTranslationLanguagesAsync
```C# Snippet:GetTextTranslationLanguages
try
{
Response<GetLanguagesResult> response = await client.GetLanguagesAsync(cancellationToken: CancellationToken.None).ConfigureAwait(false);
Response<GetLanguagesResult> response = client.GetLanguages(cancellationToken: CancellationToken.None);
GetLanguagesResult languages = response.Value;

Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}.");
Expand All @@ -126,13 +126,13 @@ Please refer to the service documentation for a conceptual discussion of [langua

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

```C# Snippet:GetTextTranslationAsync
```C# Snippet:GetTextTranslation
try
{
string targetLanguage = "cs";
string inputText = "This is a test.";

Response<IReadOnlyList<TranslatedTextItem>> response = await client.TranslateAsync(targetLanguage, inputText).ConfigureAwait(false);
Response<IReadOnlyList<TranslatedTextItem>> response = client.Translate(targetLanguage, inputText);
IReadOnlyList<TranslatedTextItem> translations = response.Value;
TranslatedTextItem translation = translations.FirstOrDefault();

Expand All @@ -154,7 +154,7 @@ Please refer to the service documentation for a conceptual discussion of [transl

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

```C# Snippet:GetTransliteratedTextAsync
```C# Snippet:GetTransliteratedText
try
{
string language = "zh-Hans";
Expand All @@ -163,7 +163,7 @@ try

string inputText = "这是个测试。";

Response<IReadOnlyList<TransliteratedText>> response = await client.TransliterateAsync(language, fromScript, toScript, inputText).ConfigureAwait(false);
Response<IReadOnlyList<TransliteratedText>> response = client.Transliterate(language, fromScript, toScript, inputText);
IReadOnlyList<TransliteratedText> transliterations = response.Value;
TransliteratedText transliteration = transliterations.FirstOrDefault();

Expand All @@ -184,12 +184,12 @@ Please refer to the service documentation for a conceptual discussion of [transl

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

```C# Snippet:FindTextSentenceBoundariesAsync
```C# Snippet:FindTextSentenceBoundaries
try
{
string inputText = "How are you? I am fine. What did you do today?";

Response<IReadOnlyList<BreakSentenceItem>> response = await client.FindSentenceBoundariesAsync(inputText).ConfigureAwait(false);
Response<IReadOnlyList<BreakSentenceItem>> response = client.FindSentenceBoundaries(inputText);
IReadOnlyList<BreakSentenceItem> brokenSentences = response.Value;
BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault();

Expand All @@ -211,14 +211,14 @@ Please refer to the service documentation for a conceptual discussion of [break

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

```C# Snippet:LookupDictionaryEntriesAsync
```C# Snippet:LookupDictionaryEntries
try
{
string sourceLanguage = "en";
string targetLanguage = "es";
string inputText = "fly";

Response<IReadOnlyList<DictionaryLookupItem>> response = await client.LookupDictionaryEntriesAsync(sourceLanguage, targetLanguage, inputText).ConfigureAwait(false);
Response<IReadOnlyList<DictionaryLookupItem>> response = client.LookupDictionaryEntries(sourceLanguage, targetLanguage, inputText);
IReadOnlyList<DictionaryLookupItem> dictionaryEntries = response.Value;
DictionaryLookupItem dictionaryEntry = dictionaryEntries.FirstOrDefault();

Expand All @@ -240,7 +240,7 @@ Please refer to the service documentation for a conceptual discussion of [dictio

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

```C# Snippet:GetGrammaticalStructureAsync
```C# Snippet:GetGrammaticalStructure
try
{
string sourceLanguage = "en";
Expand All @@ -250,7 +250,7 @@ try
new InputTextWithTranslation("fly", "volar")
};

Response<IReadOnlyList<DictionaryExampleItem>> response = await client.LookupDictionaryExamplesAsync(sourceLanguage, targetLanguage, inputTextElements).ConfigureAwait(false);
Response<IReadOnlyList<DictionaryExampleItem>> response = client.LookupDictionaryExamples(sourceLanguage, targetLanguage, inputTextElements);
IReadOnlyList<DictionaryExampleItem> dictionaryEntries = response.Value;
DictionaryExampleItem dictionaryEntry = dictionaryEntries.FirstOrDefault();

Expand All @@ -275,10 +275,10 @@ When you interact with the Translator Service using the Text Translation client

For example, if you submit a translation request without a target translate language, a `400` error is returned, indicating "Bad Request".

```C# Snippet:HandleBadRequestAsync
```C# Snippet:HandleBadRequest
try
{
var translation = await client.TranslateAsync(Array.Empty<string>(), new[] { "This is a Test" }).ConfigureAwait(false);
var translation = client.Translate(Array.Empty<string>(), new[] { "This is a Test" });
}
catch (RequestFailedException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ This sample demonstrates how to get languages that are supported by other operat

This will return language metadata from all supported scopes.

```C# Snippet:GetTextTranslationLanguagesMetadataAsync
```C# Snippet:GetTextTranslationLanguagesMetadata
try
{
Response<GetLanguagesResult> response = await client.GetLanguagesAsync().ConfigureAwait(false);
Response<GetLanguagesResult> response = client.GetLanguages();
GetLanguagesResult languages = response.Value;

Console.WriteLine($"Number of supported languages for translate operation: {languages.Translation.Count}.");
Expand Down Expand Up @@ -45,11 +45,11 @@ catch (RequestFailedException exception)

You can limit the scope of the response of the languages API by providing the optional paramter `scope`. A comma-separated list of names defining the group of languages to return. Allowed group names are: `translation`, `transliteration` and `dictionary`. If no scope is given, then all groups are returned, which is equivalent to passing `translation,transliteration,dictionary`.

```C# Snippet:GetTextTranslationLanguagesByScopeAsync
```C# Snippet:GetTextTranslationLanguagesByScope
try
{
string scope = "translation";
Response<GetLanguagesResult> response = await client.GetLanguagesAsync(scope: scope).ConfigureAwait(false);
Response<GetLanguagesResult> response = client.GetLanguages(scope: scope);
GetLanguagesResult languages = response.Value;

Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}.");
Expand Down Expand Up @@ -86,11 +86,11 @@ catch (RequestFailedException exception)
You can select the language to use for user interface strings. Some of the fields in the response are names of languages or names of regions. Use this parameter to define the language in which these names are returned. The language is specified by providing a well-formed BCP 47 language tag. For instance, use the value `fr` to request names in French or use the value `zh-Hant` to request names in Chinese Traditional.
Names are provided in the English language when a target language is not specified or when localization is not available.

```C# Snippet:GetTextTranslationLanguagesByCultureAsync
```C# Snippet:GetTextTranslationLanguagesByCulture
try
{
string acceptLanguage = "es";
Response<GetLanguagesResult> response = await client.GetLanguagesAsync(acceptLanguage: acceptLanguage).ConfigureAwait(false);
Response<GetLanguagesResult> response = client.GetLanguages(acceptLanguage: acceptLanguage);
GetLanguagesResult languages = response.Value;

Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}.");
Expand Down
Loading

0 comments on commit e340b46

Please sign in to comment.