Sources for file plugins/class/language.php in version 4.0 Beta 1



Click on a comment to hide it. Click here to show all comments.

/**
 * Project:     Xnyo 4: Bubbles
 * File:        plugins/class/language.php
 *
 * Version:     4.0-dev
 * SVN Id:      $Id: language.php 5 2007-05-18 03:49:07Z bok $
 * SVN URL:     $HeadURL:
http://svn.syd.wholesalebroadband.com.au/xnyo/trunk/plugins/class/language.php $
 * Authors:     Robert Amos <bok[at]odynia.org>
 *
 * Copyright (c) 2001-2007 Robert Amos <bok[at]odynia.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/

/**
 * This file sets the language that the user.. uses
**/

class XnyoLanguage implements XnyoClassPlugin
{
    
// Plugin Information
    
public $_plugin_info = array
    (
        
'name' => 'Xnyo Language Plugin',
        
'description' => 'Picks the language from a list of supported languages thats closest to what the
UA prefers'
,
        
'action' => XNYO_PLUGIN_ACTION_STORAGE,
        
'varname' => 'language'
    
);

    private 
$xnyo;

    
/**
     * Constructor
    **/
    
public function __construct ()
    {
        
$this->xnyo $GLOBALS['xnyo_parent'];

        
// initialise the session's language, if it doesn't already exist
        
if (!isset($_SESSION['xnyo_language']))
            
$_SESSION['xnyo_language'] = '';

        
// make nice shortcut
        
$this->xnyo->language = &$_SESSION['xnyo_language'];

        
// if there are no available languages specified, dont bother
        
if (!is_array($this->xnyo->session->languages) || !count($this->xnyo->session->languages))
        {
            if (empty(
$this->xnyo->language))
            {
                if (
XNYO_DEBUG$this->xnyo->d('No languages configured, setting $xnyo->language to the default
(<b>%s</b>)'
$this->xnyo->session->default_language);
                
$this->xnyo->language $this->xnyo->session->default_language;
            }
            return 
true;
        }

        
// check the request variables for a language
        
if (isset($_REQUEST['lang']) && (isset($this->xnyo->session->languages[$_REQUEST['lang']]) ||
in_array($_REQUEST['lang'], $this->xnyo->session->languages)))
        {
            
$_REQUEST['lang'] = XnyoInput::safetext($_REQUEST['lang']);
            if (
XNYO_DEBUG$this->xnyo->d('Found a valid language in $_REQUEST, changing langage to
<b>%s</b>'
$_REQUEST['lang']);
            
$this->xnyo->language $_REQUEST['lang'];
            return 
true;
        }
        
        
// do we already have a language?
        
if (!empty($this->xnyo->language))
            
// no point going ahead and checking more
            
return true;

        
// check the Accept-Language header
        
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
            
// for all languages as seperated by a comma
            
foreach (explode(','$_SERVER['HTTP_ACCEPT_LANGUAGE']) as $key => $var)
            {
                
// cleanup excess whitespaces
                
$var trim($var);
                
                
// clean up additional percentage information - we'll add support for this soon
                
if (strpos($var';') !== false)
                    
$var substr($var0strpos($var';'));
                
                
// last cleanup
                
$var XnyoInput::safetext($var);

                
// check this value in the language list
                
if (isset($this->xnyo->session->languages[$var]) || in_array($var,
$this->xnyo->session->languages))
                {
                    if (
XNYO_DEBUG$this->xnyo->d('Matched a language in the Accept-Language header, setting
language to <b>%s</b>'
$var);
                    
$this->xnyo->language $var;
                    return 
true;
                }
            }
        }
        
        if (
XNYO_DEBUG$this->xnyo->d('No languages found in $_REQUEST or via Accept-Language header,
setting langauge to default (<b>%s</b>)'
$this->xnyo->session->default_language);
        
$this->xnyo->language $this->xnyo->session->default_language;
    }

}


Website is Copyright © Odynia.org 2000-2005 - Xnyo is released under a BSD license.