Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.language.php
<?php
/**************************************************************************
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/

class lang
{

	var $system_language = 'en_ca'; //Deprecated
	var $user_language = 'en_ca'; //Deprecated
	var $language = 'en_ca';
	var $msgs;
	var $current_service;
	
	/*!
		@function get_supported_languages()
		@author Ryan Thompson
		@abstract Gets supported languages from database
		@version 0.1
		@return $languages[] - array containing supported languages
		@since 06-11-2003
	*/
	function get_supported_languages()
	{
		GLOBAL $db;
		$sql = "SELECT lg_id FROM o_languages WHERE enabled='1'";
		$db->query($sql);

		while($db->fetch_results())
		{
			$languages[] = trim($db->record['lg_id']);
		}
		return $languages;
	}
	
	/*!
		@function get_language()
		@author Ryan Thompson
		@abstract Attempts to retrieve a valid language in order (User Prefs, Browser, System, fallback(en_ca))
		@version 0.1
		@params $user_id
		@return $this->language
		@since 20-11-2003
	*/
	function get_language($user_id)
	{
		
		if(!empty($user_id))
		{
			//We have a user lets get there preffered language
			$this->language = $this->get_user_language($user_id);			
		}
		
		if(empty($this->language))
		{
			//No preference. Let look at browser
			$lang = explode('.', $_ENV['LANG']);
			$this->language = strtolower($lang[0]);
			
		}
		
		if(empty($this->language))
		{
			$this->language = $this->get_sys_language();	
		}
		
		//Last resort if we can't find a valid language elsewhere
		if(empty($this->language))
		{
			$this->language = 'en_ca';
		}
		
		return;
		
	}
	
	/*!
		@function get_sys_language()
		@author Ryan Thompson
		@abstract Retrieves system language
		@version 0.2
		@return $system_language
		@since 24-03-2003
	*/
	function get_sys_language()
	{
		GLOBAL $db;
		
		$sql = "SELECT value FROM o_settings WHERE setting_id='7'";
		$db->query($sql);
		$db->fetch_results();
		return $db->record['value'];
	}
	
	/*!
		@function get_user_language()
		@author Ryan Thompson
		@abstract Retrieves user language
		@version 0.1
		@params $user_id
		@return $user_language
		@since 20-11-2003
	*/
	function get_user_language($user_id)
	{
		
		GLOBAL $db;
		$sql = "SELECT value FROM o_preferences WHERE service='gl' AND user_id='$user_id' AND preference='language'";
		$db->query($sql);
		$db->fetch_results();
		return $db->record['value'];
	}
			
	/*!
		@function get_msg()
		@author Ryan Thompson
		@abstract Retrieves a single message from database
		@version 0.1
		@params $id_text - The text identifier
		@params $service - The current active service
		@return $O->db->record['messages']
		@since 30-10-2003
	*/
	function get_msg($id_text, $service='gl')
	{
		GLOBAL $db;
		//$language = $O->lang->user_language;
		
		//Is there an easier way?
		$sql = "SELECT messages FROM o_text WHERE 
			id_text='$id_text' AND language='{$this->language}' AND service='$service'";
	
		$db->query($sql);
		$db->fetch_results();
		
		return $db->record['messages'];
	}

	/*!
		@function get_messages()
		@author Ryan Thompson
		@abstract Retrieves array of global and service related messages
		@version 0.1
		@params $service - The current active service
		@return $this->msgs
		@since 30-10-2003
	*/
	function get_messages($service)
	{
		GLOBAL $db;
		//$language = $this->user_language;
		$sql ="SELECT id_text, messages FROM o_text WHERE language='{$this->language}' AND service='$service'  OR service='gl'";
		$db->query($sql);
		
		while($db->fetch_results())
		{
			$this->msgs[$db->record['id_text']] = $db->record['messages'];
		}
		
		return $this->msgs;
	}
}

?>
Return current item: O - OpenSource GroupWare