如何使用PHP从$_SERVER['HTTP_ACCEPT_LANGUAGE']获取语言值?

时间:2021-10-25 15:08:55
<?php
$language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
echo $language;
?>

When I use Firefox to test this block of code, I get en-us,en;q=0.7,ja;q=0.3,

当我使用Firefox测试这段代码时,我得到en-us,en;q=0.7,ja;q=0.3,

when I use IE to test the block of code, I get zh-cn.

当我使用IE来测试代码块时,我得到了zhcn。

Is the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] a string? How to determine whether the preferred language is Chinese or Japanese? How can I write a regular expression to get the language from the value of $_SERVER['HTTP_ACCEPT_LANGUAGE']?

$_SERVER['HTTP_ACCEPT_LANGUAGE']的值是字符串吗?如何确定首选语言是中文还是日语?我如何编写正则表达式来从$_SERVER的值['HTTP_ACCEPT_LANGUAGE']中获取语言?

4 个解决方案

#1


26  

Yes, the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] is a string -- see $_SERVER.

是的,$_SERVER['HTTP_ACCEPT_LANGUAGE']的值是一个字符串——参见$_SERVER。

Its content is sent by the browser -- which explains why you get different results depending on the browser you are using : most likely, your Firefox is configured to request pages in english (high priority) or japanese (low priority), while your IE is configured to request pages in chinese.

它的内容是由浏览器发送的——这就解释了为什么根据使用的浏览器而得到不同的结果:最可能的情况是,您的Firefox配置为以英语(高优先级)或日语(低优先级)请求页面,而您的IE配置为以中文请求页面。

This is because that HTTP header can contain :

这是因为HTTP头可以包含:

  • a list of languages
  • 一个语言列表
  • optionnaly, with region codes
  • optionnaly,地区代码
  • with associated priorities.
  • 与相关的优先事项。

The idea being that the server should respond, using the language that suits "the best" what's requested by the user.

其思想是,服务器应该响应,使用适合用户请求的“最佳”语言。


About parsing that header, this blog-post might be a interesting read : Parse Accept-Language to detect a user's language

关于解析这个标题,这个博客文章可能是一个有趣的阅读:解析Accept-Language来检测用户的语言。

There is a portion of code proposed to parse that HTTP header -- and it generates an array that looks like this (quoting) :

有一部分代码建议解析HTTP报头——它生成的数组如下(引用):

Array
(
    [en-ca] => 1
    [en] => 0.8
    [en-us] => 0.6
    [de-de] => 0.4
    [de] => 0.2
)

Which is an array of languages, sorted by priority, in descending order -- which is probably what you want.

这是一个语言数组,按优先级排序,按降序排序——这可能是你想要的。

#2


4  

I just use explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']) to get the first possible language that my client might use. It works fine on chrome and IE 10. Not sure if it would be wrong on other browsers.

我只是使用explosion(“,”,$_SERVER['HTTP_ACCEPT_LANGUAGE'])来获得我的客户机可能使用的第一种可能的语言。它在chrome和IE 10上运行良好。不确定在其他浏览器上是否会出错。

#3


4  

As of v5.3 PHP has function for that purpose:

在v5.3中,PHP就有这样的功能:

$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);

See: http://php.net/manual/en/locale.acceptfromhttp.php

参见:http://php.net/manual/en/locale.acceptfromhttp.php

#4


2  

try this:

试试这个:

function getUserBaseLanguage() {
    global $_SERVER;
    $accept_languages           = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $accept_languages_arr       = explode(",",$accept_languages);
    foreach($accept_languages_arr as $accept_language) {
        preg_match ("/^(([a-zA-Z]+)(-([a-zA-Z]+)){0,1})(;q=([0-9.]+)){0,1}/" , $accept_language, $matches );
        if (!$matches[6]) $matches[6]=1;
        $result[$matches[1]] = array(
            'lng_base'  => $matches[2],
            'lng_ext'   => $matches[4],
            'lng'       => $matches[1],
            'priority'  => $matches[6],
            '_str'      => $accept_language,
        );
    }
    return $result;
}

print_r(getUserBaseLanguage());

output:

输出:

Array
(
[pl] => Array
    (
        [lng_base] => pl
        [lng_ext] => 
        [lng] => pl
        [priority] => 1
        [_str] => pl
    )

[en-US] => Array
    (
        [lng_base] => en
        [lng_ext] => US
        [lng] => en-US
        [priority] => 0.7
        [_str] => en-US;q=0.7
    )

[en] => Array
    (
        [lng_base] => en
        [lng_ext] => 
        [lng] => en
        [priority] => 0.3
        [_str] => en;q=0.3
    )

)

#1


26  

Yes, the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] is a string -- see $_SERVER.

是的,$_SERVER['HTTP_ACCEPT_LANGUAGE']的值是一个字符串——参见$_SERVER。

Its content is sent by the browser -- which explains why you get different results depending on the browser you are using : most likely, your Firefox is configured to request pages in english (high priority) or japanese (low priority), while your IE is configured to request pages in chinese.

它的内容是由浏览器发送的——这就解释了为什么根据使用的浏览器而得到不同的结果:最可能的情况是,您的Firefox配置为以英语(高优先级)或日语(低优先级)请求页面,而您的IE配置为以中文请求页面。

This is because that HTTP header can contain :

这是因为HTTP头可以包含:

  • a list of languages
  • 一个语言列表
  • optionnaly, with region codes
  • optionnaly,地区代码
  • with associated priorities.
  • 与相关的优先事项。

The idea being that the server should respond, using the language that suits "the best" what's requested by the user.

其思想是,服务器应该响应,使用适合用户请求的“最佳”语言。


About parsing that header, this blog-post might be a interesting read : Parse Accept-Language to detect a user's language

关于解析这个标题,这个博客文章可能是一个有趣的阅读:解析Accept-Language来检测用户的语言。

There is a portion of code proposed to parse that HTTP header -- and it generates an array that looks like this (quoting) :

有一部分代码建议解析HTTP报头——它生成的数组如下(引用):

Array
(
    [en-ca] => 1
    [en] => 0.8
    [en-us] => 0.6
    [de-de] => 0.4
    [de] => 0.2
)

Which is an array of languages, sorted by priority, in descending order -- which is probably what you want.

这是一个语言数组,按优先级排序,按降序排序——这可能是你想要的。

#2


4  

I just use explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']) to get the first possible language that my client might use. It works fine on chrome and IE 10. Not sure if it would be wrong on other browsers.

我只是使用explosion(“,”,$_SERVER['HTTP_ACCEPT_LANGUAGE'])来获得我的客户机可能使用的第一种可能的语言。它在chrome和IE 10上运行良好。不确定在其他浏览器上是否会出错。

#3


4  

As of v5.3 PHP has function for that purpose:

在v5.3中,PHP就有这样的功能:

$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);

See: http://php.net/manual/en/locale.acceptfromhttp.php

参见:http://php.net/manual/en/locale.acceptfromhttp.php

#4


2  

try this:

试试这个:

function getUserBaseLanguage() {
    global $_SERVER;
    $accept_languages           = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $accept_languages_arr       = explode(",",$accept_languages);
    foreach($accept_languages_arr as $accept_language) {
        preg_match ("/^(([a-zA-Z]+)(-([a-zA-Z]+)){0,1})(;q=([0-9.]+)){0,1}/" , $accept_language, $matches );
        if (!$matches[6]) $matches[6]=1;
        $result[$matches[1]] = array(
            'lng_base'  => $matches[2],
            'lng_ext'   => $matches[4],
            'lng'       => $matches[1],
            'priority'  => $matches[6],
            '_str'      => $accept_language,
        );
    }
    return $result;
}

print_r(getUserBaseLanguage());

output:

输出:

Array
(
[pl] => Array
    (
        [lng_base] => pl
        [lng_ext] => 
        [lng] => pl
        [priority] => 1
        [_str] => pl
    )

[en-US] => Array
    (
        [lng_base] => en
        [lng_ext] => US
        [lng] => en-US
        [priority] => 0.7
        [_str] => en-US;q=0.7
    )

[en] => Array
    (
        [lng_base] => en
        [lng_ext] => 
        [lng] => en
        [priority] => 0.3
        [_str] => en;q=0.3
    )

)