Difference between #2 and #1 of Entire Messages into javascript object

unchanged
Title
Entire Messages into javascript object
unchanged
Category
How-tos
unchanged
Tags
javascript, messages, i18n
changed
Content
I needed the all messages with their translation into javascript. 
This is my solution:

1.1- Extend CPhpMessageSource, creating MPhpMessageSource
into protected/components :
~~~
[php]
class MPhpMessageSource extends CPhpMessageSource
{
    public function init()
    {
		parent::init();
    }

    public function getLoadedMessages($category = 'core') 
    {
        return $this->loadMessages($category, Yii::app()->language);
    }
}
~~~
2.2- You add this to protected/config/main.php into
components array:

~~~
[php]
'messages' => array(
            'class' => 'APhpMessageSource'
        ),
~~~

3.3- You add this code into your view:


~~~
[php]
<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>
~~~