Change google translate dropdown programmatically

On the site, I tried to add a Google Translate drop-down menu using the following code:

function googleTranslateElementInit() { new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element'); } <script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> 

When you select a google script insert from the drop-down list into which the google script inserts are inserted, the Google Translate panel appears at the top of the page and all the text is translated into the selected language.

However, if I try to launch the descent using JavaScript, this will not work:

 $('.goog-te-combo').val('fr') 

French is selected from the drop-down list, but Google Translate does not start.

Why ... why doesn't it work? I also tried:

 $('.goog-te-combo').trigger('click') $('.goog-te-combo').change() 

UPDATE: FYI is not my site. I used the Chrome console to load jQuery and execute jQuery methods.

+9
javascript jquery
source share
6 answers

You may have a drop down page reload trigger. You can either reload the page with #googtrans(en|ja) or #googtrans/en/ja added to the URL, or set the googtrans cookie to /en/ja (where ja is an example of the selected target language) before reloading.

+9
source share

I know this is an old topic, but I just want to share this solution. I came up with a question about firing a google translate element change event.

Add a function that will use the dispatchEvent or fireEvent function:

 function triggerHtmlEvent(element, eventName) { var event; if(document.createEvent) { event = document.createEvent('HTMLEvents'); event.initEvent(eventName, true, true); element.dispatchEvent(event); } else { event = document.createEventObject(); event.eventType = eventName; element.fireEvent('on' + event.eventType, event); } } 

after setting the value, select the DOM object to select (using document.getElement ...) and call the function above:

 triggerHtmlEvent(selectObject, 'change'); 
+4
source share

// to select the selected language

 string language=Request.Cookies["googtrans"].Value 

// set the desired language

  Response.Cookies["googtrans"].Value = Your language; 

// for example: Response.Cookies ["googtrans"]. Value = "/ en / hi";

+4
source share

Add this to your code:

 //to get currently selected language string language=Request.Cookies["googtrans"].Value //to set desired language Response.Cookies["googtrans"].Value = Your language; //eg: Response.Cookies["googtrans"].Value = "/en/hi"; 
+3
source share

When viewing your page, it seems that jQuery is not loaded, so you cannot use the $() function.

You need to add a jQuery link in the <head></head> section, for example:

 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 

Then

 $('.goog-te-combo').val('fr'); 

must work.

+1
source share

After spending quite a bit of time trying to get this to work, I applied the jquery translation plugin and was able to achieve everything that I wanted to do quite easily, including automatic translation into the browser language to load the page and interactive links to the language, flags, etc.

Plugin and download details are here http://code.google.com/p/jquery-translate/

+1
source share

All Articles