Borbin the 🐱

Translate to any language using PowerShell

01 June, 2024


Google's online translation service is an easy way to translate text into any language.
For example:

[string]$text = "The picture."
online-translate "de" $text

The returned translation is "Das Bild."


Or:

[string]$text = "The picture search is finished. %1!d! pictures have been scanned and %2!d! duplicate pictures were found.\nWould you like to see the list with the duplicate picture names?"
online-translate "de" $text

The returned translation is "Die Bildsuche ist beendet. %1!d! Bilder wurden gescannt und %2!d! Es wurden doppelte Bilder gefunden. Möchten Sie die Liste mit den doppelten Bildnamen sehen?"

Please note the unmodified placeholders (Ensure to use the REST API setting 'client=gtx').


This is all done with a PowerShell script using the public Google REST API:

<#
.DESCRIPTION
    Translate any text for a language using the google online translation service.
#>
function online-translate([string]$language, [string]$text) {

    # Escape newlines.
    $text = $text.Replace("`n", '\n')

    # The google rest API.
    [string]$uri = "https://translate.googleapis.com/translate_a/single?client=gtx&tl=$language&q=$text&sl=auto&dt=t"
    [string]$response = (Invoke-WebRequest -Uri $uri -Method Get).Content

    # Combine the segments of the response to a single string.
    # Regex is rather simple: Use the start pattern '[[["', or the segment pattern ']]]],["'
    # to capture the sentences in the text group.
    $m = ([regex]'(?:(?:^\[\[\[)|(?:\]\]\]\],\[))"(?<text>.*?)",".*?",null').Matches($response)
    [string]$translation = ($m | % { $_.groups['text'].Value }) -join ""
    
    # Adjust the translated text.
    $translation.Replace('\"', '"').Replace('\\n', "`n").Replace('\\r', "`r").Replace('[TAB]', "`t").Replace('\u003c', '<').Replace('\u003e', ">").Replace('\u003d', "=").Replace('\\\\', "\\")
}


The REST API response is more complex than the call itself, but with a simple regex this problem is easily solved in the function.
The starting pattern '[[["' or the segment pattern ']]]],["' is used to capture the sentences in the text group.
The number of segments depends on the text input. For example:

Small text return a single segment response:

[[["Das Bild.","Das Bild.",null,null,5]],null,"de",null,null,null,1,[],[["de"],null,[1],["de"]]]


Larger text return a multi segment response:

[[["Die Bildsuche ist beendet. ","The picture search is finished.",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]],["%1!d! ","%1!d!",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]],["Bilder wurden gescannt und %2!d! ","pictures have been scanned and %2!d!",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]],["Es wurden doppelte Bilder gefunden.\\nMöchten Sie die Liste mit den doppelten Bildnamen sehen?","duplicate pictures were found.\\nWould you like to see the list with the duplicate picture names?",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]]],null,"en",null,null,null,1,[],[["en"],null,[1],["en"]]]