Google Translate

  • google translate 谷歌翻译
Translate
    
    
    
  • 语言判断

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)