Use Google Translate to deeply translate JSON structures

Estonian, Dutch, & French translations

“Hey, can you get me that (‘copy’ || ‘design’ || ‘translations’ || ‘fonts’) at some point in the next two weeks? We’d like to have it in the app for the next demo”

My trust in humans hasn’t improved in the years that I have surrendered my life to knowledge of the computer world. In my experience, requests like those above are sure to go nowhere. You’re sure to demo your app with lorem ipsum in place of the copy. Otherwise, you’ll make your best guess as to what font to use. Probably both.

An area where I’ve run into the endless finger-pointing game of responsibility is certainly translations. It’s likely there is a good reason for this, I imagine this is the product managers thought sequence on translations:

  1. Do we have any translators on staff? (Likely No)
  2. Should I hire someone freelance to do it? Should I do it with an online service?
  3. Do we know all the text we need to translate?
  4. Can’t a robot do this?

In most cases, #2 is a pretty solid answer. Developing software is expensive. Translations a pretty inexpensive. Given the right team and business management, this should be a pretty light lift. But, let’s assume that’s not the case here.

As for programmers, we’re much better at helping product managers gather up all the text necessary for translation. With any structured format, we can collect all the English strings in the app. Since we’re a decent bet for English (or native language) translations, we can at least create the structure and tools for storing strings. Maybe it would look something like this:

{
"waterAnimal": "Angel Fish",
"landAnimal": "Giraffe",
"colors": [
{
"name": "red",
"shading": "dark"
},
{
"name": "green",
"shading": "light"
},
{
"name": "blue",
"shading": "deep"
}
],
"cost": {
"dollars": {
"primary": "Fifty",
"tax": "Ten"
},
"euros": {
"primary": "Forty Two",
"tax": "Eight"
}
}
}

So how do we motivate these folks to do their job and give us the translations? Typically, I have put poor translations in place. In other cases, I will define the string as “Ten (NEEDS SPANISH TRANSLATION).” While it gets the point across, it’s not always the best for formatting and language testing (QA is usually trying to work with whatever limited functionality they have at this point).

Enter Google Translate: The bain of my Spanish speaking colleagues. One thing I love about Google Translate is that it gets ‘close-ish’ ‘enough-ish’ to be annoying to native speakers. This is great, because it doesn’t let translators or product managers off the hook. Instead, it lays out a very simple template for what words to translate. PMs are translators are now magically motivated to do their job, otherwise they will look like poor translators, indeed.

The GTD-SCRIPTS series is all about scripts that ‘Get Things Done.’ I like simple, reusable, single-file scripts that do otherwise painstaking work in a matter of seconds, After all, this is what computers are for, and we are programmers. Here’s my short rulebook on GTD-SCRIPTS:

  1. Always write in a language and with libraries you know well (Node JS for me).
  2. Focus on general use (parameters, input/output).
  3. Keep it simple, stupid!

Performance, not a big issue. Unit testing, not worth it. Stick with many log statements and reusable, error-averse functions you know and love. I use many Lodash methods. I am not making an argument for Lodash here, I just know it well (see rule #1).

Translate Json GTD Script on Github

The translate-json.js node script is simple. It takes an api key, input .json file, and a comma separated list of language codes supported by Google Translate. Full list here. Here’s an example from the repo:

node translate-json YOUR_GOOGLE_API_KEY fixtures/translate-json/en.json fr,et,nl

This command takes the json object and does a deep translation (arrays included) to French, Estonian, and Dutch. It writes files to the /tmp directory because (see rule #3).

Bonus: Placing #!/usr/bin/env node at the top of a node js file makes it runnable like a normal bash script. Sometimes I add these node files to my path and then I can just run: translate-json X Y ZA,ZB,ZC anywhere on the command line.

Several Improvements:

  1. Caching words that have already been translated (saving $ on API calls).
  2. Specifying output file name and location.
  3. Skipping numbers, booleans, and other values that should not be translated.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.