| version 1.15, 2003/01/26 00:21:47 |
version 1.16, 2003/01/26 12:46:30 |
|
|
| * "en-us" does not result into "en") |
* "en-us" does not result into "en") |
| * @return string array of accepted languages according to Q |
* @return string array of accepted languages according to Q |
| */ |
*/ |
| function parseHttpAcceptLanguage($accept_lang = null, $strict = 0) /* {{{ */ |
function parse_HTTP_accept_language($accept_lang = null, $strict = 0) /* {{{ */ |
| { |
{ |
| $accept_lang == null && $accept_lang = (string) @$_SERVER['HTTP_ACCEPT_LANGUAGE']; |
$accept_lang == null && $accept_lang = (string) @$_SERVER['HTTP_ACCEPT_LANGUAGE']; |
| $accept_lang = strtolower($accept_lang); |
$accept_lang = strtolower($accept_lang); |
|
|
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** |
/** |
| * Returns top level domain name |
* Returns top level domain name. |
| * |
* |
| * @param string $remote_host remote hostname (if not present, it will |
* @param string $remote_host special remote hostname (if not present, |
| * extracted from environment) |
* it will extracted from environment) |
| * @return string top level domain name |
* @return string top level domain name |
| */ |
*/ |
| function getTopLevelDomain($remote_host = null) /* {{{ */ |
function get_top_level_domain($remote_host = null) /* {{{ */ |
| { |
{ |
| $remote_host == null && $remote_host = @$HTTP_SERVER_VARS['REMOTE_HOST']; |
$remote_host == null && $remote_host = @$_SERVER['REMOTE_HOST']; |
| if (empty($remote_host)) { |
if (empty($remote_host)) { |
| $remote_host = @gethostbyaddr(@$HTTP_SERVER_VARS['REMOTE_ADDR']); |
$remote_host = @gethostbyaddr(@$_SERVER['REMOTE_ADDR']); |
| } |
} |
| if (!empty($remote_host) && ereg("\.[^\.]+$", $remote_host, $arr)) { |
if (!empty($remote_host) && preg_match('|\.([^\.]+)$|', $remote_host, $matches)) { |
| $lang = strtolower($arr[1]); |
$lang = strtolower($matches[1]); |
| return $lang; |
return $lang; |
| } |
} |
| return false; |
return false; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| |
/* |
| |
* Negotiate client's language. |
| |
* |
| |
* @param array $l_core_ar array of possible languages |
| |
* @param string $accept_lang special Accept-Language header; |
| |
* see Platon::parse_HTTP_accept_language() |
| |
* @param string $remote_host special remote hostname |
| |
* see Platon::get_top_level_domain() |
| |
* @return string negotiated language ($accepted array member) |
| |
*/ |
| |
function negotiate_language($l_core_ar, $accept_lang = null, $remote_host = null) /* {{{ */ |
| |
{ |
| |
// Sanity check |
| |
if (count($l_core_ar) < 2) { |
| |
if (count($l_core_ar) == 1) { |
| |
return $l_core_ar[0]; |
| |
} |
| |
return false; |
| |
} |
| |
// Groups of similar languages. Every language can be only in one group. |
| |
// This array needs to be little bit updated in future. |
| |
static $lang_groups = array( |
| |
array('sk', 'cz', 'cs'), |
| |
array('en', 'gb', 'us', 'au') |
| |
); |
| |
$l_default = array_shift($l_core_ar); |
| |
$l_user_ar = Platon::parse_HTTP_accept_language($accept_lang); |
| |
$l_tld = Platon::get_top_level_domain($remote_host); |
| |
$l_tld != false && $l_user_ar += array($l_tld); |
| |
//var_dump($l_user_ar); |
| |
for ($i = 0; $i < count($l_user_ar); $i++) { |
| |
$lang = $l_user_ar[$i]; |
| |
if (in_array($lang, $l_core_ar)) { |
| |
return $lang; |
| |
} |
| |
for ($j = 0; $j < count($lang_groups); $j++) { |
| |
if (! in_array($lang, $lang_groups[$j])) { |
| |
continue; |
| |
} |
| |
for ($k = 0; $k < count($lang_groups[$j]); $k++) { |
| |
if (in_array($lang_groups[$j][$k], $l_core_ar)) { |
| |
return $lang_groups[$j][$k]; |
| |
} |
| |
} |
| |
break; |
| |
} |
| |
} |
| |
return $l_default; |
| |
} /* }}} */ |
| |
|
| } |
} |
| |
|
| /* vim: set expandtab tabstop=4 shiftwidth=4: |
/* vim: set expandtab tabstop=4 shiftwidth=4: |