Skip to content

Azure Translate setup

Adam edited this page Sep 10, 2019 · 10 revisions

To enable the notifier the ability to leverage Azure Translation, you'll need a Translation API setup. The following will walk you through this brief setup and how you can test the service outside of the management pack to make sure all is working.

  1. Sign into https://portal.azure.com with the credentials/account you'll use to create the service.
  2. Create a New Resource, searching for "Translator Text" and choosing Create.
  3. Name your new Translator service, place into a current or new Resource Group, and finally choose your pricing tier. At the time of this publication, Microsoft charges per the number of characters translated. To better quantify this, a 30 page document has about 17,000 characters and the seven Harry Potter books combined total about 60 million characters.*
  4. Once it's been provisioned, head into said resource and view the "Keys" section

createCogSvc

Using one of the keys you can use the following to test connectivity and translation functionality.

Take the following PowerShell functions that enable translation between languages. In the first function, we identify the source language to use as an input to the second function that is responsible for the translation. In this example, we'll convert the English statement of "Just let me know when you are available and I will call you" to Spanish.

#Plug in your ACS API key
$azureCogSvcTranslateAPIKey = ""

#determine the language being used before converting it
function Get-AzureEmailLanguage ($TextToEvaluate)
{  
    #build the request
    $translationServiceURI = "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0"
    $RecoRequestHeader = @{
      'Ocp-Apim-Subscription-Key' = "$azureCogSvcTranslateAPIKey";
      'Content-Type' = "application/json"
    }

    #prepare the body of the request
    $TextToEvaluate = @{'Text' = $($TextToEvaluate)} | ConvertTo-Json

    #Send text to Azure for translation
    $RecoResponse = Invoke-RestMethod -Method POST -Uri $translationServiceURI -Headers $RecoRequestHeader -Body "[$($TextToEvaluate)]"

    #Return the language with the highest match score
    return $RecoResponse | sort-object score | select-object -first 1
}

#translate the language
function Get-AzureEmailTranslation ($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";
      '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)
}

#process
$statement = "Just let me know when you are available and I will call you"
$detectedLanguage = Get-AzureEmailLanguage -TextToEvaluate $statement
if ($detectedLanguage.isTranslationSupported -eq $true)
{
    #convert the statement to spanish
    Get-AzureEmailTranslation -TextToTranslate $statement -SourceLanguage "$($detectedLanguage.language)" -TargetLanguage "es"
}

In this example, the English statement of

Just let me know when you are available and I will call you

Returns the following in Spanish

Sólo avísame cuando estés disponible y te llamaré

*Sources
Microsoft Translator FAQ

Clone this wiki locally