Translate messages inside a module

Hey, guys!

I’m trying to store messages belonging to a module inside the module itself, but when I call Yii::t() the messages doesn’t get translated.

What I’m doing wrong?

I have a module called guestbook and inside guestbook there is the following structure:

messages

|-> pt_br

   |->att.php    //this file contains all attribute labels for my models inside guestbook module

so, in the model guestbook.models.Guestbook.php:




public function attributeLabels(){

    return array(

       'guestbookTitle' => Yii::t('guestbook.att', 'Guestbook Title'),

       'guestbookText' => Yii::t('guestbook.att', 'Guestbook Text'),




    );

}



If I put the messages under application.messages and call Yii::t(‘att’,‘message’) then things work fine.

Could anybody please give a hint?

Thanks

Regards!!

:)

Try:




Yii::t('guestbookModule.att', 'title');



Also you can add a static function “t” into the module class, so you can simply write guestbookModule::t(‘title’).

Comment removed.

I see many modules that do this… but actually it can be a bit annoying for extracting all messages through the shell. As you need to to hunt down all 3rd party custom t functions.

and_s: thank you very much, mate! Never thought that I should insert the "Module" word in the context. Some things in Yii are a bit weird…hehe

mech7: you’re right, extracting through shell would be a little painful in this case, i’ll take this into account. Thanks for advising!

regards!!

:)

I agree it is not very obvious, but docs say:

So you have to use a full class name, which includes "Module" suffix.

yes, however, once we have one alias for each module, i thought Yii would recognize it

but no problem, one more word will not kill me…heheh

thanks, man!

You can register different messagesources in yii and so you can register your module-messages too:

1. you have to register your messagesource from the module in config/main.php

or in your module->init()

Example with the file-structure:

modules/mymodule/messages/mycategory.php

register the messagesource in config/main.php




'components'=>array(

...

array('mymoduleMessages' => array(

	    	    'class'=>'CPhpMessageSource',

	    	    'basePath'=>'protected/modules/mymodule/messages',

	        )));

...

)



or you can register in class mymodule




public function init()

    {

        ....    	

    	//register translation messages from module dbadmin

    	//so no need do add to config/main.php

    	Yii::app()->setComponents(

            array('mymoduleMessages' => array(

	    	    'class'=>'CPhpMessageSource',

	    	    'basePath'=>'protected/modules/mymodule/messages',

	        )));

    }



2. you have to specify the source when calling Yii::t

Yii::t(‘mycategory’, $message,$params,‘mymoduleMessages’,$language);

you can add a static method "t" to your module to simplify the call




public static function t($message, $plural = 1, 

$category='mycategory',$source='mymoduleMessages',$language=NULL) {

  $params = array($plural); //see plural messages

  return Yii::t($category, $message,$params,'mymoduleMessages',$language); 

}



Hi all,

you can get the messages using the normal syntax by registering ‘messages’ instead of ‘mymoduleMessages’:




public function init()

    {

        ....    	

    	//register translation messages from module dbadmin

    	//so no need do add to config/main.php

    	Yii::app()->setComponents(

            array('messages' => array(

	    	    'class'=>'CPhpMessageSource',

	    	    'basePath'=>'protected/modules/mymodule/messages',

	        )));

    }



great article!