Skip to main content

Using Google Translator to Internationalize your code

While developing a software, it is often desired that it would be available in multiple languages so that more people can use it. Some standard coding practices include storing messages visible to the user in a JSON or an XML file in English, and have it translated to multiple languages and save it as JSON or XML files with the corresponding language name. In this blog post, I am going to mock a bunch of messages in JSON format and use Google Translator to generate a JSON string with all messages translated. We shall be using Google Translator page and the developer tools,  and not the APIs.

As stated above, the first step is to create the mock JSON string having messages in English. I think the following would be good enough to represent the same loaded into a variable.
var en_json = {
"edit": "edit",
"open": "open",
"message_continue": "Do you wish to continue",
"message_error": "Error performing the action"
}

Next thing to do is open up Google Translator page and open up the JavaScript console in the Developer Tools. Upon inspecting the elements, I found that there is only one textarea tag in the page and that belongs to the input. We can get that loaded into a variable using the following JavaScript command.
var txtarea = document.getElementsByTagName('textarea')[0];
Similarly, the place where translated results are shown has a class name 'results-container'. Therefore, we can load that into a variable using the following JavaScript command.
var result = document.getElementsByClassName('results-container')[0];
We can test typing into the textarea and vieweing the result as shown below. By the way, I have German chosen as the results language.
txtarea.value = "Does it work?"
console.log(result.innerText) 
"Funktioniert es?
 "
It's working, Great!  Now all we need to do is iterate over all the messages in en_json and create a new variable named de_json. The following code snippet demonstrates the same.
var timeRequiredToTranslate = 5000;
var de_json = {};
var i = 0, j = 0;
var keys = [];
for (var key in en_json) {
    keys.push(key);
    setTimeout(function(){
        txtarea.value = en_json[keys[j]];
    }, (2*i - 1) * timeRequiredToTranslate); // Odd intervals
    
    setTimeout(function(){
        de_json[keys[j]] = result.innerText;
        j++;
    }, (2*i) * timeRequiredToTranslate); // Even intervals
    i++;
}
 
Although we named the output variable as 'de_json', its not stringified into JSON yet. To do that, run the following command.
console.log(JSON.stringify(de_json)) 
"{"edit":"bearbeiten\n ","open":"öffnen\n ","message_continue":"Möchten Sie fortfahren\n ","message_error":"Fehler beim Ausführen der Aktion\n "}"
It is not very difficult to write an automated process around this if you are actually thinking about building a Makefile out of this method. There may be certain optimizations that can be done to reduce the amount of time it takes to run the script. I have not tried it, but, perhaps event listeners on 'result' DIV could be used to know when the output would come up, store it and immediately edit the 'txtarea'. That way you can eliminate the setTimeout with definite time interval.

Comments

Popular posts from this blog

How to Create your own Ad Blocker Chrome Extension

Not very long ago, I read an article stating that Google banned advertisement blockers from its stores. There are several websites which does not let you in if you have an Ad blocker extension installed on your browser. I have not investigated on how these websites detect that, nor do I know whether our own version of advertisement blocker would be immune to that. But, isn't it fun to build something like this on our own? As the first step, I found a random website that has a Google Ad. I right clicked on the Google Ad and clicked on Inspect button. As I moved up the Elements inspector, I found that the id of the advertisement starts with "google_ads_". Therefore, I opened the console and typed in the following command: div = document.querySelector('[id^="google_ads_"]') This selected the div  that contained the advertisement. I went ahead and typed in the following command that would remove the div from the page. div.parentElement.remove...

Turning your phone into a virtual-joystick

Update: I kept working on this and I have released it as a package for Windows, Linux and macOS. Check it out: https://github.com/zenineasa/joystick/releases/tag/v1.0.0 -- During the days when I was pursying my master's programme, my friends and I used to occasionally go to a classroom in the university, turn on a projector, connect devices like Nintento Switch or Gaming computers and loads of joysticks, and play different simple multiplayer games; MarioKart was my favourite. From time-to-time, when I get together with people, I ponder that it would be a good idea if I bought such devices. Indeed, I do have a laptop, which could easily run such games; SuperTuxKart is similar enough to MarioKart and it can run on Linux, Windows and Mac. However, I do not have joysticks with me at the moment. Therefore, I think it would be a good idea if I simply worked on a project that would enable using our phones as joysticks. From a high-level, the plan is to host APIs on a NodeJS server that wo...

How to remove Blogger (or any free blogging platform) credits from the footer

I am not sure if removing the footer credits is legal or not, but I just wanted to try doing it. As I searched the internet, I found several pages describing how it could be done by making some adjustments in CSS. However, I could not find an article which uses simple JavaScript commands to do the same. So, I decided the write this. As you can see now, my footer does have Powered By Blogger credit signature. I am intending on keeping it that way until I am sure that removing it would not get me into any legal issues. I am just writing this article for the educational purposes. Right click on the Powered By Blogger  button and click on Inspect to have Chrome Developer tools loaded with the div that holds the credit content. You can see that the element should have a content similar to the following. Notice that the div is having a class value blogger . We can use this to our advantage. <div class="blogger"> <a href="https://www.blogger.com...