我需要一个像
这样的脚本if ($userLanguage === 'english') {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}
上面的脚本用作示例。我看了遍布谷歌,所有它给了我的地理定位脚本等。我不想在我的脚本中使用第三方网址。你永远不知道他们的服务是否会下降。
我在哪里可以找到这样的东西?
答案 0 :(得分:3)
我不打算在此重复所有有效的答案,但是this link显示了另一种利用http_negotiate_language() method
{{3}的好方法(对于多语言网站) }
因此,结合该映射和令人兴奋的代码,您将拥有:
$map = array("en" => "english", "es" => "spanish");
$userLanguage = $map[ http_negotiate_language(array_keys($map)) ];
if ($userLanguage === 'english') {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}
但如果您只想检测英语( en )语言,则可能需要将脚本缩短为:
if ('en' == substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}
答案 1 :(得分:0)
我会做这样的事情:
<?php
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($language){
case "sv":
include("index_swedish.php");
break;
case "nl":
include("index_dutch.php");
break;
default:
include("index_english.php"); // or do what ever you want
break;
}
?>
的列表
答案 2 :(得分:0)
是的,这个想法很好,也尝试一下,
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
header("location: ".$lang."/index.php");
答案 3 :(得分:0)
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($lang == 'en') {
echo "we have detected you are English. would you like to visit our site in English?";
}
else {
header("location: /index.php?lang=default");
}