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!
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.
By default, the Assistant automatically detects the language being spoken and translates it into your preferred language.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With