Yii Doc:
Modules can be nested in unlimited levels. That is, a module can contain another module which can contain yet another module. We call the former parent module while the latter child module. Child modules must be declared in the modules property of their parent module, like we declare modules in the application configuration shown as above.
I try to create myltilingual application using Yii::t() function:
Yii Doc:
And when using Yii::t() to translate an extension message, the following format开发者_运维百科 should be used, instead:Yii::t('Xyz.categoryName', 'message to be translated')
I translate modules messages with Yii::t('MyModule.source', 'Test');
it works for modules.
Yii::t('MyModule.SubModule.source', 'Test');
The question is:
How to define source path for SubModule in Module when messages stored in:
/protected/modules/MyModule/modules/SubModule/messages/
You are trying to use Yii::t
wrong.
The path for the CPhpMessageSource
(first parameter of Yii::t
) should be the module in which the call to Yii::t
appears. It doesn't matter if that module is aggregated inside another module.
So in your example, you should use
Yii::t('SubModule.source', 'Test');
and place the messages in /protected/modules/SubModule/messages/
.
If you need the message to change based on whether the translation is performed from inside a submodule, add parameters to the message.
It seems that this is not possible to do with CPhpMessageSource , you have to extend and create your own message class .
You have to modify your configuration file i.e. config/main.php as well.
'components'=>array(
...........
'messages' => array(
'class' => 'MyPhpMessageSource',
),
.............
and here is the sample MyPhpMessageSource.php that works for your needs.
class MyPhpMessageSource extends CPhpMessageSource {
private $_files=array();
protected function getMessageFile($category,$language)
{
if(!isset($this->_files[$category][$language]))
{
$parts = explode('.', $category);
$count = count($parts);
if( $count > 1 ) {
$filePath = '';
$moduleClass=$parts[$count-2];
$class=new ReflectionClass($moduleClass);
$filePath .= dirname($class->getFileName()).DIRECTORY_SEPARATOR;
/*
for($i=0; $i<$count-1; $i++) {
$moduleClass=$parts[$i];
if($i == 0) {
$class=new ReflectionClass($moduleClass);
$filePath .= dirname($class->getFileName()).DIRECTORY_SEPARATOR;
} else
$filePath .= $moduleClass.DIRECTORY_SEPARATOR;
}
*/
$filePath .= 'messages' . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $parts[$count-1] . '.php';
echo "$filePath<br/>";
$this->_files[$category][$language] = $filePath;
}
else
$this->_files[$category][$language]=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
}
return $this->_files[$category][$language];
}
}
精彩评论