Google Translate Get Current Language
Solution 1:
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.
Solution 2:
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.
Solution 3:
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.
Solution 4:
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.
Solution 5:
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'];
Post a Comment for "Google Translate Get Current Language"