Location: PHPKode > scripts > Internationalization and Localization > internationalization-and-localization/I18N.class.php
<?php

/**
 * Project:     CWF: Cricava Web Framework
 * File:        I18N.class.php
 *
 * @link http://www.cricava.com/
 * @copyright 2005-2009 Cricava Technologies, Inc.
 * @author Mariano Iglesias <hide@address.com>
 * @package com.cricava.cwf
 * @version 1.0
 */

/**#@+
 * Includes
 */
require_once(dirname(__FILE__) . '/DatabaseDataSource.class.php');
require_once(dirname(__FILE__) . '/FileDataSource.class.php');
require_once(dirname(__FILE__) . '/XmlDataSource.class.php');
/**#@-*/

/**
 * This class manages internationalization & localization based on a language
 * and locale code, and it gets its strings from resources files, or database.
 *
 * @author  Mariano Iglesias
 * @package com.cricava.cwf
 * @subpackage i18n
 * @since   1.0
 */

class I18N
{
	/**
	 * Current container
	 *
	 * @access private
	 * @since  1.0
	 * @var   string
	 */
	var $container;

	/**
	 * Current language.(two-letter ISO 639_1 code)
	 *
	 * @access private
	 * @since  1.0
	 * @var   string
	 */
	var $language;

	/**
	 * Current locale.(two-letter ISO 3166_1 Alpha 2 code)
	 *
	 * @access private
	 * @since  1.0
	 * @var   string
	 */
	var $locale;

	/**
	 * Current data source (an implementation of DataSource)
	 *
	 * @access private
	 * @since  1.0
	 * @var   DataSource
	 */
	var $dataSource;

    /**
     * Create a new I18N instance, setting default values: $language = 'en', $locale = null, $container = 'language'
     *
     * @access public
     * @since  1.0
     */
	function & I18N()
	{
		$this->language = 'en';
		$this->locale = null;
		$this->container = 'language';
	}

	/**
	 * Function that handles the fetching of elements.
	 * <ul>
	 * <li>If no element specified, it will load all elements for a section.</li>
	 * <li>If no section specified, it will load all sections for the current container.</li>
	 * </ul>
	 *
	 * @param string	The section from where to get elements
	 * @param string	The element to obtain
	 *
	 * @return mixed	If element specified, a string, otherwise an array
	 *
	 * @access public
	 * @since 1.0
	 */
	function & fetch($section = null, $element = null)
	{
		$elements =& $this->dataSource->fetch($this->language, $this->locale, $this->container, $section, $element);

		if (isset($elements) && count($elements) > 0)
		{
			if (isset($element))
			{
				$elements = $elements[$this->container][$section][$element];
			}
			else if (isset($section))
			{
				$elements = $elements[$this->container][$section];
			}
			else
			{
				$elements = $elements[$this->container];
			}
		}

		return $elements;
	}

	/**
	 * Obtain the list of languages and their locales.
	 *
	 * @param boolean	true if you want to obtain only active languages & locales, false otherwise
	 *
	 * @return array	The list of languages
	 *
	 * @access public
	 * @since 1.0
	 */
	function & fetchLanguages($onlyActive = true)
	{
		$languages =& $this->dataSource->fetchLanguages($this->language, $onlyActive);

		return $languages;
	}

	/**
	 * Get the current container
	 *
	 * @return string	Current container
	 *
	 * @access public
	 * @since 1.0
	 */
	function getContainer()
	{
		return $this->container;
	}

	/**
	 * Get the current language.(two-letter ISO 639_1 code)
	 *
	 * @return string	Current language
	 *
	 * @access public
	 * @since 1.0
	 */
	function getLanguage()
	{
		return $this->language;
	}

	/**
	 * Get the current locale.(two-letter ISO 3166_1 Alpha 2 code)
	 *
	 * @return string	Current locale
	 *
	 * @access public
	 * @since 1.0
	 */
	function getLocale()
	{
		return $this->locale;
	}

	/**
	 * Get the current source (an implementation of DataSource)
	 *
	 * @return DataSource	Current source
	 *
	 * @access public
	 * @since 1.0
	 */
	function getDataSource()
	{
		return $this->dataSource;
	}

	/**
	 * Sett the current container
	 *
	 * @param string	Current container
	 *
	 * @access public
	 * @since 1.0
	 */
	function setContainer($container)
	{
		$this->container = $container;
	}

	/**
	 * Set the current language.(two-letter ISO 639_1 code)
	 *
	 * @param string	Current language
	 *
	 * @access public
	 * @since 1.0
	 */
	function setLanguage($language)
	{
		$this->language = $language;
	}

	/**
	 * Set the current locale.(two-letter ISO 3166_1 Alpha 2 code)
	 *
	 * @param string	Current locale
	 *
	 * @access public
	 * @since 1.0
	 */
	function setLocale($locale)
	{
		$this->locale = $locale;
	}

	/**
	 * Set the current source.
	 *
	 * @param DataSource	Current source (an implementation of DataSource)
	 *
	 * @access public
	 * @since 1.0
	 */
	function setDataSource($dataSource)
	{
		$this->dataSource = $dataSource;
	}
}
?>
Return current item: Internationalization and Localization