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?
它是否正确?有没有更好/更清洁的方法来做到这一点?
5 个解决方案
#1
15
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 difficult than that, and things like es-ES
and es-US
are standard.
但即使这样,你也不会只为每个英语用户和西班牙语用户提供。它可能变得更加困难,像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-I18N for an example.
这意味着您应该遍历您尝试的正则表达式列表,并以此方式确定页面语言。有关示例,请参阅PHP-I18N。
#2
12
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块就会将我重定向到borwser的语言。如果您居住在另一个国家并且例如瑞典语作为母语,但是您的英语浏览器是因为您在英国购买了计算机,这可能会成为一个问题。
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上没有此php代码(因此不是index.php),为每种语言(甚至是主语言的一种语言)创建具有重复版本的文件夹。所以现在我的网站会自动检测语言,用户也可以选择手动更改它,以备不时之需!
Hope it will help out someone else with the same problem!
希望它能帮助其他人解决同样的问题!
#3
5
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");
#4
2
That should work fine. You could also use http_negotiate_language and discusses here
这应该工作正常。您也可以使用http_negotiate_language并在此处讨论
#5
-1
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
}
#1
15
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 difficult than that, and things like es-ES
and es-US
are standard.
但即使这样,你也不会只为每个英语用户和西班牙语用户提供。它可能变得更加困难,像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-I18N for an example.
这意味着您应该遍历您尝试的正则表达式列表,并以此方式确定页面语言。有关示例,请参阅PHP-I18N。
#2
12
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块就会将我重定向到borwser的语言。如果您居住在另一个国家并且例如瑞典语作为母语,但是您的英语浏览器是因为您在英国购买了计算机,这可能会成为一个问题。
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上没有此php代码(因此不是index.php),为每种语言(甚至是主语言的一种语言)创建具有重复版本的文件夹。所以现在我的网站会自动检测语言,用户也可以选择手动更改它,以备不时之需!
Hope it will help out someone else with the same problem!
希望它能帮助其他人解决同样的问题!
#3
5
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");
#4
2
That should work fine. You could also use http_negotiate_language and discusses here
这应该工作正常。您也可以使用http_negotiate_language并在此处讨论
#5
-1
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
}