javascript 以编程方式更改 Google 翻译下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5569927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Google Translate dropdown programmatically
提问by Jamie Carruthers
On a site I tried adding the Google Translate dropdown using the following code:
在一个网站上,我尝试使用以下代码添加 Google 翻译下拉列表:
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 from the dropdown that the google script inserts, a Google Translate bar appears at the top of the page, and all text is translated in to the selected language.
当您从 google 脚本插入的下拉列表中进行选择时,页面顶部会出现一个 Google 翻译栏,并且所有文本都会被翻译成所选语言。
However if I try and trigger the dropdown change using JavaScript, it doesn't work:
但是,如果我尝试使用 JavaScript 触发下拉更改,则它不起作用:
$('.goog-te-combo').val('fr')
'French' is selected from the dropdown, however Google Translate is not triggered.
从下拉列表中选择了“法语”,但不会触发 Google 翻译。
Why o why does it not work? I've also tried:
为什么 o 为什么它不起作用?我也试过:
$('.goog-te-combo').trigger('click')
$('.goog-te-combo').change()
UPDATE: FYI this is not my site. I was using the Chrome console to load jQuery and execute the jQuery methods.
更新:仅供参考,这不是我的网站。我使用 Chrome 控制台加载 jQuery 并执行 jQuery 方法。
回答by superann
You can have your dropdown trigger a page reload. You can either reload the page with #googtrans(en|ja)
or #googtrans/en/ja
appended to the URL, or set the googtrans cookie value to /en/ja
(where ja is an example of the selected target language) before reloading.
您可以让下拉菜单触发页面重新加载。您可以使用URL#googtrans(en|ja)
或#googtrans/en/ja
附加到 URL重新加载页面,或者/en/ja
在重新加载之前将 googtrans cookie 值设置为(其中 ja 是所选目标语言的示例)。
回答by joeyend
I know this is already an old topic, but I just wanna share the solution I came up with the issue on firing the google translate select element change event.
我知道这已经是一个老话题了,但我只想分享我提出的关于触发谷歌翻译选择元素更改事件的问题的解决方案。
Add function that will use the dispatchEvent or fireEvent function:
添加将使用 dispatchEvent 或 fireEvent 函数的函数:
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, get the DOM object for select (using document.getElement...) and call the function above:
设置值后,获取选择的DOM对象(使用document.getElement...)并调用上面的函数:
triggerHtmlEvent(selectObject, 'change');
回答by Robin Joseph
//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";
//例如:Response.Cookies["googtrans"].Value = "/en/hi";
回答by Robin
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";
回答by ggutenberg
In looking at your page it appears that jQuery isn't loaded, so you won't be able to use the $()
function.
在查看您的页面时,似乎未加载 jQuery,因此您将无法使用该$()
功能。
You need to add a reference to jQuery in your <head></head>
section, such as:
您需要在您的<head></head>
部分中添加对 jQuery 的引用,例如:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
Then
然后
$('.goog-te-combo').val('fr');
should work.
应该管用。
回答by paj
Having spent quite a lot of time trying to get this to work too, I implemented the jquery translation plugin and was able to achieve everything I wanted to do quite easily including automatic translate to browser language on page load and clickable language links, flags etc.
我花了很多时间试图让它工作,我实现了 jquery 翻译插件,并且能够很容易地实现我想做的一切,包括在页面加载时自动翻译成浏览器语言和可点击的语言链接、标志等。
Details of the plugin and downloads are here http://code.google.com/p/jquery-translate/
插件和下载的详细信息在这里http://code.google.com/p/jquery-translate/
回答by Joaquim Harmes Figueiredo
The correct way to use I wrote here. The google translate set the change function using the select 'select.goog-te-combo', if u use only the class, the JS don't call the correct listener. @joeyend way work, because the function identify the real 'selector'.
正确的使用方法我写在这里。谷歌翻译使用 select 'select.goog-te-combo' 设置更改功能,如果您只使用该类,则 JS 不会调用正确的侦听器。@joeyend 方式工作,因为该功能识别真正的“选择器”。
var trans = jQuery('select.goog-te-combo');
trans.val('fr')
trans.change();