The project that my team is tackling at work is a new employee portal that user will land on after logging in. One of the requirements is that we support multiple languages. I looked at the Microsoft Text Translator API to translate the web page but that leads into a couple other sub-requirements. First is we only want to translate our static content; labels, navigation menu, etc. Our customers are organizations and they have told us that they do not want us to translate their content, alerts to employees and such. The second sub-requirement is that we don't want to have to make an additional service to translate each page as it is rendered. So with those two additional requirements that killed passing the entire HTML page to Azure. Currently we have a single JSON document that contains all of the messages in different languages and we use that as a look. That works fine for now, but it has a few flaws that would prevent it from scaling well. First, every time we add a new label we have to add it to the main English lookup and manually translate it and add it to each other language we support (currently we support English, Spanish, and Polish). Secondly, if we ever wanted to support additional languages we would have to manually translate each element which would be very time consuming. With those issues in mind I took a second look at the Text Translator API to see if I could make it work for my use case.
The REST Interface
While reading through the online documents I found the API Reference for the REST interface. I first looked at the TranslateArray endpoint, but decided against using it. Firstly, my source document is already over the 10,000 character limit for a single call and I didn't want to manage chucking the request. Secondly, I wanted to make sure that each translated word could still be matched with the lookup key. After giving up on the TranslateArray I started looking at the Translate endpoint. I settled on the Translate endpoint mainly due to being able to match the lookup key with the word after it has been translated. Since the translate endpoint only supports a single word or phrase at a time I set about writing a small application that would loop over the messages, call the Text API, match the translated word with the lookup key and finally write the translated file to disk.
Translator Console App
I've been learning F# over the past few months so I decided I would take this as an opportunity work in the language and get some more practice. I started by creating a new DotNet-Core2 Console Application in Visual Studio 2017. The code is fairly simple, you pass the arguments to the command line, the application then tries to load the file from disk, JSON serialize it into a F# record, then loop over each message calling the Text API before returning a new record with the translated text and finally writing a file to disk. You can check out the source at my Github.
Setting Up Azure
The Text Translator API is part of Microsoft's Cognitive Services AI. In your azure portal click on 'Create a resource' and search for Translator Text API.
Next you'll have to go through the usual step of giving it a name, selecting a subscription, resource group, and pricing tier. The free tier is more than enough for testing and development. I've only used around 40,000 characters during development. After the API has been created and deployed you should be taken to the Quick Start section.
In order to use my console app you'll need one of the Keys for this API. The keys can be found by clicking on Keys on the menu section and coping either Key 1 or Key 2. That's really it for setting up the API. I found this to be one of the easier and quicker Azure setups.
Final Thoughts
That's it. I wanted a simple app that I could (hopefully) connect to my build process so that my team and I wouldn't have to manually update a file each time we added some new content that needed to be translated. Let me know what you think and as always. Happy Coding.
Sean Wernimont The Blind Squirrel Copyright 2015-2020
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|