添加谷歌翻译到你的网站

网站懒得做 i18n?

不如直接谷歌翻译

使用 translate.google.cn 资源,大陆地区也能使用
自动判断浏览器语言
支持 Google 翻译支持的所有语言
隐藏谷歌翻译的样式,更美观

index.html

<div id="google_translate_element"></div>

<script async src="/translate-google.js"></script>

<script type="text/javascript">
    function googleTranslateElementInit(){
        new google.translate.TranslateElement({
            pageLanguage: 'zh-CN',
            includedLanguages: 'af,ga,sq,it,ar,ja,az,kn,eu,ko,bn,la,be,lv,bg,lt,ca,mk,zh-CN,ms,zh-TW,mt,hr,no,cs,fa,da,pl,nl,pt,en,ro,eo,ru,et,sr,tl,sk,fi,sl,fr,es,gl,sw,ka,sv,de,ta,el,te,gu,th,ht,tr,iw,uk,hi,ur,hu,vi,is,cy,id,yi',
            autoDisplay:false
        },'google_translate_element');
    }
</script>

styles.css

#google_translate_element {
    overflow: hidden;
    border-width: 1px;
    border-radius: 0.25rem;
}

.goog-te-gadget {
    font-size: 0 !important;
}

.goog-te-combo {
    margin-top: 0 !important;
    padding-top: 0 !important;
    font-size: 0.75rem !important;
    line-height: 1rem !important;
    outline: 2px solid transparent !important;
    outline-offset: 2px !important;
}
.goog-logo-link {
    display: none !important;
}
.goog-te-banner-frame {
    display: none !important;
    position: fixed !important;
}

translate-google.js

(function () {
    var gtConstEvalStartTime = new Date();
    var h = this || self,
        l = /^[\w+/_-]+[=]{0,2}$/,
        m = null;
    function n(a) {
        return (a = a.querySelector && a.querySelector("script[nonce]")) && (a = a.nonce || a.getAttribute("nonce")) &&
            l.test(a) ? a : ""
    }
    function p(a, b) {
        function c() {}
        c.prototype = b.prototype;
        a.i = b.prototype;
        a.prototype = new c;
        a.prototype.constructor = a;
        a.h = function (g, f, k) {
            for (var e = Array(arguments.length - 2), d = 2; d < arguments.length; d++) e[d - 2] = arguments[d];
            return b.prototype[f].apply(g, e)
        }
    }
    function q(a) {
        return a
    };
    function r(a) {
        if (Error.captureStackTrace) Error.captureStackTrace(this, r);
        else {
            var b = Error().stack;
            b && (this.stack = b)
        }
        a && (this.message = String(a))
    }
    p(r, Error);
    r.prototype.name = "CustomError";
    function u(a, b) {
        a = a.split("%s");
        for (var c = "", g = a.length - 1, f = 0; f < g; f++) c += a[f] + (f < b.length ? b[f] : "%s");
        r.call(this, c + a[g])
    }
    p(u, r);
    u.prototype.name = "AssertionError";
    function v(a, b) {
        throw new u("Failure" + (a ? ": " + a : ""), Array.prototype.slice.call(arguments, 1));
    };
    var w;
    function x(a, b) {
        this.g = b === y ? a : ""
    }
    x.prototype.toString = function () {
        return this.g + ""
    };
    var y = {};
    function z(a) {
        var b = document.getElementsByTagName("head")[0];
        b || (b = document.body.parentNode.appendChild(document.createElement("head")));
        b.appendChild(a)
    }
    function _loadJs(a) {
        var b = document;
        var c = "SCRIPT";
        "application/xhtml+xml" === b.contentType && (c = c.toLowerCase());
        c = b.createElement(c);
        c.type = "text/javascript";
        c.charset = "UTF-8";
        if (void 0 === w) {
            b = null;
            var g = h.trustedTypes;
            if (g && g.createPolicy) {
                try {
                    b = g.createPolicy("goog#html", {
                        createHTML: q,
                        createScript: q,
                        createScriptURL: q
                    })
                } catch (t) {
                    h.console && h.console.error(t.message)
                }
                w = b
            } else w = b
        }
        a = (b = w) ? b.createScriptURL(a) : a;
        a = new x(a, y);
        a: {
            try {
                var f = c && c.ownerDocument,
                    k = f && (f.defaultView || f.parentWindow);
                k = k || h;
                if (k.Element && k.Location) {
                    var e = k;
                    break a
                }
            } catch (t) {}
            e = null
        }
        if (e && "undefined" != typeof e.HTMLScriptElement && (!c || !(c instanceof e.HTMLScriptElement) && (c instanceof e
                .Location || c instanceof e.Element))) {
            e = typeof c;
            if ("object" == e && null != c || "function" == e) try {
                var d = c.constructor.displayName || c.constructor.name || Object.prototype.toString.call(c)
            } catch (t) {
                d = "<object could not be stringified>"
            } else d = void 0 === c ? "undefined" : null === c ? "null" : typeof c;
            v("Argument is not a %s (or a non-Element, non-Location mock); got: %s",
                "HTMLScriptElement", d)
        }
        a instanceof x && a.constructor === x ? d = a.g : (d = typeof a, v(
            "expected object of type TrustedResourceUrl, got '" + a + "' of type " + ("object" != d ? d : a ?
                Array.isArray(a) ? "array" : d : "null")), d = "type_error:TrustedResourceUrl");
        c.src = d;
        (d = c.ownerDocument && c.ownerDocument.defaultView) && d != h ? d = n(d.document) : (null === m && (m = n(
            h.document)), d = m);
        d && c.setAttribute("nonce", d);
        z(c)
    }
    function _loadCss(a) {
        var b = document.createElement("link");
        b.type = "text/css";
        b.rel = "stylesheet";
        b.charset = "UTF-8";
        b.href = a;
        z(b)
    }
    function _isNS(a) {
        a = a.split(".");
        for (var b = window, c = 0; c < a.length; ++c)
            if (!(b = b[a[c]])) return !1;
        return !0
    }
    function _setupNS(a) {
        a = a.split(".");
        for (var b = window, c = 0; c < a.length; ++c) b.hasOwnProperty ? b.hasOwnProperty(a[c]) ? b = b[a[c]] : b =
            b[a[c]] = {} : b = b[a[c]] || (b[a[c]] = {});
        return b
    }
    window.addEventListener && "undefined" == typeof document.readyState && window.addEventListener(
        "DOMContentLoaded",
        function () {
            document.readyState = "complete"
        }, !1);
    if (_isNS('google.translate.Element')) {
        return
    }(function () {
        var c = _setupNS('google.translate._const');
        c._cest = gtConstEvalStartTime;
        gtConstEvalStartTime = undefined;
        c._cl = navigator.language || navigator.userLanguage;
        c._cuc = 'googleTranslateElementInit';
        c._cac = '';
        c._cam = '';
        c._ctkk = '449649.3822363247';
        var h = 'translate.googleapis.com';
        var s = (true ? 'https' : window.location.protocol == 'https:' ? 'https' : 'http') + '://';
        var b = s + h;
        c._pah = h;
        c._pas = s;
        c._pbi = b + '/translate_static/img/te_bk.gif';
        c._pci = b + '/translate_static/img/te_ctrl3.gif';
        c._pli = b + '/translate_static/img/loading.gif';
        c._plla = h + '/translate_a/l';
        c._pmi = b + '/translate_static/img/mini_google.png';
        c._ps = b + '/translate_static/css/translateelement.css';
        c._puh = 'translate.google.cn';
        _loadCss(c._ps);
        _loadJs(b + `/translate_static/js/element/main_${navigator.language || navigator.userLanguage}.js`);
    })();
})();
Typecho赞赏功能

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

薇拉航线🌺

(๑>ڡ<)☆谢谢老板们的投食~

使用微信扫描二维码完成支付


文章二维码

手机阅读

所有原创文章采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站部分内容收集于互联网,如果有侵权内容、不妥之处,请联系我们删除。敬请谅解!


关注微信公众号『丰烨地毯』

第一时间了解最新地毯信息
关注官微不迷路~

添加新评论

  关于博主

一个90后的国际货运专业毕业的学生 搭建网站纯粹是个人兴趣使然 偶尔会写一丢丢PHP和JS的前端代码 目前正在纺织公司学习外贸方面的新人 欢迎各位朋友的交流学习!

  网易评论

思考中……

  近期评论

无论有多困难,都坚强地抬头挺胸,告诉所有人,你并非他们想象的那样不堪一击。

每个人心里都有一段伤痕,时间才是最好的疗剂。

人总是珍惜未得到的,而遗忘了所拥有的。

退一步,并不象征我认输;放手,并不表示我放弃;微笑,并不意味我快乐!

人海中再回首,朋友真诚依旧,生命里重逢,心境平和温柔,往事如风,岁月如歌,漫漫人生路,苍桑几许,幸福几何!