Google Translate Widget - Translation complete callback

后端 未结 3 1130
梦谈多话
梦谈多话 2021-01-17 18:26

I\'m using the google translate widget on one of my sites with the following google supplied code:

<
3条回答
  •  梦谈多话
    2021-01-17 18:37

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

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

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

提交回复
热议问题