Google translate get current language
Asked Answered
I

7

8

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!

Ineluctable answered 20/11, 2012 at 19:44 Comment(1)
it has options for pageLanguage, includedLanguages, and what's the option for the language of the translate button itself?Polyester
I
4

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.

Ineluctable answered 20/11, 2012 at 20:8 Comment(2)
This won't work because lang will depend on the browser's language settings.Injun
That element .goog-te-menu-value span:first seems to no longer existBonspiel
W
5

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.
Wilbur answered 15/12, 2014 at 16:11 Comment(0)
I
4

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.

Ineluctable answered 20/11, 2012 at 20:8 Comment(2)
This won't work because lang will depend on the browser's language settings.Injun
That element .goog-te-menu-value span:first seems to no longer existBonspiel
T
2

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.

Telekinesis answered 22/9, 2013 at 20:43 Comment(5)
Google has since updated the parameters, so google.translate.TranslateElement().c no longer worksBonspiel
google.translate.TranslateElement().e now works for meTelekinesis
google.translate.TranslateElement().e only tells me the initial language, not the current language, once selectedBonspiel
Fair enough. .e was what I needed, but using .a.sd in combination would seem to help most people.Telekinesis
google.translate.TranslateElement().a.cd works for meMontreal
B
2

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.

Bonspiel answered 7/11, 2014 at 14:17 Comment(4)
google.translate.TranslateElement().e now works for meTelekinesis
@timpeterson google.translate.TranslateElement().e only tells me the initial language, not the current language, once selectedBonspiel
Fair enough. .e was what I needed, but using .a.sd in combination would seem to help most people.Telekinesis
I had to use google.translate.TranslateElement().c on my site.Aceydeucy
F
0

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'];
Flite answered 20/11, 2012 at 19:54 Comment(1)
I am receiving undefined on the variable. I don't think translate adds anything to the URL, correct?Ineluctable
U
0

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>
Unpriced answered 22/3, 2013 at 6:40 Comment(0)
K
0

I was stuck in the same problem. After spending a day,i got a hack to read selected language from google translate widget.You can read from html tag as we select,the lang attribute gets updated , You can simply : document.getElementsByTagName('html')[0].lang

Krissykrista answered 14/9, 2022 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.