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

27,137

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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
    )

)
Share:
27,137
Steven
Author by

Steven

I'm a headhunter based in Hangzhou, China. I recruit software engineers, language experts, C-suite talents for companies. If you need my headhunting services, you can contact me via steven.tu[at]mipfanyi.com(Please replace "[at]" with "@"). If you want to get a job, you can also send a resume to my email.

Updated on July 20, 2020

Comments

  • Steven
    Steven almost 4 years
    <?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,

    when I use IE to test the block of code, I get 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']?