Today I will show how I went about automating google translate to speed up the translation of a large set of terms I needed for one of my apps, TravelRates.
The actual translation is very simple, however, since it is based on the paid version of google translation it does require that you have a Google Console developer account set up.
Automating your translations saves time
Before getting started I want to quickly mention why I think this is worthwhile. I was localizing my app and had about ~300 terms, country names to be specific, I needed to translate to two Swedish and Japanese. Instead of going through each and every one, copying and pasting in my JSON base file, I decided to script this.
The result was that I got to spend time doing something I enjoy, coding, and I sped up the process of the actual translation a lot. I still had to go through the end result to ensure all the terms had been translated correctly, Turkey as an example, ended up being A Turkey..
Translating “Turkey” alone in google translate will give you “A Turkey” in Japanese.
Photo by Mikkel Bergmann on Unsplash
Still, the entire process probably shaved off 70% of the time required in comparison to checking each and every term manually.
Getting the command-line tool
If you don’t already have Node.js I recommend you get it. Node is a wonderful tool if not a language and comes with so many helpful CLI tools ripe for the picking. The tool we will be using today, gtranslate, is one of those.
To install gtranslate run the npm global install command line anywhere on your computer:
npm install -g gtranslate
Getting an api key
This step is a bit of a pain in terms of overhead, especially if you only have a few phrases you need to get translated. But then again, if it’s only a few phrases, you probably wouldn’t be here to have it automated. Setting up an API key with Google Cloud isn’t free but for my couple of hundred of words translated, including debugging time, I paid $0.19. Definitely worth it.
Provided you already have a cloud account setup, follow these 13 steps below to get your google translate API key to register with the gtranslate tool.
First up open the google developer console.
Create a new application
Press New project
Enter application name and press create
Make sure you have selected the right application, this doesn’t always happen automatically and then click to enable APIs
Search for the Cloud Translation API and select it
Choose to enable the API
Enter the page to add a credential
Choose to create a credential
Skip directly to adding an API key since that is what will be used
Give your key a name and restrict it to only Cloud translation. You can also restrict it by app or IP which is a good idea
Copy the key, keep this secret
Next, to make sure we don’t lose all our money, go to quotas to restrict usage
I won’t delve into details here but you can easily modify your quotas to restrict how much this key is allowed to use the API
Scripting your translation
Actually automating your google translation is the easy part. First, we need to register the API key we got in the steps above:
gtranslate --register <your key>
I use node.js to script my translation because I’m comfortable with Javascript and it’s natively very good for working with JSON. However, since the gtranslate is a command line tool, of course, you can use it with Python, Ruby or any other script engine you like.
As an example, I will use the country-json repository with a list of countries as input:
[{
"country": "Afghanistan"
},
{
"country": "Albania"
}
]
This I will massage into a simple country:translation hash map. Using this code:
This will give you a neat json output like this:
[
{
"name": "Angola",
"translation": "Angola"
},
{
"name": "Anguilla",
"translation": "Anguila"
},
{
"name": "Antarctica",
"translation": "Antártida"
},
{
"name": "Antigua and Barbuda",
"translation": "Antigua y Barbuda"
}
]
If you want to try it without risking getting billed just apply a slice on the input data and translate only a few terms.
It’s as simple as that.
Closing comments
GTranslate is a simple command line tool to call google translate. It does require you to have a google cloud account but setting one up is easy and won’t cost you much.
Using node.js to automate google translate this way is dead simple and you get the data in the format you want, json, xml or something else.
Be careful though! Even google translate isn’t that clever yet. If you feed google translate just “Turkey” it has no idea you’re thinking about the country and not a bird. Always double check the output.
Good luck!
(feature photo, the Japanese Pepper robot by Alex Knight on Unsplash)