5
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2009 Yii Software LLC
7
* @copyright Copyright © 2008-2010 Yii Software LLC
8
8
* @license http://www.yiiframework.com/license/
30
30
* When {@link cachingDuration} is set as a positive number, message translations will be cached.
32
* Starting from version 1.0.10, messages for an extension class (e.g. a widget, a module) can be specially managed and used.
33
* In particular, if a message belongs to an extension whose class name is Xyz, then the message category
34
* can be specified in the format of 'Xyz.categoryName'. And the corresponding message file
35
* is assumed to be 'BasePath/messages/LanguageID/categoryName.php', where 'BasePath' refers to
36
* the directory that contains the extension class file. When using Yii::t() to translate an extension message,
37
* we should use: Yii::t('Xyz.categoryName', 'message to be translated').
32
39
* @author Qiang Xue <qiang.xue@gmail.com>
33
* @version $Id: CPhpMessageSource.php 433 2008-12-30 22:59:17Z qiang.xue $
40
* @version $Id: CPhpMessageSource.php 1678 2010-01-07 21:02:00Z qiang.xue $
34
41
* @package system.i18n
45
52
public $cachingDuration=0;
54
* @var string the ID of the cache application component that is used to cache the messages.
55
* Defaults to 'cache' which refers to the primary cache application component.
56
* Set this property to false if you want to disable caching the messages.
59
public $cacheID='cache';
47
61
* @var string the base path for all translated messages. Defaults to null, meaning
48
62
* the "messages" subdirectory of the application directory (e.g. "protected/messages").
66
private $_files=array();
53
69
* Initializes the application component.
54
70
* This method overrides the parent implementation by preprocessing
81
* Determines the message file name based on the given category and language.
82
* If the category name contains a dot, it will be split into the module class name and the category name.
83
* In this case, the message file will be assumed to be located within the 'messages' subdirectory of
84
* the directory containing the module class file.
85
* Otherwise, the message file is assumed to be under the {@link basePath}.
86
* @param string category name
87
* @param string language ID
88
* @return string the message file path
91
protected function getMessageFile($category,$language)
93
if(!isset($this->_files[$category][$language]))
95
if(($pos=strpos($category,'.'))!==false)
97
$moduleClass=substr($category,0,$pos);
98
$moduleCategory=substr($category,$pos+1);
99
$class=new ReflectionClass($moduleClass);
100
$this->_files[$category][$language]=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'messages'.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$moduleCategory.'.php';
103
$this->_files[$category][$language]=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
105
return $this->_files[$category][$language];
65
109
* Loads the message translation for the specified language and category.
66
110
* @param string the message category
67
111
* @param string the target language
70
114
protected function loadMessages($category,$language)
72
$messageFile=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
116
$messageFile=$this->getMessageFile($category,$language);
74
if($this->cachingDuration>0 && ($cache=Yii::app()->getCache())!==null)
118
if($this->cachingDuration>0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
76
120
$key=self::CACHE_KEY_PREFIX . $messageFile;
77
121
if(($data=$cache->get($key))!==false)