網站嵌入google翻譯(舊版本)


這是舊版網站嵌入google翻譯的方法,以前設定的翻譯頁面已經近不去一段時間了,不確定還能使用多久,不過目前把程式貼上去還是能使用。

方法

使用時改成對應的語言代碼就可以了

<a href="javascript:void(0)" onclick="doGTranslate('zh-TW|en');">中文翻英文</a>
                    <div id="google_translate_element2" style="display:none"></div>

    <!-- Google translation -->
    <script type="text/javascript">
    function googleTranslateElementInit2() {
        new google.translate.TranslateElement({
            pageLanguage: 'zh-TW',
            autoDisplay: false},
            'google_translate_element2');}
    </script>
	<script async type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2"></script>

    <script type="text/javascript">
    function GTranslateFireEvent(a, b) {
        try {
            if (document.createEvent) {
                var c = document.createEvent("HTMLEvents");
                c.initEvent(b, true, true);
                a.dispatchEvent(c);
    
            } else {
                var c = document.createEventObject();
                a.fireEvent('on' + b, c);
            }
        } catch (e) {}
    }

    function doGTranslate(a) {
        if (a.value) a = a.value;
        if (a == '') return;
        var b = a.split('|')[1];
        var c;
        var d = document.getElementsByTagName('select');
        for (var i = 0; i < d.length; i++)
            if (d[i].className == 'goog-te-combo') c = d[i];
        if (document.getElementById('google_translate_element2') == null || document.getElementById('google_translate_element2').innerHTML.length == 0 || c.length == 0 || c.innerHTML.length == 0) {
            setTimeout(function() {
                doGTranslate(a)
            }, 500)
        } else {
            c.value = b;
            GTranslateFireEvent(c, 'change');
            GTranslateFireEvent(c, 'change')
        }
    }
    </script>
Tags : google翻譯