How to use CPhpMessageSource

Hello all,

First off, i like Yii very much and my goal is to learn it and use it in some cool projects,

but, of course, i have more questions then actual knowledge.

Right now i'm stuck at setting language,

how do i use "CPhpMessageSource" ?

In my protected/config/main.php

should i write:



'components'=>array(


...


'messages'=>array(


			'class'=>'CPhpMessageSource',


         'basePath'=>realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'messages/ru'),


			'language'=>'ru',


		),


)


This doesn’t work :(

I'm also wondering, what settings are better for best performance? I mean, should i use /framework/messages/ stuff ? or i can use "custom" messages in webroot/protected/messages?

Are you developing a multilanguage application? If not, you don't need to use this. (If you only want to make those validation messages to appear in Russian, you can simply configure the 'message' option in the rules).

If you ARE developing multilanguage application, you may choose to store translated messages in either PHP array (CPhpMessage) or database (CDbMessageSource). If using the former, you don't need to configure anything since Yii already predefines the 'messages' source. All you need is to put the translated messages under 'protected/messages'.

No, it’s not a multilanguage application… yet  ;)

Quote

you can simply configure the 'message' option in the rules

Can you please tell me how to do that? I'm all over docs, but can't find the right way to do it.

I can simply add 'language' => 'ru' to config/main.php but it'll use /framework/messages files, and they are actually can and will change in further Yii releases, and i don't want new Yii version to overwrite my custom messages.

What i mean is I sure can use 'language' option, but it just seems a bit wrong, and i want to know if it's the right way to do - to edit framework files, or is there a nice way to tell Yii to use my customized and translated messages, those i will store in webapp/protected/

Yes, you also set 'language' property of your application to be 'ru' to make use of the translated messages included in Yii. However, in case a message is not translated in Yii, you still need to do it yourself by explicitly specify the 'message' option. An example is as follows:



class MyModel extends CActiveRecord


{


    public function rules()


    {


         return array(


             array('name', 'required', 'message'=>'This field is required.'),


         );


    }


}


so webapp/protected/messages is reserver for only multilanguage applications?

OK, so i think the best to have custom language is to create one in framework dir, framework/messages/ru_c (russioan, custom) ?

Please, correct me if i'm wrong.

Thank you for you replies!

Yes, protected/messages is only for multilanguage apps. It's not recommended you change code under framework/.

The best way to do is to set language to be 'ru' and specify 'message' option if a message is not translated or not translated as you expected.

Ok, well, you might actually think that i'm a totall idiot, but i still don't get it.

In template i have:



<?php echo CHtml::label('Remember me next time',CHtml::getActiveId($form,'rememberMe')); ?>


in my model (loginfrom):



public function rules()


	{


		return array(


			// username and password are required


			array('username, password', 'required'),





			// password needs to be authenticated


			array('password', 'authenticate'),


		);


		


	}


So, how do i properly change the "Remember me next time" text, without hacking /framework/ stuff? Please, help me.

You can directly change the label text in the view.

As an alternative way, you can use the following view code and model code:

view:



<?php echo CHtml::activeLabel($form, 'rememberMe'); ?>


form:



public function attributeLabels()


{


     return array(


           'rememberMe'=>'Remember me next time',


     );


}


I have a question :

I use ClistView widget and it has a message "Go to page", that is not accessible in widget to modify it.

how can i change it in much project. and other messages like that.

thanks