PX : code

language negotiation by Stig S. Bakken
Download this code


<?php

/*
 * $Id: language.phl,v 1.3 1998/08/03 13:21:45 ssb Exp $
 */

$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;
}

?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.