I18N
|
|
|
|
<?php // language.php
// notes
// - language codes are from ISO 639
// language_accept():
// takes a list of languages the current document is
// available in as a parameter, and returns either a
// language that the browser and server have in common,
// or an empty string if no match was found
// parameters:
// $accept - array of language codes to accept
// returns: negotiated language code or '' function language_accept($accept='') {
global $HTTP_ACCEPT_LANGUAGE;
global $HTTP_ACCEPT_CHARSET;
$lang=split(',', $HTTP_ACCEPT_LANGUAGE);
// parse http_accept_language header
foreach($lang as $i=>$value) {
$value=split(';', $value);
$lang[$i]=trim($value[0]);
}
return language_negotiate($lang, $accept);
}
// language_negotiate():
// finds a matching language between the two arrays. Tries
// to match whole language code, then first two characters
// (ie. 'en-us', 'en')
// parameters:
// $ask_lang - array of language codes the browser wants
// $accept_lang - array of language codes this document is available in
// returns: language code or '' function language_negotiate($ask_lang, $accept_lang) {
if (!(is_array($ask_lang) && is_array($accept_lang))) return '';
// if it exists exactly, or just the first two characters
foreach($ask_lang as $lang) {
if (in_array($lang, $accept_lang)) return $lang;
$short_lang=substr($lang, 0, 2);
if (in_array($short_lang, $accept_lang)) return $short_lang;
}
return '';
}
?>
|
|
|
Usage Example
|
// try changing your browser's language
echo "language=[";
echo language_accept(array('fr', 'en', 'es', 'de'));
echo "]<BR>";
|
|
|
Rate This Script
|
|
|
|