PHP网站做简体繁体英文切换功能-多语言版本
翻译后效果图:
现在我们介绍一下中文简体转繁体的
这里用到一个js插件
lang.js.rar
在通用控制器HomeController.class.php中
<?php
function __construct() {
parent::__construct();
$zh_web_lang = array();
$lang = cookie('zh_web_lang');
//谁放在前面,谁为默认的显示。
if ($lang == "jianti") {
$zh_web_lang['hreflang'] = "s";
$zh_web_lang['atuolang'] = "t";
$zh_web_lang['lang'] = "fanti";
$zh_web_lang['html'] = "简体中文";
} else {
$zh_web_lang['hreflang'] = "t";
$zh_web_lang['atuolang'] = "s";
$zh_web_lang['lang'] = "jianti";
$zh_web_lang['html'] = "繁體中文";
}
$this->assign('zh_web_lang', $zh_web_lang); //输出默认语言
}
IndexController.class.php中用于切换cookie
<?php
public function trunlang() {
if (IS_POST) {
$lang = cookie('zh_web_lang');
if ($lang == "jianti") {
cookie('zh_web_lang', 'fanti');
} else {
cookie('zh_web_lang', 'jianti');
}
} else {
$this->redirect('Index/index');
}
}
在页面的切换点中,html的头部文件
<a href="javascript:zh_lang('{$zh_web_lang.hreflang}');" id="zh_lang"
data-lang="{$zh_web_lang.lang}">{$zh_web_lang.html}
</a>
<script type="text/javascript" src="/Public/js/lang.js?v=2"></script>
<script type="text/javascript">
// 简体繁體中文切换
function zh_lang(go) {
$.ajax({ url: "{:U('Index/trunlang')}",
type: "POST",
cache: false,
success: function () {
var tran_a = $('#zh_lang');
if (tran_a.attr("data-lang") == "fanti") {
// 转换成简体
tran_a.attr("href", "javascript:zh_lang('t');");
tran_a.attr("data-lang", 'jianti');
tran_a.html('繁體中文');
} else {
// 转换成繁體
tran_a.attr("href", "javascript:zh_lang('s');");
tran_a.attr("data-lang", 'fanti');
tran_a.html('简体中文');
}
zh_tran(go);
}
});
}
window.onload = $(function () {
zh_tran("{$zh_web_lang.atuolang}");
});
</script>
如果简体和繁体的切换标识需要分开的,可以把上面的a标签修改成下面的即可
<li class="{:$zh_web_lang['atuolang']=='s'?'on':''}">
<a
<if condition="$zh_web_lang['atuolang'] eq 't'">href="javascript:zh_lang('{$zh_web_lang.hreflang}','j');"</if>
class="zh_lang" data-lang="{$zh_web_lang.lang}">{$Think.lang.Zh-cn}</a>
</li>
<li class="{:$zh_web_lang['atuolang']=='t'?'on':''}">
<a
<if condition="$zh_web_lang['atuolang'] eq 's'">href="javascript:zh_lang('{$zh_web_lang.hreflang}','f');"</if>
class="zh_lang" data-lang="{$zh_web_lang.lang}">{$Think.lang.Tw-cn}</a>
</li>
第二种是谷歌翻译:
<select onchange="doGTranslate(this);"><option value="">Select Language</option><option value="en|af">Afrikaans</option><option value="en|sq">Albanian</option><option value="en|ar">Arabic</option><option value="en|hy">Armenian</option><option value="en|az">Azerbaijani</option><option value="en|eu">Basque</option><option value="en|be">Belarusian</option><option value="en|bg">Bulgarian</option><option value="en|ca">Catalan</option><option value="en|zh-CN">Chinese (Simplified)</option><option value="en|zh-TW">Chinese (Traditional)</option><option value="en|hr">Croatian</option><option value="en|cs">Czech</option><option value="en|da">Danish</option><option value="en|nl">Dutch</option><option value="en|en">English</option><option value="en|et">Estonian</option><option value="en|tl">Filipino</option><option value="en|fi">Finnish</option><option value="en|fr">French</option><option value="en|gl">Galician</option><option value="en|ka">Georgian</option><option value="en|de">German</option><option value="en|el">Greek</option><option value="en|ht">Haitian Creole</option><option value="en|iw">Hebrew</option><option value="en|hi">Hindi</option><option value="en|hu">Hungarian</option><option value="en|is">Icelandic</option><option value="en|id">Indonesian</option><option value="en|ga">Irish</option><option value="en|it">Italian</option><option value="en|ja">Japanese</option><option value="en|ko">Korean</option><option value="en|lv">Latvian</option><option value="en|lt">Lithuanian</option><option value="en|mk">Macedonian</option><option value="en|ms">Malay</option><option value="en|mt">Maltese</option><option value="en|no">Norwegian</option><option value="en|fa">Persian</option><option value="en|pl">Polish</option><option value="en|pt">Portuguese</option><option value="en|ro">Romanian</option><option value="en|ru">Russian</option><option value="en|sr">Serbian</option><option value="en|sk">Slovak</option><option value="en|sl">Slovenian</option><option value="en|es">Spanish</option><option value="en|sw">Swahili</option><option value="en|sv">Swedish</option><option value="en|th">Thai</option><option value="en|tr">Turkish</option><option value="en|uk">Ukrainian</option><option value="en|ur">Urdu</option><option value="en|vi">Vietnamese</option><option value="en|cy">Welsh</option><option value="en|yi">Yiddish</option></select><div id="google_translate_element2"></div>
<script type="text/javascript">
function googleTranslateElementInit2() {new google.translate.TranslateElement({pageLanguage: 'en',autoDisplay: false}, 'google_translate_element2');}
</script>
</script><script type="text/javascript" src="https://translate.google.cn/translate_a/element.js?&cb=googleTranslateElementInit2"></script>
<script type="text/javascript">
/* <![CDATA[ */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('6 7(a,b){n{4(2.9){3 c=2.9("o");c.p(b,f,f);a.q(c)}g{3 c=2.r();a.s(\'t\'+b,c)}}u(e){}}6 h(a){4(a.8)a=a.8;4(a==\'\')v;3 b=a.w(\'|\')[1];3 c;3 d=2.x(\'y\');z(3 i=0;i<d.5;i++)4(d[i].A==\'B-C-D\')c=d[i];4(2.j(\'k\')==E||2.j(\'k\').l.5==0||c.5==0||c.l.5==0){F(6(){h(a)},G)}g{c.8=b;7(c,\'m\');7(c,\'m\')}}',43,43,'||document|var|if|length|function|GTranslateFireEvent|value|createEvent||||||true|else|doGTranslate||getElementById|google_translate_element2|innerHTML|change|try|HTMLEvents|initEvent|dispatchEvent|createEventObject|fireEvent|on|catch|return|split|getElementsByTagName|select|for|className|goog|te|combo|null|setTimeout|500'.split('|'),0,{}))
function delCookie(name) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() - Days * 24 * 60 * 60 * 30);
//这里一定要注意,如果直接访问ip的话,不用注明域名domain
//但访问的是域名例如www.baidu.com时,翻译插件的cookie同时存在于一级和二级域名中
//即删除翻译cookie时要把domain=www.baidu.com和domain=.baidu.com两个cookie一起删除才行
var domain = document.domain;
var domainIsIp = false;
var dd = domain.split(".");
if(dd.length==4){
domainIsIp=true;
}
document.cookie = name + "='';path=/;expires="+ exp.toUTCString();
if(domainIsIp==false){
domain="."+dd[1]+"."+dd[2];
document.cookie = name + "='';domain="+domain+";expires="+exp.toGMTString()+";path=/";
}
上面是谷歌翻译说支持的一些语言,可以用来外贸网站语言种类还是很多的。