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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316476/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:00:40  来源:igfitidea点击:

How to get the language value from $_SERVER['HTTP_ACCEPT_LANGUAGE'] using PHP?

phpregexhttplocalizationinternationalization

提问by Steven

<?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 测试代码块时,我得到zh-cn.

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

回答by Pascal MARTIN

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
  • 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


关于解析该标题,这篇博文可能是一篇有趣的文章:解析接受语言以检测用户的语言

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.

这是一组按优先级排序的语言,按降序排列 - 这可能是您想要的。

回答by feeela

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

回答by nathanlrf

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.

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

回答by Grringo

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
    )

)