Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 841
Naterade Avatar asked Nov 20 '12 19:11

Naterade


People also ask

How do I change the default language in Google Translate?

There is no direct way to set the default language. Google Translate uses a cookie called "googtrans" to track which language is selected. You can set that cookie yourself before the page loads and Google Translate will use it.

Can Google Translate detect spoken language?

By default, the Assistant automatically detects the language being spoken and translates it into your preferred language.

Can you translate my current screen?

In apps and other screens, just hold down the Home button and tap the "Translate this screen" card, and the text gets translated. The translation feature is available for phones with the language set to English, French, Italian, German, Spanish, Portuguese and Russian, Google said.

Is there a live Google Translate?

Google Live Translate will work on any Android-powered phone, but only the Google Pixel 6 makes it possible to translate live in over half a dozen languages without an internet connection, all thanks to the Google Tensor processor inside.


3 Answers

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.

like image 134
Naterade Avatar answered Oct 25 '22 00:10

Naterade


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.
like image 28
jchip Avatar answered Oct 24 '22 23:10

jchip


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.

like image 37
tim peterson Avatar answered Oct 25 '22 00:10

tim peterson