Location: PHPKode > scripts > Claw > claw/ext/SmartyClawRendererI18n.php
<?php

/**
 * Internationalization helper for ClawRenderer. Useful with controllers too
 *
 * @author Tomas Varaneckas <tomas [dot] varaneckas [at] gmail [dot] com>
 * @package claw.ext
 * @version $Id: SmartyClawRendererI18n.php 120 2006-03-19 19:26:44Z spajus $
 */
class SmartyClawRendererI18n extends SmartyClawRendererHelper
{
	/**
	 * Block function array
	 *
	 * @var Array
	 */
	private $blocks = array();

	/**
	 * Allowed function array
	 *
	 * @var Array
	 */	
	private $functions = array(); 

	/**
	 * Dictionary (Two-level ClawConfig)
	 *
	 * @var ClawConfig
	 */	
	private $dictionary;	

	/**
	 * Currently active language
	 *
	 * @var string
	 */
	private $language;

	/**
	 * Gets array of block functions
	 *
	 * @return Array
	 */	
	public function getBlockFunctions()
	{
		return $this->blocks;
	}

	/**
	 * Gets array of allowed functions
	 *
	 * @return Array
	 */		
	public function getAllowedFunctions()
	{
		return $this->functions;
	}

	/**
	 * Use smarty tags?
	 *
	 * @return boolean
	 */
	public function useSmartyTags()
	{
		return true;
	}	

	/**
	 * Constructor. Feed in a ClawConfig
	 */	
	public function __construct(ClawConfig $dictionary)
	{
		$this->dictionary = $dictionary;
	}

	/**
	 * Set current language
	 *
	 * @param string $language
	 */
	public function setLanguage($language)
	{
		$this->language = $language;
	}

	/**
	 * Get current language
	 *
	 * @param string $language
	 */	
	public function getLanguage()
	{
		return $this->language;
	}

	/**
	 * Gets appropriate translation 
	 *
	 * Usage:
	 *      {lang->translate word="hello"}
	 *      {lang->translate word="hello" lang="FR"}
	 * 
	 *      or use a shorthand:
	 * 
	 *      {lang->hello}
	 *
	 * Params:
	 *      string  'word'    word to translate
	 *      int     'locale'  optional locale
	 *
	 * @param Array $params Array of Smarty template params
	 * @param Smarty $smarty Smarty template engine
	 * @return string
	 */	
	public function translate($params = array())
	{
		$word = !isset($params['word']) ? null : $params['word'];
		$locale = !isset($params['locale']) ? $this->language : $params['locale'];
		return $this->dictionary->get($locale, $word);
	}

	/**
	 * Magic method to make {lang->word} shorthand possible
	 *
	 * @param string $var which word to get
	 * @return string
	 */
	public function __get($var)
	{
		return $this->translate(array('word' => $var));
	}

	/**
	 * Another way to get a word.
	 *
	 * @param string $meth which word to get
	 * @param Array $args place locale there...
	 * @return string
	 */
	public function __call($meth, $args)
	{
		$arg = is_array($args) ? array_pop($arg) : $this->language;
		return $this->translate(array('word' => $meth, 'locale' => $arg));
	}
}
?>
Return current item: Claw