PHP 5.4.31 Released

http_negotiate_language

(PECL pecl_http >= 0.1.0)

http_negotiate_languageNegotiate client's preferred language

Beschreibung

string http_negotiate_language ( array $supported [, array &$result ] )

This function negotiates the clients preferred language based on its Accept-Language HTTP header. The qualifier is recognized and languages without qualifier are rated highest. The qualifier will be decreased by 10% for partial matches (i.e. matching primary language).

Parameter-Liste

supported

array containing the supported languages as values

result

will be filled with an array containing the negotiation results

R├╝ckgabewerte

Returns the negotiated language or the default language (i.e. first array entry) if none match.

Beispiele

Beispiel #1 Using http_negotiate_language()

<?php
$langs 
= array(
        
'en-US',// default
        
'fr',
        
'fr-FR',
        
'de',
        
'de-DE',
        
'de-AT',
        
'de-CH',
);

include 
'./langs/'http_negotiate_language($langs$result) .'.php';

print_r($result);
?>

add a note add a note

User Contributed Notes 3 notes

up
12
Anonymous
5 years ago
as I don't have this function available I tried to write it myself. Maybe useful for someone else, too.

<?php
/*
  determine which language out of an available set the user prefers most
 
  $available_languages        array with language-tag-strings (must be lowercase) that are available
  $http_accept_language    a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out)
*/
function prefered_language ($available_languages,$http_accept_language="auto") {
   
// if $http_accept_language was left out, read it from the HTTP-Header
   
if ($http_accept_language == "auto") $http_accept_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';

   
// standard  for HTTP_ACCEPT_LANGUAGE is defined under
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
    // pattern to find is therefore something like this:
    //    1#( language-range [ ";" "q" "=" qvalue ] )
    // where:
    //    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
    //    qvalue         = ( "0" [ "." 0*3DIGIT ] )
    //            | ( "1" [ "." 0*3("0") ] )
   
preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" .
                  
"(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i",
                  
$http_accept_language, $hits, PREG_SET_ORDER);

   
// default language (in case of no hits) is the first in the array
   
$bestlang = $available_languages[0];
   
$bestqval = 0;

    foreach (
$hits as $arr) {
       
// read data from the array of this hit
       
$langprefix = strtolower ($arr[1]);
        if (!empty(
$arr[3])) {
           
$langrange = strtolower ($arr[3]);
           
$language = $langprefix . "-" . $langrange;
        }
        else
$language = $langprefix;
       
$qvalue = 1.0;
        if (!empty(
$arr[5])) $qvalue = floatval($arr[5]);
     
       
// find q-maximal language 
       
if (in_array($language,$available_languages) && ($qvalue > $bestqval)) {
           
$bestlang = $language;
           
$bestqval = $qvalue;
        }
       
// if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does)
       
else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) {
           
$bestlang = $langprefix;
           
$bestqval = $qvalue*0.9;
        }
    }
    return
$bestlang;
}
?>
up
0
Mathias Leppich
3 days ago
While http_negotiate_language() lacks a separat parameter to provide the contents of the "Accept-Language" header, it is possible to feed the parameter by overwriting the $_SERVER["HTTP_ACCEPT_LANGUAGE"] super global...

<?php
$old
= $_SERVER["HTTP_ACCEPT_LANGUAGE"];
$_SERVER["HTTP_ACCEPT_LANGUAGE"] = "zh-cn,zh,zh-tw;q=0.7,de;q=0.5,en;q=0.3";
$lang = http_negotiate_language(["en","de"]);
$_SERVER["HTTP_ACCEPT_LANGUAGE"] = $old; // restore original header value
var_dump($lang); // string(2) "de"
?>
up
-1
Fiumara, Pablo Alejandro
8 months ago
This function http_negotiate_language() is more modern than using $_SERVER["HTTP_ACCEPT_LANGUAGE"]
To Top