Developer Hack: Google Translate can help you translate code (and even translation files)

Photo by Pixabay on

Localization of your software or app is a good thing. It makes the app easier to use for end users. Which makes them actually want to use your app! Unfortunately, that means that you, the developer, also have to actually translate a bunch of things.

I don’t know your multilingual situation, but for me that’s a pain in the butt because I am only confident in my knowledge of two languages: Dutch and the one I am currently writing in. If you are using Google Translate to read this page… It’s not this one. It’s English.

When translations aren’t provided by your client or the person responsible for providing them, that means the job of translating falls upon the shoulders of the developer. Who’d rather be learning a new programming language than an actual language, am I right?

What if I told you that Google Translate is your friend? Now, I can hear some of you think “Yes, Google Translate is great but I’ll still have to mess around with a lot of code just to get the translations in there.”

Which would be true, even if you are using localization modules of a package. In my case, I was working on Laravel which uses PHP pages with strings. Which is pretty easy to update, except… it’s still a chore even when using Google Translate to translate the terms.

Or it would be, if Google Translate weren’t a developers’ best friend. Because Google Translate is actually pretty good at figuring out at what it should or shouldn’t try to translate when you paste code. In most cases, it’ll only translate your language strings which you want to touch. Code inside your HTML tags? Not translated. Your placeholders in translation files? Untouched, Google Translate only translates the translation string.

Here’s two examples of Google Translate in action

In this example, I posted part of my localization file fom Laravel. If you look at the right, you’ll see that my placeholders are untouched. GT only translated the language strings. Result? I can just copy the output and paste it in my translation file.

Yes, language purists, those translations might not be a 100% correct. But they’re still better than my own translations!

And here’s Google Translate translating the HTML. Which doesn’t really have a purpose as I’m supposed to be using localization strings instead, but Google Translate doesn’t care. It just translates what it thinks I want to translate.

In summary, Google Translate makes translation your code a whole lot easier.

And if anyone objects, they are free to provide updates to your localization files, while you’re learning a new programming language. Win-win scenario!

Discover more from PowerUser Guide

Subscribe now to keep reading and get access to the full archive.

Continue reading