Skip to content

Commit 27e6435

Browse files
Merge pull request #211281 from laujan/1987340-update-qs-with-region-instructions
clarify header:location requirement
2 parents 6694119 + af094c3 commit 27e6435

File tree

2 files changed

+226
-162
lines changed

2 files changed

+226
-162
lines changed

articles/cognitive-services/Translator/quickstart-translator.md

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: nitinme
88
ms.service: cognitive-services
99
ms.subservice: translator-text
1010
ms.topic: quickstart
11-
ms.date: 07/28/2022
11+
ms.date: 09/14/2022
1212
ms.author: lajanuar
1313
ms.devlang: csharp, golang, java, javascript, python
1414
---
@@ -17,17 +17,18 @@ ms.devlang: csharp, golang, java, javascript, python
1717
<!-- markdownlint-disable MD001 -->
1818
<!-- markdownlint-disable MD024 -->
1919
<!-- markdownlint-disable MD036 -->
20+
<!-- markdownlint-disable MD049 -->
2021

2122
# Quickstart: Azure Cognitive Services Translator
2223

2324
In this quickstart, you'll get started using the Translator service to [translate text](reference/v3-0-translate.md) with a programming language of your choice and the REST API.
2425

2526
> [!NOTE]
2627
>
27-
> * For this quickstart it is recommended that you use a Translator text single-service subscription.
28-
> * With the single-service subscription you'll include one authorization header (**Ocp-Apim-Subscription-key**) with the REST API request. The value for Ocp-Apim-Subscription-key is your Azure secret key for your Translator Text subscription.
29-
> * If you choose to use the multi-service Cognitive Services subscription, it requires two authentication headers (**Ocp-Api-Subscription-Key** and **Ocp-Apim-Subscription-Region**). The value for Ocp-Apim-Subscription-Region is the region associated with your subscription.
30-
> * For more information on how to use the Ocp-Apim-Subscription-Region header, _see_ [Use the Text Translator APIs](translator-text-apis.md).
28+
> * For this quickstart it is recommended that you use a Translator text single-service global resource.
29+
> * With a single-service global resource you'll include one authorization header (**Ocp-Apim-Subscription-key**) with the REST API request. The value for Ocp-Apim-Subscription-key is your Azure secret key for your Translator Text subscription.
30+
> * If you choose to use the multi-service Cognitive Services or regional Translator resource, two authentication headers will be required: (**Ocp-Api-Subscription-Key** and **Ocp-Apim-Subscription-Region**). The value for Ocp-Apim-Subscription-Region is the region associated with your subscription.
31+
> * For more information on how to use the **Ocp-Apim-Subscription-Region** header, _see_ [Text Translator REST API headers](translator-text-apis.md).
3132
3233
## Prerequisites
3334

@@ -52,13 +53,12 @@ To call the Translator service via the [REST API](reference/rest-api-guide.md),
5253

5354
For more information on Translator authentication options, _see_ the [Translator v3 reference](./reference/v3-0-reference.md#authentication) guide.
5455

55-
|Header|Value| Condition |
56+
Header|Value| Condition |
5657
|--- |:--- |:---|
57-
|**Ocp-Apim-Subscription-Key** |Your Translator service key from the Azure portal.|<ul><li>***Required***</li></ul> |
58-
|**Content-Type**|The content type of the payload. The accepted value is **application/json** or **charset=UTF-8**.|<ul><li>***Required***</li></ul>|
59-
|**Content-Length**|The **length of the request** body.|<ul><li>***Optional***</li></ul> |
60-
|**X-ClientTraceId**|A client-generated GUID to uniquely identify the request. You can omit this header if you include the trace ID in the query string using a query parameter named ClientTraceId.|<ul><li>***Optional***</li></ul>
61-
|||
58+
|**Ocp-Apim-Subscription-Key** |Your Translator service key from the Azure portal.|&bullet; ***Required***|
59+
|**Ocp-Apim-Subscription-Region**|The region where your resource was created. |&bullet; ***Required*** when using a multi-service Cognitive Services or regional (non-global) resource.</br>&bullet; ***Optional*** when using a single-service global Translator Resource.
60+
|**Content-Type**|The content type of the payload. The accepted value is **application/json** or **charset=UTF-8**.|&bullet; **Required**|
61+
|**Content-Length**|The **length of the request** body.|&bullet; ***Optional***|
6262

6363
> [!IMPORTANT]
6464
>
@@ -135,6 +135,10 @@ class Program
135135
private static readonly string key = "<your-translator-key>";
136136
private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com";
137137

138+
// location, also known as region.
139+
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
140+
private static readonly string location = "<YOUR-RESOURCE-LOCATION>";
141+
138142
static async Task Main(string[] args)
139143
{
140144
// Input and output languages are defined as parameters.
@@ -151,6 +155,8 @@ class Program
151155
request.RequestUri = new Uri(endpoint + route);
152156
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
153157
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
158+
// location required if you're using a multi-service or regional (not global) resource.
159+
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
154160

155161
// Send the request and get response.
156162
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
@@ -246,11 +252,13 @@ import (
246252

247253
func main() {
248254
key := "<YOUR-TRANSLATOR-KEY>"
249-
// Add your location, also known as region. The default is global.
250-
// This is required if using a Cognitive Services resource.
251255
endpoint := "https://api.cognitive.microsofttranslator.com/"
252256
uri := endpoint + "/translate?api-version=3.0"
253257

258+
// location, also known as region.
259+
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
260+
location := "<YOUR-RESOURCE-LOCATION>"
261+
254262
// Build the request URL. See: https://go.dev/pkg/net/url/#example_URL_Parse
255263
u, _ := url.Parse(uri)
256264
q := u.Query()
@@ -274,6 +282,8 @@ func main() {
274282
}
275283
// Add required headers to the request
276284
req.Header.Add("Ocp-Apim-Subscription-Key", key)
285+
// location required if you're using a multi-service or regional (not global) resource.
286+
req.Header.Add("Ocp-Apim-Subscription-Region", location)
277287
req.Header.Add("Content-Type", "application/json")
278288

279289
// Call the Translator API
@@ -433,6 +443,10 @@ import okhttp3.Response;
433443

434444
public class TranslatorText {
435445
private static String key = "<your-translator-key";
446+
447+
// location, also known as region.
448+
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
449+
private static String location = "<YOUR-RESOURCE-LOCATION>";
436450

437451

438452
// Instantiates the OkHttpClient.
@@ -447,6 +461,8 @@ public class TranslatorText {
447461
.url("https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=fr&to=zu")
448462
.post(body)
449463
.addHeader("Ocp-Apim-Subscription-Key", key)
464+
// location required if you're using a multi-service or regional (not global) resource.
465+
.addHeader("Ocp-Apim-Subscription-Region", location)
450466
.addHeader("Content-type", "application/json")
451467
.build();
452468
Response response = client.newCall(request).execute();
@@ -498,22 +514,18 @@ After a successful call, you should see the following response:
498514

499515
```json
500516
[
501-
{
502-
"detectedLanguage": {
503-
"language": "en",
504-
"score": 1.0
505-
},
506-
"translations": [
507-
{
508-
"text": "J'aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!",
509-
"to": "fr"
510-
},
511-
{
512-
"text": "Ngingathanda ngempela ukushayela imoto yakho endaweni evimbelayo izikhathi ezimbalwa!",
513-
"to": "zu"
514-
}
515-
]
516-
}
517+
{
518+
"translations": [
519+
{
520+
"text": "J'aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!",
521+
"to": "fr"
522+
},
523+
{
524+
"text": "Ngingathanda ngempela ukushayela imoto yakho endaweni evimbelayo izikhathi ezimbalwa!",
525+
"to": "zu"
526+
}
527+
]
528+
}
517529
]
518530

519531
```
@@ -584,12 +596,18 @@ After a successful call, you should see the following response:
584596
let key = "<your-translator-key>";
585597
let endpoint = "https://api.cognitive.microsofttranslator.com";
586598

599+
// location, also known as region.
600+
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
601+
let location = "<YOUR-RESOURCE-LOCATION>";
602+
587603
axios({
588604
baseURL: endpoint,
589605
url: '/translate',
590606
method: 'post',
591607
headers: {
592608
'Ocp-Apim-Subscription-Key': key,
609+
// location required if you're using a multi-service or regional (not global) resource.
610+
'Ocp-Apim-Subscription-Region': location,
593611
'Content-type': 'application/json',
594612
'X-ClientTraceId': uuidv4().toString()
595613
},
@@ -630,10 +648,6 @@ After a successful call, you should see the following response:
630648
```json
631649
[
632650
{
633-
"detectedLanguage": {
634-
"language": "en",
635-
"score": 1.0
636-
},
637651
"translations": [
638652
{
639653
"text": "J'aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!",
@@ -687,9 +701,9 @@ import requests, uuid, json
687701
key = "<your-translator-key>"
688702
endpoint = "https://api.cognitive.microsofttranslator.com"
689703

690-
# Add your location, also known as region. The default is global.
691-
# This is required if using a Cognitive Services resource.
692-
location = "YOUR_RESOURCE_LOCATION"
704+
# location, also known as region.
705+
# required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
706+
location = "<YOUR-RESOURCE-LOCATION>"
693707

694708
path = '/translate'
695709
constructed_url = endpoint + path
@@ -702,6 +716,8 @@ params = {
702716

703717
headers = {
704718
'Ocp-Apim-Subscription-Key': key,
719+
# location required if you're using a multi-service or regional (not global) resource.
720+
'Ocp-Apim-Subscription-Region': location,
705721
'Content-type': 'application/json',
706722
'X-ClientTraceId': str(uuid.uuid4())
707723
}
@@ -739,22 +755,18 @@ After a successful call, you should see the following response:
739755

740756
```json
741757
[
742-
{
743-
"detectedLanguage": {
744-
"language": "en",
745-
"score": 1.0
746-
},
747-
"translations": [
748-
{
749-
"text": "J'aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!",
750-
"to": "fr"
751-
},
752-
{
753-
"text": "Ngingathanda ngempela ukushayela imoto yakho endaweni evimbelayo izikhathi ezimbalwa!",
754-
"to": "zu"
755-
}
756-
]
757-
}
758+
{
759+
"translations": [
760+
{
761+
"text": "J'aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!",
762+
"to": "fr"
763+
},
764+
{
765+
"text": "Ngingathanda ngempela ukushayela imoto yakho endaweni evimbelayo izikhathi ezimbalwa!",
766+
"to": "zu"
767+
}
768+
]
769+
}
758770
]
759771

760772
```

0 commit comments

Comments
 (0)