Create your resource and get the key for Translator cognitive service here

I am using authentication with multi-service resource, if you want to refer to other authentication types. Refer here

When you use a Cognitive Service's multi-service resource. This allows you to use a single secret key to authenticate requests for multiple services.

When you use a multi-service secret key, you must include two authentication headers with your request. There are two headers that you need to call the Translator.

Headers Description
Ocp-Apim-Subscription-Key The value is the Azure secret key for your multi-service resource.
Ocp-Apim-Subscription-Region The value is the region of the multi-service resource.

Region is required for the multi-service Text API subscription. The region you select is the only region that you can use for text translation when using the multi-service subscription key, and must be the same region you selected when you signed up for your multi-service subscription through the Azure portal.

Available regions australiaeastbrazilsouthcanadacentralcentralindiacentraluscentraluseuapeastasiaeastuseastus2francecentraljapaneastjapanwestkoreacentralnorthcentralusnortheuropesouthcentralussoutheastasiauksouthwestcentraluswesteuropewestuswestus2, and southafricanorth.

PowerShell code:

#Plug in your ACS API key  $azureCogSvcTranslateAPIKey = "" #provide your resource region   $azureRegion = ""  #translate the language  function GetTranslation ($TextToTranslate, $SourceLanguage, $TargetLanguage)  {        #build the request      $translationServiceURI = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=$($SourceLanguage)&to=$($TargetLanguage)"      $RecoRequestHeader = @{        'Ocp-Apim-Subscription-Key' = "$azureCogSvcTranslateAPIKey";  	  'Ocp-Apim-Subscription-Region'="$azureRegion"        'Content-Type' = "application/json"      }        #prepare the body of the request      $TextToTranslate = @{'Text' = $($TextToTranslate)} | ConvertTo-Json        #Send text to Azure for translation      $RecoResponse = Invoke-RestMethod -Method POST -Uri $translationServiceURI -Headers $RecoRequestHeader -Body "[$($TextToTranslate)]"        #Return the converted text      return $($RecoResponse.translations[0].text)  }      $text= "Translating text from english to french"      GetTranslation -TextToTranslate $text-SourceLanguage "en" -TargetLanguage "fr"

Hope its helpful..! 🙂


This free site is ad-supported. Learn more