Polly and Translate – Sugar and spice <3

I’ve been using Amazon Polly for a while now to create my mini audiobooks. But more about that another time – Amazon Translate just went Generally Available!

For the uninitiated, that means it’s out of Beta and available to every one of us. Yay! 🙂

Let’s see what we can do with it!

How many tools do we have to play around with?

Just a single one, ConvertTo-TRNTargetLanguage. What can it do?

Starting with my favorite command, Get-Help :

That easy? Let’s try!

OK, a bit of duplication weirdness here which should have probably been picked up during Beta testing, but otherwise as expected. And cmdlet is pretty self-explanatory, too. Didn’t really need that help, did we? 🙂

What we really need now, and Get-Help doesn’t provide is another simple thing – A complete list of supported languages!

This took a bit of looking under the hood to find, but the end result is this:
Spanish, German, French, Portuguese, Arabic and Chinese.

Last two will generate weird output characters if your environment isn’t properly configured for them. But then again, why’d you use them if it isn’t? 🙂

So let’s take a stab at translating some of the universally useful stuff. We’ll take the two most common sentences travelers around the world use to ensure their personal safety and translate them to all of the languages in one loop. You can’t say we’re not looking after you here, right?

Encoding is a mess here but that’s what you get when you use unsupported characters in your PowerShell and paste them into a code block plugin which is probably just as much unsupported. Support the stuff you need and it will all be just fine.

Alright, so what did we learn so far?
1. Use language codes, not the language names.
2. You can translate from other languages to English and from English to other languages.

This thing is pretty new so if you think it’s not flexible enough just give it some time. Better yet, submit a feature request to AWS to help push changes forward. That’s a good way to help the development of the product you like – new recommendations and ideas for new features.

OK, so this has been the Amazon Translate part. Where’s the Polly I promised you?

It’s… tricky.

Polly is the bigger part of the code, yet I don’t really have time to dissect its code like I did with Translate, for two reasons. First, it’s an order of magnitude more complicated and this blog post is rapidly approaching the size of a small novel.

So, Polly – The Quick Way.

To pull this off, we need an audio player to play our converted clips. VLC or MPlayer will do just fine. Make sure the path is correct, although the script will warn you regardless and fall back to translation-only mode.

Next, a temporary path to save translated text and encoded audio. By default it’s home (~) so default will work on Linux and Windows. Presently, only Windows is working as expected. Linux will return translation results only, no Polly.

Helpful option for Linux can be a -Quiet switch. It stops Polly from running and displays only the text output.

In essence, priority is to define our text in English and the voice we want to use. If text is not defined, one of three default sentences will be used. If voice isn’t defined, one will be selected from available voices for that language.

How are we doing this?

If $Voice is left at default value Random we’ll get a random voice from the list of a few $PollyVoices.

How did we get that list of voices?

Pretty easy, in fact. Let’s say that we’re using German language and we haven’t decided on the voice to use. We will end up with three choices:

Then it’s up to Get-Random to select one for us. Of course, we can set $Voice to Salli or Joanna, two arguably best Polly voices, but they wouldn’t be able to handle German language properly. Still, it’s worth a try as hilarious results can be produced, like when Ricardo reads French 😉

Now we have all the information. Let’s see how our translation becomes a voice and gets played back to us:

What in the world is this, right?! 🙂

We’re creating a new FileStream object and setting it to a location of our mp3 file. Next, the text content is pulled from the translation file. Polly is instructed to use this text with the given voice and to encode it in the format we want. Stream is saved and closed.

Here we’re displaying the original text first, and Polly voice details and translation below it. And, finally, our audio player is started! Hopefully parameters are set so that it’s invisible while playing and automatically closes after the clip is complete.

Then we do the cleanup because our mom taught us to clean up behind us and because it is also a nice thing to do. Say “NO!” to litter 😉

And that’s it. We know how to get our text translated and how to get Polly to speak it back to us. Personally, I’ve been having fun with this for far longer than anyone has any business to.

But I <3 Polly because she reads my books to me 🙂 Next time I'll show you how she can do it for you, too 🙂 There are plenty of script parameters and switches you can use, so make sure to run that beautiful Get-Help cmdlet. If you can't remember how it works, fire the Get-POLAudioTranslation.ps1 -Help switch. It will display the script information and ways to obtain help in a nice way:

Here is the screenshot to provide a more-than-a-thousand-word review of the script in action, like we need more words:
Perhaps a better way to see the script in action is by checking out this video (2.2 MB).

As usual, Get the code (and any future updates) from GitHub: https://github.com/PowerSix/MyPowerShellSpace

Keep in mind that blog posts are NOT getting updated! Look for script updates on GitHub!

And, finally, here’s the complete script:

Leave a Comment