Google Translate Widget - Translation complete callback

后端 未结 3 1093
梦谈多话
梦谈多话 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:32
    $( 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
        });
    });
    
    0 讨论(0)
  • 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 :-)

    0 讨论(0)
  • 2021-01-17 18:55

    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.

    0 讨论(0)
提交回复
热议问题