<?php

function __($string) {
	return i18n::getTranslation($string);
}

function smarty_i18n($args, Smarty $smarty) {
	if (!array_key_exists('text', $args) or !$args['text']) {
		return '';
	}
	return i18n::getTranslation($args['text']);
}

/**
 * Internationalization (i18n) service facade
 *
 * Provides configuration for i18n service.
 * Generates translation stub objects.
 *
 * @version DR1.6
 */
final class i18n {



	public static $cookieExpiry = 31536000;
	public static $cookieName = 'i18n';

	private static $defaultLang = 'en_EN';
	private static $backend;



	/**
	 * Returns translation stub object
	 *
	 * Instantiates and configures translation stub object.
	 * Stub object will perform delayed translation while being converted to string.
	 *
	 * @param string string to be translated
	 * @return i18nTranslation translation stub object
	 */
	public static function getTranslation($string) {
		if (!self::$backend) {
			throw new Exception('i18n backend not configured');
		}
		return new i18nTranslation($string, self::getLang(), self::$backend);
	}



	/**
	 * Configures i18n translation service backend
	 *
	 * @param i18nTranslationService translation service object
	 */
	public static function setBackend(i18nTranslationService $backend) {
		self::$backend = $backend;
	}



	/**
	 * Configures default language
	 *
	 * @param string language symbol, e.g. en_EN
	 */
	public static function setDefaultLang($lang) {
		self::$defaultLang = $lang;
	}



	/**
	 * Configures current language
	 *
	 * @param string language symbol, e.g. en_EN
	 */
	public static function setLang($lang) {
		setcookie(self::$cookieName, $lang, time() + self::$cookieExpiry);
		$_COOKIE[self::$cookieName] = $lang;
	}



	/**
	 * Return current language
	 *
	 * If current language is not configured default language will be returned.
	 *
	 * @return string current language
	 */
	public static function getLang() {
		if (!array_key_exists(self::$cookieName, $_COOKIE)) {
			self::setLang(self::$defaultLang);
		}
		return $_COOKIE[self::$cookieName];
	}



} // class i18n

?>