自动检测语言并重定向用户 [英] Auto detect language and redirect user

查看:89
本文介绍了自动检测语言并重定向用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做自己的网站,我设法编写了一些代码,该代码可以根据浏览器的语言将用户定向到该语言版本.这是脚本:

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

我已经将它放在index.php中.之所以似乎有效,是因为我不在讲英语的国家/地区,但是我的浏览器是英文,并且将我重定向到了英文版本.

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.

这是正确的吗?有更好/更清洁的方法吗?

Is this correct? Is there a better/cleaner way to do this?

推荐答案

好吧,我遇到了一些代码问题,这并不奇怪,因为我不是PHP专家.因此,我一直在寻找可能的解决方案,并在另一个网站上找到了以下代码:

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
    // 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();
}
?>

这做得很好!我只剩下一个问题.即使直接链接到另一种语言,也无法更改语言,因为一旦页面加载,php块就会将我重定向到浏览器的语言.如果您居住在另一个国家,并且以瑞典语为母语,但是由于您在英国购买了计算机,则使用英语的浏览器,可能会出现问题.

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.

因此,我针对此问题的解决方案是为每种语言(甚至是主要语言的一种)创建具有相同版本的文件夹,而index.html上却没有此php代码(因此不是index.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!

希望它可以帮助遇到相同问题的其他人!

Hope it will help out someone else with the same problem!

这篇关于自动检测语言并重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆