Liebe(r) Carmen,
Deine Implementierung der Content-Negotiation ist nicht sonderlich flexibel. Möchtest Du das nicht mit etwas mehr Funktionalität haben?
Als Merzedes unter solchen Lösungen kann man diese PHP-Klasse empfehlen: Content Negotiation tools for PHP
In meinem Projekt verwende ich diese Funktion zum Erkennen der gewünschten Sprache:
/**
* language negotiation
*
* @param array $allowed set of allowed or available languages
* @param string $default language to be used as default
* @param bool $strict_mode don't lower-case language parameters
*
* @return string final choice for language
*/
function get_http_lang ($allowed, $default, $strict_mode = true) {
// split header
$accepted_languages = preg_split('~,\s*~', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
// set standard values
$current_lang = $default;
$current_q = 0;
// process all allowed languages
foreach ($accepted_languages as $accepted) {
// get all available information about each language
$res = preg_match(
'/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i',
$accepted,
$matches
);
// syntax valid?
if (!$res) {
// no? ignore
continue;
}
// get language code and split into individual parts
$lang_code = explode('-', $matches[1]);
// quality provided?
if (isset($matches[2])) {
// use quality
$lang_quality = (float) $matches[2];
} else {
// compatibility mode: assume a quality of 1
$lang_quality = 1.0;
}
// until code has been fully used...
while (count($lang_code)) {
// is language among allowed languages?
if (in_array(strtolower (join ('-', $lang_code)), $allowed)) {
// test quality
if ($lang_quality > $current_q) {
// use this language
$current_lang = strtolower(join('-', $lang_code));
$current_q = $lang_quality;
// leave inner while loop
break;
}
}
// don't minimize language when in strict mode
if ($strict_mode) {
// break inner while loop
break;
}
// cut right-most part
array_pop($lang_code);
}
}
return $current_lang;
}
Liebe Grüße,
Felix Riesterer.