Google translate get current language

After finding zero of anything to help me online....

I am using the current function for a multi language site:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,es',     layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

However I have no idea how to get the current language once a user changes it. I'm not sure if this is even possible. Basically I want to update to Spanish images if Spanish is chosen over English. Any help would be appreciated!

Answers:

Answer

Your not gonna believe it:

window.setInterval(function(){
     var lang = $(".goog-te-menu-value span:first").text();
     alert(lang);
},5000);

I just had to dig to find the container in Firebug and grab the value from the first span element. Hope this helps someone.

Answer

The currently selected language is stored in a cookie named googtrans.

Here's a simple example of grabbing the value from the cookie (based on cookie code from here: What is the shortest function for reading a cookie by name in JavaScript?):

function readCookie(name) {
    var c = document.cookie.split('; '),
    cookies = {}, i, C;

    for (i = c.length - 1; i >= 0; i--) {
        C = c[i].split('=');
        cookies[C[0]] = C[1];
     }

     return cookies[name];
}
console.log(readCookie('googtrans')); //eg. 'en/no' for Norwegian, '/en/hr' for Croatian, etc.
Answer

Calling google.translate.TranslateElement().c gives the code for the current language. For example, "fr" for french, "en" for english, "de" for german.

Inspecting the google.translate global object is generally informative. It's a little hard b/c Google's obviously compiled the code such that things like TranslateElement.c don't have human readable names, but with a little effort you can make sense of some of the parameters.

Answer

Calling google.translate.TranslateElement().a.sd gives the identifier of the current language.

Examples: "fr" for French, "en" for English, "de" for German, "it" for Italian


Calling google.translate.TranslateElement().a is an object of other current parameters, like the Google Analytics tracking code.

Check it out in the console to see what is there. How do I access the console?

Inspecting the google.translate global object is generally informative. It is a little hard because Google is constantly updating the code, making changes to the parameter allocations, and has obviously compiled the code in such a way that things like google.translate.TranslateElement() does not have human readable names; with a little effort though, you can make sense of some of the parameters.

Answer

You might be able to get the language from the URL.

    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

Then

var selected_lang = getUrlVars()['lang'];
Answer

I am using google translate module in my joomla site. My requirement was that we have one language button at the top. Default the site will be in English. So the button to display on top will be Spanish. When user clicks on the button the page translates to Spanish and the button changes to English and vice versa. This works fine as long you don't refresh the page. Now say the translated page is in Spanish and the button at the top is English. Now when you refresh the page the button changes back to Spanish (javascript hide show function, default is Spanish). As I was enable to trace the current language, the language button at the top were not according to the language selected. After to much digging I found this script which helped me resolve my problem. I hope it helps someone else too.

<script>
 //to get currently selected language
window.setInterval(function(){
     lang = $("select.goog-te-combo option:selected").text();
    alert(lang);
},5000);
 </script>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.