Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active August 27, 2018 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/0930c83c7bb8279daa92a6bff3e60b6a to your computer and use it in GitHub Desktop.
Save darrenjrobinson/0930c83c7bb8279daa92a6bff3e60b6a to your computer and use it in GitHub Desktop.
Azure Cognitive Services Text Language Translation
# API Key from https://azure.microsoft.com/en-us/services/cognitive-services/translator-text-api/
$apiKey = "yourAPIKey"
# Translation API
$translateBaseURI = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
# Convert from - en = English
$fromLang = "en"
# Supported Languages https://docs.microsoft.com/en-us/azure/cognitive-services/translator/languages
# Convert to - de = German
$toLang = "de"
# API Auth Headers
$headers = @{}
$headers.Add("Ocp-Apim-Subscription-Key",$apiKey)
$headers.Add("Content-Type","application/json")
# Conversion URI
$convertURI = "$($translateBaseURI)&from=$($fromLang)&to=$($toLang)"
# Text to Convert
$textToConvert = "This text has been converted from English to German using Azure Cognitive Services"
# Build Conversion Body
$text = @{'Text' = $($textToConvert)}
$text = $text | ConvertTo-Json
# Convert
$conversionResult = Invoke-RestMethod -Method POST -Uri $convertURI -Headers $headers -Body "[$($text)]"
write-host -ForegroundColor 'yellow' "'$($textToConvert)' converted to '$($conversionResult.translations[0].text)'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment