* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2009 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CPhpMessageSource represents a message source that stores translated messages in PHP scripts.
*
* CPhpMessageSource uses PHP files and arrays to keep message translations.
*
* When {@link cachingDuration} is set as a positive number, message translations will be cached.
*
* @author Qiang Xue
* @version $Id: CPhpMessageSource.php 433 2008-12-30 22:59:17Z qiang.xue $
* @package system.i18n
* @since 1.0
*/
class CPhpMessageSource extends CMessageSource
{
const CACHE_KEY_PREFIX='Yii.CPhpMessageSource.';
/**
* @var integer the time in seconds that the messages can remain valid in cache.
* Defaults to 0, meaning the caching is disabled.
*/
public $cachingDuration=0;
/**
* @var string the base path for all translated messages. Defaults to null, meaning
* the "messages" subdirectory of the application directory (e.g. "protected/messages").
*/
public $basePath;
/**
* Initializes the application component.
* This method overrides the parent implementation by preprocessing
* the user request data.
*/
public function init()
{
parent::init();
if($this->basePath===null)
$this->basePath=Yii::getPathOfAlias('application.messages');
}
/**
* Loads the message translation for the specified language and category.
* @param string the message category
* @param string the target language
* @return array the loaded messages
*/
protected function loadMessages($category,$language)
{
$messageFile=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
if($this->cachingDuration>0 && ($cache=Yii::app()->getCache())!==null)
{
$key=self::CACHE_KEY_PREFIX . $messageFile;
if(($data=$cache->get($key))!==false)
return unserialize($data);
}
if(is_file($messageFile))
{
$messages=include($messageFile);
if(!is_array($messages))
$messages=array();
if(isset($cache))
{
$dependency=new CFileCacheDependency($messageFile);
$cache->set($key,serialize($messages),$this->cachingDuration,$dependency);
}
return $messages;
}
else
return array();
}
}