简体   繁体   English

Google Translate Widget - 翻译完整回调

[英]Google Translate Widget - Translation complete callback

I'm using the google translate widget on one of my sites with the following google supplied code: 我使用以下谷歌提供的代码在我的一个网站上使用谷歌翻译小部件:

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

<script>

My problem: The translate runs after the page has loaded but I also have a script that auto sizes my primary navigation elements based on their width. 我的问题:翻译在页面加载后运行,但我也有一个脚本,根据宽度自动调整主要导航元素的大小。

This runs before the translate has finished so it resizes based on untranslated English labels. 这在翻译完成之前运行,因此它会根据未翻译的英文标签进行调整。 Once the translate has changed the navigation wording, the navigation elements need to be resized to fit the newly translated words as they are likely to be different size (width) to the English. 一旦翻译改变了导航措辞,导航元素需要调整大小以适应新翻译的单词,因为它们可能与英语的大小(宽度)不同。

I've tried calling the Google translate code before I run the code to resize the primary navigation but the translate runs asynchronously so my code runs before the translate is complete. 在运行代码以调整主导航大小之前,我已尝试调用Google翻译代码,但翻译以异步方式运行,因此我的代码在翻译完成之前运行。

Is there a callback event raised when the translation is complete (or some way to detect when the translation is complete), so I can wait before I attempt to resize the navigation? 翻译完成时是否会引发回调事件(或某种方式来检测翻译何时完成),所以我可以在尝试调整导航大小之前等待?

Also, I need to run a script AFTER the page has finished translating. 此外,我需要在页面完成翻译后运行脚本。

Here's the fix I ended up using: 这是我最终使用的修复:

jQuery(function(){
    firstMenu = $("#nav li:first").text();

    setTimeout(waitNav, 500);
});

function waitNav() {

    if (firstMenu != $('#nav li:first').text()) {

        initNav();
        firstMenu = $('#nav li:first').text();
        setTimeout(waitNav, 500);

    }
    else {
        setTimeout(waitNav, 500);
    }

}

Basically, keep checking if a known piece of text has changed (in this case it's the first item in the navigation block). 基本上,继续检查已知文本是否已更改(在这种情况下,它是导航块中的第一项)。

If it's changed that means the translator has run, run the code you need to run after the translation (initNav() here). 如果它已经改变意味着翻译器已经运行,那么运行翻译后需要运行的代码(这里是initNav())。

I keep checking for changes in case the user selects another language. 在用户选择其他语言的情况下,我会不断检查更改。

It's not perfect, but it works for me :-) 它并不完美,但它对我有用:-)

$( document ).ready(function() {
    $('#google_translate_element').bind('DOMSubtreeModified', function() {
        var val = $(this);
        var strlang = "" + val[0].innerText + "";
        console.log(strlang); // print your selected language in console
    });
});

You can detect changes like this: 你可以检测到这样的变化:

$('#some-translatable-element').bind('DOMSubtreeModified', function() {
  yourCallback();
});

The drawback is that the event is beeing fired multiple times since google translate does multiple changes in the process. 缺点是该事件多次被激活,因为谷歌翻译在此过程中进行了多次更改。

A suggestion is to detect changes on the last element on your page, cause then you know all elements above is translated. 建议是检测页面上最后一个元素的更改,因为您知道上面的所有元素都已翻译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM