美文网首页
Google Translate

Google Translate

作者: 爱折腾的傻小子 | 来源:发表于2018-12-18 14:21 被阅读23次
  • google translate 谷歌翻译
<a id='google-translate-button' style="position:absolute;top:0;right: 0;color: black;text-decoration:underline;">Translate</a>
    <script type="application/javascript">
        function googleTranslateElementInit() {
            new google.translate.TranslateElement(
                {
                    pageLanguage: '{{ google_translate.to }}',
                    // layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL
                    layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT
                },
                'google_translate_element'
            );
        }

        function triggerHtmlEvent(element, eventName) {
            var event;
            if (document.createEvent) {
                event = document.createEvent('HTMLEvents');
                event.initEvent(eventName, true, true);
                element.dispatchEvent(event);
            } else {
                event = document.createEventObject();
                event.eventType = eventName;
                element.fireEvent('on' + event.eventType, event);
            }
        }
    </script>
    <script src="//translate.google.cn/translate_a/element.js?cb=googleTranslateElementInit"></script>
    <script type="application/javascript">
        $(function () {
            function floatGoogleTranslate() {
                // 未曾翻译过
                if( sessionStorage.getItem('_google_') !== 'yes' && sessionStorage.getItem('_google_') !== 'no-translate' ){
                    var google_index = layer.open({
                        type : 0,
                        title: false,
                        icon: 3,
                        content : "Do you need translation?",
                        btn: ['translate','no translate'],
                        anim: 1,
                        offset: "rt",
                        closeBtn: 0,
                        shadeClose: true,
                        yes: function () {
                            layer.close(google_index);
                            sessionStorage.setItem('_google_','yes');
                            jQuery('.goog-te-combo').val('{{ google_translate.from }}');
                            window.location = window.location.href.replace(/\#googtrans(.){1,}/g,"")+'#googtrans({{ google_translate.to }}|{{ google_translate.from }})';
                            location.reload();
                            return false;
                        },
                        btn2: function () {
                            layer.close(google_index);
                            sessionStorage.setItem('_google_','no-translate');
                            return false;
                        }
                    });
                }
            }
            floatGoogleTranslate();

            // 创建浮出层
            $(document).on('click','#google-translate-button',function (event) {
                // 弹出翻译提示口
                var google_index_t = layer.open({
                    type : 0,
                    title: false,
                    icon: 3,
                    content : "Do you need translation?",
                    btn: ['translate','no translate'],
                    anim: 1,
                    offset: "rt",
                    closeBtn: 0,
                    shadeClose: true,
                    yes: function () {
                        layer.close(google_index_t);
                        sessionStorage.setItem('_google_','yes');
                        jQuery('.goog-te-combo').val('{{ google_translate.from }}');
                        window.location = window.location.href.replace(/\#googtrans(.){1,}/g,"")+'#googtrans({{ google_translate.to }}|{{ google_translate.from }})';
                        location.reload();
                        return false;
                    },
                    btn2: function () {
                        layer.close(google_index_t);
                        if( sessionStorage.getItem('_google_') === 'yes' ){
                            jQuery('.goog-te-combo').val('{{ google_translate.to }}');
                            window.location = window.location.href.replace(/\#googtrans(.){1,}/g,"")+'#googtrans({{ google_translate.from }}|{{ google_translate.to }})';
                            location.reload();
                        }
                        sessionStorage.setItem('_google_','no-translate');
                        return false;
                    }
                });
            });
        });
    </script>
  • 语言判断

<?php
class ControllerEventGoogleTranslate extends Controller {

    /**
     * 判断浏览器语言类型
     * 1. 返回值存在 需要提示翻译成返回值类型语言
     * 2. 返回值为空 当前页面不需要翻译
     */
    public function index()
    {
        // server language
        $to = $this->registry->get('config')->get('language_directory');
        $localLang = strtolower($to);
        if( preg_match('#^([a-z]{1,})-[a-z]{1,}$#i',$localLang,$matches) ){
            $localLang = $matches[1];
        }

        // web language
        $httpLang  = $this->registry->get('request')->server{"HTTP_ACCEPT_LANGUAGE"};

        // 整理浏览器语言
        $str = preg_replace(['~q=0\.(\d){1}(,){0,1}~i','~,~i','~;$~i'],['',';',''],$httpLang);

        // 判断当前php语言是否在浏览器里
        if( !preg_match("#".preg_quote($localLang)."#i",$str) ){
            $t_array = explode(';',$str);
            $temp = array_shift($t_array);
            if( !preg_match("#zh-[a-zA-Z]{1,}#i",$str) ){
                $httpLangArray = explode('-',$temp);
                return [
                    'from'=>$httpLangArray[0],
                    'to'=>$localLang
                ];
            }else if( $temp == 'zh' ){
                $temp = 'zh-CN';
            }

            return [
                'from'=>$temp,
                'to'=>$localLang
            ];
        }

        return '';
    }
}

相关文章

网友评论

      本文标题:Google Translate

      本文链接:https://www.haomeiwen.com/subject/fyflkqtx.html