触发 Google 网络翻译元素

发布于 2024-11-14 13:26:28 字数 522 浏览 8 评论 0原文

由于谷歌翻译 API 正在关闭,我试图让谷歌翻译网络元素在用户的整个会话中工作,这样他们就不必在每个不同的页面上将选择框选项更改为其语言。

初始加载函数如下:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, "google_translate_element");
 };

其中 google_translate_element 是要放入选择框的 div。当选择框位于其中时,它始终具有“goog-te-combo”类。我可以使用 jQuery 更改框的值,没有任何问题,例如。 $('.goog-te-combo').val('fr') 会将框更改为法语。 但是,当我尝试使用 $('.goog-te-combo').trigger() 使用各种事件类型(更改、单击、mouseup、mousedown 等)触发翻译时,翻译永远不会触发。

有谁知道触发翻译的方法吗?

Since the google translation api is shutting down, I am trying to get the google translate web element to work across the entire session for a user, so that they do not have to change the select box option to their language on each different page.

The initial load function is given as follows:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, "google_translate_element");
 };

Where google_translate_element is the div into which to put the select box. When the select box is in there it always has the class "goog-te-combo". I can change the value of the box using jQuery with no issue, eg. $('.goog-te-combo').val('fr') will change the box to French.
But when I try to trigger the translation using $('.goog-te-combo').trigger() using all sorts event types (change, click, mouseup, mousedown, etc) the translation never fires.

Does anyone know of a way to trigger the translation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌路终见情 2024-11-21 13:26:28

这是一个老问题,但我会回答,因为我遇到了同样的问题并解决了它。
我必须从 jQuery 获取 DOM,然后触发并执行。

 function fireEvent(element,event){
     console.log("in Fire Event");
    if (document.createEventObject){
            // dispatch for IE
            console.log("in IE FireEvent");
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
            // dispatch for firefox + others
            console.log("In HTML5 dispatchEvent");
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent(event, true, true ); // event type,bubbling,cancelable
            return !element.dispatchEvent(evt);
    }
 }

我称之为使用

 var jObj = $('.goog-te-combo');
 var db = jObj.get(0);
 jObj.val(Lang);
 fireEvent(db, 'change');

This is an old question but I'll answer since i had the same problem and got around it.
I had to get the DOM from jQuery and fire and execute.

 function fireEvent(element,event){
     console.log("in Fire Event");
    if (document.createEventObject){
            // dispatch for IE
            console.log("in IE FireEvent");
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
            // dispatch for firefox + others
            console.log("In HTML5 dispatchEvent");
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent(event, true, true ); // event type,bubbling,cancelable
            return !element.dispatchEvent(evt);
    }
 }

And I call it using

 var jObj = $('.goog-te-combo');
 var db = jObj.get(0);
 jObj.val(Lang);
 fireEvent(db, 'change');
毅然前行 2024-11-21 13:26:28

我一整天也对这个主题有疑问:)。但我已经得出了结论。

使用 Chrome 检查器工具,我发现 Google 翻译选择框是在 window.load 事件触发后创建的!如果您尝试使用 jQuery 编辑它,这是一个大问题。

所以我找到了一个解决方法。我知道这可能是一个错误的方法,但它对我有用。就我而言,我想(与工作相关的请求)将选择框的第一个选项(通常是“选择您的语言”)更改为不同的内容,例如。 123.

所以我实施了以下解决方案:

$(window).load(function () {
   setTimeout(function () {
      if ($("select.goog-te-combo option:first").val()=='')
      {
         $("select.goog-te-combo option:first").text("123");
      }
   }, 1000);
});

希望我能以某种方式帮助某人。

向大家问好。

I have been having problems with this subject all day too :). But I have came to a conclusion.

Using the Chrome inspector tool I found that the Google translate select box is created AFTER the window.load event triggers! This is a big problem if you are trying to edit it using jQuery.

So I found a workaround. I know that's probably a wrong approach, but it is working for me. In my case I wanted (job related request) to change the first option of the select box, normally "Select you Language" to something different, ex. 123.

So I have implemented the following solution:

$(window).load(function () {
   setTimeout(function () {
      if ($("select.goog-te-combo option:first").val()=='')
      {
         $("select.goog-te-combo option:first").text("123");
      }
   }, 1000);
});

Hoping i have helped someone somehow.

Regards to all.

疏忽 2024-11-21 13:26:28

Google Translate Element 语言应该在页面之间运行,尽管有时需要一些时间才能生效。早期,它不会在所有浏览器中的 Google Chrome 上持续存在,但它在 Firefox 和 IE 中运行良好。在写这篇文章时,它在 Chrome 中对我来说也运行得很好。

不过,如果您想在首页加载时强制翻译为特定语言,则可以使用 Google 在页面加载时提及 URL 哈希/片段。不过,由于在页面加载后添加哈希似乎不会触发它,因此您必须将其放在第一次加载时将用户带到那里的链接中。这是强制立即翻译的哈希语法。

http://www.somedomain.com/#googtrans(en|TARGET_LANG)

例如,强制页面在翻译系统可以的情况下立即翻译成法语。

http://www.somedomain.com/#googtrans(en|fr)

我似乎无法解读语法的含义,但我只是在原始英文页面上尝试它。 #googtrans(en|fr|ja) 结果为日语,但 #googtrans(en|ja|fr) 也是如此。省略 en| 似乎也没有任何效果。他们没有详细说明 Google 发布此花絮的具体情况(在“内容”下)是 Google 翻译 Web 元素吗?”)。

The Google Translate Element language should carry between pages, though it can take a bit to kick in sometimes. In the earlier days, it wouldn't persist on Google Chrome, of all browsers, but it worked fine in Firefox and IE. As of writing this, it worked fine for me in Chrome, too.

If you want to force the translation to a particular language on the first page load, though, you can use a Google-mentioned URL hash/fragment on page load. Since adding the hash after the page loads doesn't seem to trigger it, though, you will have to have it in a link that takes the user there the first load. Here is the hash syntax for forcing an immediate translation.

http://www.somedomain.com/#googtrans(en|TARGET_LANG)

For example, to force the page to translate into French as soon as the translate system can.

http://www.somedomain.com/#googtrans(en|fr)

I can't seem to decipher the meaning of the syntax, but I am only trying it on an originally-English page. #googtrans(en|fr|ja) results in Japanese but so does #googtrans(en|ja|fr). Omitting the en| didn't seem to have any effect either. They don't go into detail on the specifics where Google posted this tidbit (under "What is the Google Translate Web Element?").

怀里藏娇 2024-11-21 13:26:28

将 cookie googtrans 设置为 /en/XX 并重新加载页面:(

document.cookie = 'googtrans=/en/fr';
location.reload();

当然,在重新加载之前检查是否未设置 cookie,否则会出现无限重新加载循环)

set the cookie googtrans to /en/XX and reload the page:

document.cookie = 'googtrans=/en/fr';
location.reload();

(of course, check if the cookie is not set before reloading, otherwise you'll get an infinite reload loop)

清君侧 2024-11-21 13:26:28

以前没有这样做过,但通过查看 Google Chrome 中的检查器,我发现 google 加载了一个名为 main.js 的文件。查看该文件,您可能会得到他们如何触发开关的线索。 main.js 当然是打包和缩小的,所以尝试使用这个 JS Beautifier

我会多看一下并回复您:)希望它有帮助!

更新

无法像这样触发元素的原因似乎是因为选择框位于 iframe 内。您无法与不同域的 iframe 进行交互。

Haven't done this before but by looking in the inspector in Google Chrome, I'm seeing that google loads a file called main.js. Look in that file and you will probably get a clue how they trigger the switch. main.js is of course packed and minified so try using this one JS Beautifier.

I will tho look at it more and get back to you :) Hope it helps!

UPDATE

The reason why you can't trigger the element like that seems to be because the select box is inside an iframe. You can't interact with an iframe of a different domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文