Google翻译小部件-翻译完成回调 [英] Google Translate Widget - Translation complete callback

查看:142
本文介绍了Google翻译小部件-翻译完成回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上使用Google翻译小部件,并提供了以下Google提供的代码:

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.

在运行代码以调整主导航大小之前,我曾尝试调用Google翻译代码,但是翻译是异步运行的,因此我的代码在翻译完成之前就可以运行.

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.

翻译完成时(或某种检测翻译完成时的方式)是否引发了回调事件,所以我可以等一下,然后再尝试调整导航的大小?

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).

如果更改意味着翻译程序已运行,请在翻译后运行您需要运行的代码(此处为initNav()).

If it's changed that means the translator has run, run the code you need to run after the translation (initNav() here).

如果用户选择其他语言,我会一直检查更改.

I keep checking for changes in case the user selects another language.

这并不完美,但对我有用:-)

It's not perfect, but it works for me :-)

这篇关于Google翻译小部件-翻译完成回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆