修改 google.translate.TranslateElement 结果中的元素

发布于 2025-01-06 06:17:28 字数 1516 浏览 4 评论 0原文

我正在尝试将非常方便的 Google Translate 翻译元素嵌入到网页中,这非常简单并且效果很好,但我需要更改结果 HTML 中显示的默认文本:

在此处输入图像描述

在使用过许多 Google API 和 js 库后,我认为这不会有问题,因为它几乎肯定是可配置的,但在四处寻找一些时间我找不到任何引用一个让你设置它的属性,而一般的文档似乎很可怜。基本代码是:

<div id="google_translate_element"></div>
<script>
   function googleTranslateElementInit() {
      var translator = new google.translate.TranslateElement({
      pageLanguage: 'en',
      autoDisplay: false,
      multilanguagePage: false,
      layout: google.translate.TranslateElement.InlineLayout.SIMPLE
   }, 'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

当我创建 translator 时,我对能够将其设置为属性感到失望,因此决定破解它并使用 onDOMNodeInserted 侦听器将生成的 HTML 加载到

后对其进行更改。我在这里使用 jQuery,所以我的代码是:

$(document).ready(function(){
   $('#google_translate_element').bind('DOMNodeInserted', function(event) {
       $('.goog-te-menu-value span:first').html('Translate');
   });
})

这就是事情变得有趣的地方。 Chrome 完美地加载了所有内容,并完美地完成了 innerHTML 替换。 Internet Explorer (8) 完全忽略 DOMNodeInserted 侦听器,并且页面加载就像从未调用过 jQuery 函数一样。 Firefox (10) 似乎加载良好(但根本没有翻译元素),然后冻结,开始吞噬内存,然后崩溃。

关于如何让这个innerHTML替换工作有什么想法吗?如果您知道 displayLabel : "Translate" 之类的属性当然是首选,但除非它(以及一个非常丑陋的 setTimeout hack),有什么办法我能让这个工作吗?

I'm attempting to embed the very convenient Google Translate translate element into a webpage, which is super simple and works great, but I need to change the default text that displays in the resulting HTML:

enter image description here

Having worked with a number of Google APIsand js libraries, I figured this would be no problem as it would almost certainly be configurable, but having looked around for some time I can't find any reference to a property that let's you set this, and documentation in general seems to be pitiful. The basic code is:

<div id="google_translate_element"></div>
<script>
   function googleTranslateElementInit() {
      var translator = new google.translate.TranslateElement({
      pageLanguage: 'en',
      autoDisplay: false,
      multilanguagePage: false,
      layout: google.translate.TranslateElement.InlineLayout.SIMPLE
   }, 'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

Having dispaired of being able to set this as a property in the when I create the translator, I decided to hack it and use an onDOMNodeInserted listener to just change the resulting HTML once it had loaded into <div id="google_translate_element"></div>. I'm using jQuery here, so my code is:

$(document).ready(function(){
   $('#google_translate_element').bind('DOMNodeInserted', function(event) {
       $('.goog-te-menu-value span:first').html('Translate');
   });
})

And here's where things get interesting. Chrome loads everything perfectly and does the innerHTML substitution beautifully. Internet Explorer (8) ignores the DOMNodeInserted listener altogether and the page loads as if the jQuery function was never called. Firefox (10) appears to load fine (but with no translate element at all) and then freezes, begins gobbling up memory, and crashes.

Any thoughts on how I can get this innerHTML substitution to work? If you're aware of a displayLabel : "Translate"-like property that is of course preferred, but barring that (and a really ugly setTimeout hack) is there any way I can get this to work?

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

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

发布评论

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

评论(3

冬天旳寂寞 2025-01-13 06:17:28

您可以使用 CSS 来完成此操作,只是选择语言时它不会更改标签。

#google_translate_element .goog-te-gadget-simple .goog-te-menu-value span:first-child{display: none;}
#google_translate_element .goog-te-gadget-simple .goog-te-menu-value:before{content: 'Translate'}

You can do it using CSS, only it will not change the label when a language is selected.

#google_translate_element .goog-te-gadget-simple .goog-te-menu-value span:first-child{display: none;}
#google_translate_element .goog-te-gadget-simple .goog-te-menu-value:before{content: 'Translate'}

2025-01-13 06:17:28

和您一样,我不知道如何通过 init params 自定义小工具,但似乎可以用 HTML 编写您自己的自定义小工具,然后在其上调用 g.translate 功能。请参阅 http://www.toronto.ca/(页脚)。恐怕你需要做更多的挖掘才能确切地了解它是如何完成的。

g.translate 的这种用法也被引用这里。不幸的是,这个讨论现在已经很老了,但希望仍然有意义。

Like you I can't find out how to customize the gadget via init params but it appears possible to write your own customized gadget in HTML then invoke g.translate functionality on it. See http://www.toronto.ca/ (page footer). I'm afraid you will have to do some more digging to see exactly how it's done.

This use of g.translate is also referenced here. Unfortunately the discussion is quite old now but hopefully still relevant.

如梦 2025-01-13 06:17:28

我正在使用这段代码,每 3 毫秒检查一次翻译模块是否已加载到页面中;如果是这样,它就会更新文本并清除之后的间隔检查。

function cleartimer() {     
    setTimeout(function(){ 
        window.clearInterval(myVar); 
    }, 1000);             
}
function myTimer() {
    if ($('.goog-te-menu-value > span:first-child').length) {
        $('.goog-te-menu-value > span:first-child').html('Translate');
        cleartimer();
    }
}
var myVar = setInterval(function(){ myTimer() }, 300); 

I'm using this code which checks every 3ms if the translate module has been yet loaded into the page; if so, it then updates the text and clears the interval check after.

function cleartimer() {     
    setTimeout(function(){ 
        window.clearInterval(myVar); 
    }, 1000);             
}
function myTimer() {
    if ($('.goog-te-menu-value > span:first-child').length) {
        $('.goog-te-menu-value > span:first-child').html('Translate');
        cleartimer();
    }
}
var myVar = setInterval(function(){ myTimer() }, 300); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文