我在网上找的几个方法都不行。方法1index.html 文件的代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage //cut down to first 2 chars of country code
var lang = lang.substr(0,5) // 英语
if (lang == "en")
window.location.replace('en/index.php') // 简体中文
else if (lang == "zh-cn")
window.location.replace('cn/index.php')
// 繁体中文
else if (lang == "zh-hk")
window.location.replace('hk/index.php') // 除上面所列的语言
else
window.location.replace('EN_index.asp')</script>
</head><body>
</body>
</html>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 en/index.php
chrome 语言设置了繁体和简体,都跳转到了 EN_index.asp
方法 2index.php 文件的代码如下:<?php
error_reporting(E_ALL ^ E_NOTICE);
// 分析 HTTP_ACCEPT_LANGUAGE 的属性
// 这里只取第一语言设置 (其他可根据需要增强功能,这里只做简单的方法演示)
preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
$lang = $matches[1];
switch ($lang) {
case 'zh-cn' :
header('Location: http://cn.123456.net/');
break;
case 'zh-hk' :
header('Location: http://hk.123456.net/');
break;
case 'ko' :
header('Location: http://ko.123456.net/');
break;
default:
header('Location: http://en.123456.net/');
break;
}
?>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 http://en.123456.net/
chrome 语言设置了繁体和简体,都跳转到了 http://en.123456.net/
方法 3index.php 文件的代码如下:<?php
$lan = $_SERVER['HTTP_ACCEPT_LANGUAGE']; //获取浏览器语言版本
if (preg_match("/[zh]{2}\-[hk]{2}/", $lan) || preg_match("/[zh]{2}\-[tw]{2}/", $lan)) {//台湾香港转到繁体页面
$url = 'hk/index.php';
header("location: " . $url);
} elseif (preg_match("/(?i)^[en]/", $lan)) { //英文国家转到英文页面
$url = 'en/index.php';
header("location: " . $url);
} elseif (preg_match("/[zh]{2}\-[cn]{2}/", $lan)) { //大陆地区转到简体页面
$url = 'cn/index.php';
header("location: " . $url);
} else { //其他转到英文或其他页面
$url = 'en/index.php';
header("location: " . $url);
}
?>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 http://en.123456.net/
chrome 语言设置了繁体和简体,都跳转到了 http://en.123456.net/各位大哥大姐,帮帮我吧。弄了几天头都晕了
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage //cut down to first 2 chars of country code
var lang = lang.substr(0,5) // 英语
if (lang == "en")
window.location.replace('en/index.php') // 简体中文
else if (lang == "zh-cn")
window.location.replace('cn/index.php')
// 繁体中文
else if (lang == "zh-hk")
window.location.replace('hk/index.php') // 除上面所列的语言
else
window.location.replace('EN_index.asp')</script>
</head><body>
</body>
</html>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 en/index.php
chrome 语言设置了繁体和简体,都跳转到了 EN_index.asp
方法 2index.php 文件的代码如下:<?php
error_reporting(E_ALL ^ E_NOTICE);
// 分析 HTTP_ACCEPT_LANGUAGE 的属性
// 这里只取第一语言设置 (其他可根据需要增强功能,这里只做简单的方法演示)
preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
$lang = $matches[1];
switch ($lang) {
case 'zh-cn' :
header('Location: http://cn.123456.net/');
break;
case 'zh-hk' :
header('Location: http://hk.123456.net/');
break;
case 'ko' :
header('Location: http://ko.123456.net/');
break;
default:
header('Location: http://en.123456.net/');
break;
}
?>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 http://en.123456.net/
chrome 语言设置了繁体和简体,都跳转到了 http://en.123456.net/
方法 3index.php 文件的代码如下:<?php
$lan = $_SERVER['HTTP_ACCEPT_LANGUAGE']; //获取浏览器语言版本
if (preg_match("/[zh]{2}\-[hk]{2}/", $lan) || preg_match("/[zh]{2}\-[tw]{2}/", $lan)) {//台湾香港转到繁体页面
$url = 'hk/index.php';
header("location: " . $url);
} elseif (preg_match("/(?i)^[en]/", $lan)) { //英文国家转到英文页面
$url = 'en/index.php';
header("location: " . $url);
} elseif (preg_match("/[zh]{2}\-[cn]{2}/", $lan)) { //大陆地区转到简体页面
$url = 'cn/index.php';
header("location: " . $url);
} else { //其他转到英文或其他页面
$url = 'en/index.php';
header("location: " . $url);
}
?>
出来的效果是
IE 语言设置了繁体和英文,都跳转到了 http://en.123456.net/
chrome 语言设置了繁体和简体,都跳转到了 http://en.123456.net/各位大哥大姐,帮帮我吧。弄了几天头都晕了
解决方案 »
- 如何执行JQUERY方法?
- 关于禁止从指定URL访问的问题
- 请教高手 在线等
- 一段js,求解释
- 如何使DIV或者table一个一个的显示,并且中间有间隔时间
- javascript里取table的属性值
- 急急急,用function DropRow(){if (event.srcElement.parentElement.tagName.toUpperCase() == 'TR') { alert(event.srcElement.parentEle
- 请看看这样的问题是不是出在字符串方面的?很困惑
- 难解决de问题
- JS代码
- document.getElementById的问题求教大神
- 关于传递参数
<!DOCTYPE html>
<html>
<head>
<script>
var lang=(navigator.language||navigator.userLanguage).slice(-2).toLowerCase();
var langPage={
en:'en/index.php',
cn:'cn/index.php',
hk:'hk/index.php'
}
window.location.replace( langPage[lang]|| 'EN_index.asp' );
</script>
</head>
<body>
</body>
</html>
var lang=(navigator.language||navigator.userLanguage).slice(-2);
var reg= /en|cn|hk|ko/i; //你支持的语言加添加上
window.location.replace( reg.test(lang)?lang+'/index.php':'EN_index.asp');
按这代码复制过去试了一下,还是不行
跳转前弹出,看看当前语言值是什么alert( (navigator.language||navigator.userLanguage))
按这代码复制过去试了一下,还是不行
跳转前弹出,看看当前语言值是什么alert( (navigator.language||navigator.userLanguage))
从那里看语言值?alert( (navigator.language||navigator.userLanguage)) 又是添加在那?
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage //cut down to first 2 chars of country code
var lang = lang.substr(0,5).toLowerCase() alert(lang)// 英语
if (lang == "en")
window.location.replace('en/index.php') // 简体中文
else if (lang == "zh-cn")
window.location.replace('cn/index.php')
// 繁体中文
else if (lang == "zh-hk")
window.location.replace('hk/index.php') // 除上面所列的语言
else
window.location.replace('EN_index.asp')
我试了重启IE没用,那就用chrome,firefox 上测试就行了
你是主要是分 中文繁体/简体 ,其它都英语是吧<!DOCTYPE html>
<html>
<head>
<script>
var lang=(navigator.language||navigator.userLanguage).toLowerCase();alert( lang);
//假设
//英文页面 en/index.php;
//简体页面 en/index.php;
//繁体页面 hk/index.php;var langs=lang.match(/(\w+)-(\w+)/);
lang= langs[1]=='zh'?(langs[2]=='ch'?'cn':'hk'):'en' ;
window.location.replace( lang+ '/index.php' );
</script>
</head>
<body>
</body>
</html>
你也可以直接JS修改lang的值来模拟<script>
var lang=(navigator.language||navigator.userLanguage).toLowerCase();
var lang="zh-tw"; //这里直接你预设的 语言
lang=lang.toLowerCase();
//假设
//英文页面 en/index.php;
//简体页面 en/index.php;
//繁体页面 hk/index.php;
var langs=lang.match(/(\w+)-(\w+)/);
lang= langs[1]=='zh'?(langs[2]=='ch'?'cn':'hk'):'en' ;
window.location.replace( lang+ '/index.php' );
</script>
这里是什么意思?是指我以后要增强其它语言就放这个位置吗?
哦。那例如新增加语言 ar-DZ ar-AE se-FI se-NO 的话应该添加什么代码?
var lang=(navigator.language||navigator.userLanguage).toLowerCase();
var lang="zh-tw"; //这里直接你预设的 语言
lang=lang.toLowerCase();
//假设
//英文页面 en/index.php;
//简体页面 en/index.php;
//繁体页面 hk/index.php;
var langs=lang.match(/(\w+)-(\w+)/);
lang= langs[1]=='zh'?(langs[2]=='ch'?'cn':'hk'):'en':'ar-DZ':'ar-AE':'se-FI':'se-NO' ;
window.location.replace( lang+ '/index.php' );
</script>
是不是这样
<script>
var lang=(navigator.language||navigator.userLanguage).toLowerCase();
var lang="zh-tw"; //这里直接你预设的 语言,应用时记得删除
lang=lang.toLowerCase();
var langPages={
en:'en/index.php',
'en-us':'en/index.php',
'en-us':'en/index.php',
'ar-ag':'ar/index.php',
//往这里添加新增加语言
//....
'zh-cn':'hk/index.php',
'zh-tw':'hk/index.php',
'zh-hk':'hk/index.php'
}
window.location.replace( langPages[lang]||langPages.en );
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var lang=(navigator.language||navigator.userLanguage).toLowerCase();
lang=lang.toLowerCase();
//假设
//英文页面 en/index.php;
//简体页面 cn/index.php;
//繁体页面 hk/index.php;
var langs=lang.match(/(\w+)-(\w+)/);
lang= langs[1]=='zh'?(langs[2]=='ch'?'cn':'hk'):'en' ;
window.location.replace( lang+ '/index.php' );
</script>
</head><body>
</body>
</html>
现在用这个代码,chrome分别设置了繁体 简体 英文测试。繁体和英文都正常跳转,简体就会跳转到繁体页面去了
专业人士就是不一样,我花了几天在网上找的都不能用。