Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 280
Steven Avatar asked Feb 23 '10 07:02

Steven


4 Answers

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.

like image 116
Pascal MARTIN Avatar answered Nov 18 '22 03:11

Pascal MARTIN


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

like image 7
feeela Avatar answered Nov 18 '22 03:11

feeela


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.

like image 4
nathanlrf Avatar answered Nov 18 '22 05:11

nathanlrf


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
    )

)
like image 3
Grringo Avatar answered Nov 18 '22 05:11

Grringo