views:

161

answers:

0

I'm testing the code by changing an option in Firefox's options: "Options -> Choose your preferred language for displaying pages"

This is the code:

lang.php:

<?php

function dlang($Var) {
    if(empty($GLOBALS[$Var])) {
        $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
            $GLOBALS['_SERVER'][$Var]:
            (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
            $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
    }
}

function language() {
    // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
    dlang('HTTP_ACCEPT_LANGUAGE');
    dlang('HTTP_USER_AGENT');

    $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
    $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

    // Try to detect Primary language if several languages are accepted.
    foreach($GLOBALS['_LANG'] as $K) {
        if(strpos($_AL, $K)===0)
            return $K;
    }

    // Try to detect any language if not yet detected.
    foreach($GLOBALS['_LANG'] as $K) {
        if(strpos($_AL, $K)!==false)
            return $K;
    }
    foreach($GLOBALS['_LANG'] as $K) {
        if(preg_match("/[\[\( ]{$K}[;,_\-\)]/",$_UA))
            return $K;
    }

    // Return default language if language is not yet detected.
    return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
    'en',
    'es',
    'zh-tw',
    'zh-cn'
);
?>

session.php:

<?php
//proc all page display
include('lang.php'); //language detector
class Session
{
    var $lang;         //Username given on sign-up
    var $url;          //The page url current being viewed
    var $referrer;     //Last recorded site page viewed

    /* Class constructor */
    function Session() {
        $this->time = time();
        $this->startSession();
    }

    function cf($filename) { //function to clean a filename string so it is a valid filename
        $fp = explode('/',$filename);
        $num = count($fp);
        return $fp[$num-1];
    }

    /**
     * startSession - Performs all the actions necessary to
     * initialize this session object. Tries to determine if the
     * the user has logged in already, and sets the variables
     * accordingly. Also takes advantage of this page load to
     * update the active visitors tables.
     */
    function startSession() {
        session_start();   //Tell PHP to start the session

        /* Set referrer page */
        if(isset($_SESSION['url'])) {
            $this->referrer = $search = $this->cf($_SESSION['url']);
        }
        else {
            $this->referrer = "/";
        }

        /* Set current url */
        $this->url = $_SESSION['url'] = $this->cf($_SERVER['PHP_SELF']);

        /* Set user-determined language: */
        //set up languages array:
        //set cookie
        $langs = array('en','es','zh-tw', 'zh-cn');

        if(isset($_GET['lang'])){
            if(in_array($_GET['lang'],$langs)){
                $this->lang =  $_SESSION['lang'] = $_GET['lang'];
                setcookie("lang", $_SESSION['lang'], time() + (3600 * 24 * 30));
            }
        }
        else if(isSet($_COOKIE['lang'])) {
            $_SESSION['lang'] = $_COOKIE['lang'];
        }
        else {
            $_SESSION['lang'] = 'en';
        }
    }
};
/**
 * Initialize session object - This must be initialized before g
 * the form object because the form uses session variables,
 * which cannot be accessed unless the session has started.
 */
$session = new Session;
?>

localization:

<?php
include('session.php'); //language detector

// determine the value of $lang_file according the one in $lang
$languages = array('en', 'es', 'zh-tw', 'zh-cn');
if (in_array($_SESSION['lang'], $languages)) {
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
} else {
    $lang_file = 'lang.en.php';
}

// translation helper function
function l($translation) {
    global $lang;
    return $lang[$translation]; }

// include file for final output
    include_once 'languages/'.$lang_file;
?>

sample of index.php:

    <?php
    include_once 'localization.php';
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
        <title>Alex Chen - Web Development, Graphic Design, and Translation</title>
        <link rel="stylesheet" type="text/css" href="styles/global.css" />
        <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" />
...
    </head>
    <body id="home">
    <div id="header">
        <div class="container">
            <div id="header-top">
                <h1><a href="http://widerdesign.co.nr/"&gt;wider design</a></h1>
                <ul id="lang">
                    <li id="english" <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>><a href="index.php?lang=en">english</a></li>
                    <li <?php if($lang_file=='lang.es.php') {echo 'class="current"';} ?>><a href="index.php?lang=es">español</a></li>
                    <li <?php if($lang_file=='lang.zh-tw.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-tw">中文(繁體)</a></li>
                    <li <?php if($lang_file=='lang.zh-cn.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-cn">中文(简体)</a></li>
                </ul>
            </div>
        </div><!-- .container -->
    </div><!-- #header -->
    <div id="content">
        <div class="container">
            <div id="mainbar">
                <div id="tagline">
                    <div class="left">
                        <h3><?php echo l('tagline_h3'); ?></a></h3>
    ...

This is my page: [alexchen.co.nr/][1]