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

后端 未结 4 1914
野性不改
野性不改 2020-12-08 11:19

When I use Firefox to test this block of code, I get en-us,en;

相关标签:
4条回答
  • 2020-12-08 12:14

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

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

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

    0 讨论(0)
  • 2020-12-08 12:16

    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
        )
    
    )
    
    0 讨论(0)
  • 2020-12-08 12:20

    Yes, the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] is a string -- see $_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.

    This is because that HTTP header can contain :

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

    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.

    0 讨论(0)
  • 2020-12-08 12:21

    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.

    0 讨论(0)
提交回复
热议问题