简体   繁体   English

自动检测语言并重定向用户

[英]Auto detect language and redirect user

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 . 我把它放在index.php之前。 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. 这似乎有效,因为我不在英语国家,但我的浏览器是英文,我被重定向到英文版。

Is this correct? 它是否正确? Is there a better/cleaner way to do this? 有没有更好/更清洁的方法来做到这一点?

Well, I came across some problems with my code which is no surprise due to I am not a PHP expert. 好吧,我遇到了一些代码问题,这并不奇怪,因为我不是PHP专家。 I kept therefore on searching for a possible solution and I found the following code on another website: 因此,我一直在寻找可能的解决方案,我在另一个网站上找到了以下代码:

<?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. 没有办法改变语言,即使是直接链接到另一种语言,因为只要页面加载,php块就会将我重定向到borwser的语言。 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. 如果您居住在另一个国家并且例如瑞典语作为母语,但是您的英语浏览器是因为您在英国购买了计算机,这可能会成为一个问题。

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). 所以我对这个问题的解决方案是在index.html上没有这个php代码(因此不是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! 所以现在我的网站自动检测语言,用户也可以选择手动更改它,以备不时之需!

Hope it will help out someone else with the same problem! 希望它能帮助其他人解决同样的问题!

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']);

But even then, you won't just have en for every English user and es for Spanish ones. 但即便如此,你不会只是有en的每一个英语用户和es西班牙语的。 It can become much more difficult than that, and things like es-ES and es-US are standard. 它可能变得更加困难es-ESes-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

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");

That should work fine. 这应该工作正常。 You could also use http_negotiate_language and discusses here 你也可以使用http_negotiate_language并在这里讨论

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM