php 自动检测语言并重定向用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17803702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto detect language and redirect user
提问by viriato
I am doing my own website and I managed to write some code that makes directs user to the language version according to the browser's language. Here is the script:
我正在做我自己的网站,我设法编写了一些代码,根据浏览器的语言将用户定向到语言版本。这是脚本:
<?php
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "sv")
header("location: index.php");
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "pt")
header("location: pt/index.php");
else
header("location: en/index.html");
?>
I have put this in the index.php before the . It seems to be working because I am not in an English speaking country but my browser is in English and I am being redirected to the English version.
我已经把它放在 index.php 之前的 . 它似乎有效,因为我不在英语国家,但我的浏览器是英文的,我被重定向到英文版本。
Is this correct? Is there a better/cleaner way to do this?
这样对吗?有没有更好/更清洁的方法来做到这一点?
采纳答案by viriato
Well, I came across some problems with my code which is no surprise due to I am not a PHP expert. I kept therefore on searching for a possible solution and I found the following code on another website:
好吧,我的代码遇到了一些问题,这并不奇怪,因为我不是 PHP 专家。因此,我一直在寻找可能的解决方案,并在另一个网站上找到了以下代码:
<?php
// Initialize the language code variable
$lc = "";
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
$lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Now we simply evaluate that variable to detect specific languages
if($lc == "fr"){
header("location: index_french.php");
exit();
} else if($lc == "de"){
header("location: index_german.php");
exit();
}
else{ // don't forget the default case if $lc is empty
header("location: index_english.php");
exit();
}
?>
This did the job perfectly! I only had a problem left. There was no way to change language, even with direct links into another language because as soon as the page was loading, the php block would redirect me to the borwser's language. This can be a problem if you are living in another country and have for instance Swedish as a mother language but you have your browser in English because you bought your computer in the UK.
这完美地完成了工作!我只剩下一个问题。没有办法改变语言,即使直接链接到另一种语言,因为一旦页面加载,php 块就会将我重定向到浏览器的语言。如果您住在另一个国家/地区并且以瑞典语为母语,但您的浏览器是英文的,因为您的计算机是在英国购买的,这可能是一个问题。
So my solution for this issue was to create folders with a duplicate version for every language (even the one for the main language) without this php code on the index.html (and therefore not index.php). So now my website is auto detecting the language and the user also has the option to change it manually in case of they want!
因此,我针对此问题的解决方案是为每种语言(甚至是主要语言的语言)创建具有重复版本的文件夹,而在 index.html(因此不是 index.php)上没有此 php 代码。所以现在我的网站正在自动检测语言,用户还可以选择手动更改语言以备不时之需!
Hope it will help out someone else with the same problem!
希望它可以帮助其他有同样问题的人!
回答by caw
PHP 5.3.0+ comes with locale_accept_from_http()
which gets the preferred language from the Accept-Language
header.
PHP 5.3.0+ 附带locale_accept_from_http()
它从Accept-Language
标题中获取首选语言。
You should always prefer this method to a self-written method as the header field is more complicated than one might think. (It's a list of weighted preferences.)
你应该总是更喜欢这种方法而不是自写的方法,因为头字段比人们想象的要复杂。(这是一个加权偏好列表。)
You should retrieve the language like this:
您应该像这样检索语言:
$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
But even then, you won't just have en
for every English user and es
for Spanish ones. It can become much more difficultthan that, and things like es-ES
and es-US
are standard.
但即便如此,您也不会只en
为每个英语用户和es
西班牙语用户提供服务。它可能变得比这要困难得多,并且像es-ES
和es-US
这样的东西都是标准的。
This means you should iterate over a list of regular expressions that you try and determine the page language that way. See PHP-I18Nfor an example.
这意味着您应该遍历您尝试的正则表达式列表并以这种方式确定页面语言。有关示例,请参阅PHP-I18N。
回答by Bora
I think your idea is great. May be help you shortest code:
我觉得你的想法很棒。可能会帮助你最短的代码:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
header("location: ".$lang."/index.php");
回答by samb90
That should work fine. You could also use http_negotiate_languageand discusses here
那应该可以正常工作。您也可以使用http_negotiate_language并在此处讨论
回答by Koray
Most useful this code
最有用的这段代码
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if(file_exists('system/lang/'.$lang.'.php'))
{
include('system/lang/'.$lang.'.php');
}else{
include('system/lang/en.php'); //set default lang here if not exists translated language in ur system
}