Mega Code Archive

 
Categories / Php / Cookie Session
 

Function that does language negotiation based on the Accept-Language

header, a cookie or host name <?php $supported_languages = array( "no" => 1, /* Norwegian */ "en" => 1 /* English */ ); $default_language = "en"; /* Try to figure out which language to use. */ function negotiate_language($lang) { global $supported_languages, $HTTP_ACCEPT_LANGUAGE; if (isset($supported_languages[$lang])) { return $lang; } /* If the client has sent an Accept-Language: header, * see if it is for a language we support. */ if ($HTTP_ACCEPT_LANGUAGE) { $accepted = explode( ",", $HTTP_ACCEPT_LANGUAGE); for ($i = 0; $i < count($accepted); $i++) { if ($supported_languages[$accepted[$i]]) { return $accepted[$i]; } } } /* One last desperate try: check for a valid language code in the * top-level domain of the client's source address. */ if (eregi( "\\.[^\\.]+$"", $REMOTE_HOST, &$arr)) { $lang = strtolower($arr[1]); if ($supported_languages[$lang]) { return $lang; } } global $default_language; return $default_language; } ?>