自定义Google Page Translate

时间:2017-11-22 19:39:18

标签: javascript jquery google-translate

在这篇文章中尝试了几乎所有的脚本 Implementing Google Translate with custom flag icons

但他们都没有为我工作。 这个适用于在语言之间切换,但无法将Google翻译重置为默认值。点击" EN"然后回到" DE"。点击" EN"再次没有翻译页面。

我缺少什么?



var _gto;

    // will be called after Google Translate SDK has been loaded successfully
    function googleTranslateElementInit() {
        _gto = new google.translate.TranslateElement({
            defaultLanguage: 'de',
            pageLanguage: 'de',
            includedLanguages: 'de,en',
            layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
            autoDisplay: false
        }, 'google_translate_element');
    }

    $('.lang-item').on('click', function (e) {
        e.preventDefault();

        var $menuframe = $('.goog-te-menu-frame:first'),
            // Google iframe that will be invoked, after user has selected an language
            $bannerframe,
            // button reference to reset language to default
            $bannerframerestore,
            langcode = $(this).data('lang');

        // therefore it is neccassary to catch the element delayed
        setTimeout(function () {
            $bannerframe = $('.goog-te-banner-frame');
            $bannerframerestore = $bannerframe.contents().find('.goog-close-link');

            // restore to default
            if ($(e.currentTarget).hasClass('default')) {
                $bannerframerestore[0].click();
            }
        }, 500);

        $menuframe.contents().find('a span.text:contains("' + _gto.K[langcode] + '")').click();
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="google_translate_element"></div>
<ul class="lang-list">
    <li><a href="#" class="lang-item default" data-lang="de">DE</a></li>
    <li><a href="#" class="lang-item" data-lang="en">EN</a></li>
</ul>

<h1>Verkauf</h1>
<h2>SALE</h2>

<script src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

解决这个问题:

必须删除“includedLanguages”中的“defaultLanguage:'de'”和“de”。 现在它的工作原理应该如此。