You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
I needed the all messages with their translation into javascript. This is my solution:
- Extend CPhpMessageSource, creating MPhpMessageSource into protected/components :
class MPhpMessageSource extends CPhpMessageSource
{
public function init()
{
parent::init();
}
public function getLoadedMessages($category = 'core')
{
return $this->loadMessages($category, Yii::app()->language);
}
}
- You add this to protected/config/main.php into components array:
'messages' => array(
'class' => 'APhpMessageSource'
),
- You add this code into your view:
<script type="text/javascript">
var messages = <?php echo CJSON::encode(Yii::app()->messages->getLoadedMessages()) ?>;
function t(key) {
return (messages[key]) ? messages[key] : key;
}
//utilization
alert(t('Message'));
</script>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.