How to customize Yii core messages?

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.

next (#2) »

Yii core messages refer to static text strings in the core Yii framework code which are meant to be displayed to end-users (e.g. core exception messages, default validation error messages). Customization of these core messages is needed in two circumstances:

  • When an application is written for non-English users, these core messages need to be translated and the framework does not have the required translation.
  • Some core messages need to be modified slightly for various reasons. For example, the messages are too technical; the messages are inappropriate in certain scenarios; the messages contain syntax errors. In the last example, bugs should be reported, but not every application can wait till the bugs are fixed.

In this article, we introduce a technique to customize the core messages in a systematic way. If you are only interested in customizing a few validation error messages, you may refer to the article "How to customize the error message of a validation rule".

When Yii displays a core message, it actually undergoes an implicit translation process with the help of an application component named "[coreMessages|CApplication::coreMessages]". The component translates the core message into the [target language|CApplication::language] which is displayed ultimately. If a translation cannot be found, the original core message will be displayed, instead.

The idea here is to customize the coreMessages component by changing the place where it looks for translated messages. We can do so by configuring the component with the following application configuration:

return array(
	......
	'language'=>'de',
	'components'=>array(
		'coreMessages'=>array(
			'basePath'=>Yii::getPathOfAlias('application.messages'),
		),
		......
	),
);

In the above, we specify that the application is targeted to German users and the translated messages are located under the directory aliased as application.messages. In a default setting, the path alias would refer to the directory protected/messages.

Next, we need to provide our translations. Under the directory protected/messages, create a subdirectory named de which corresponds to the target language we set in the application configuration. And under the de directory, create a new file named yii.php. To this end, we should have the following directory structure:

WebRoot/
	protected/
		messages/
			de/
				yii.php
		controllers/
		views/
		......

Finally, we put message translations in the yii.php file. To save time, we may simply copy the content from framework/messages/de/yii.php and modify it as needed.

Tip: You may wonder why we would take so much trouble in order to customize the core messages. Why don't we modify the file framework/messages/de/yii.php directly? The answer is that you should never modify any core framework file. If you do that, you will face the danger that a future upgrade of the framework may overwrite your change.

17 0
17 followers
Viewed: 55 004 times
Version: Unknown (update)
Category: Tutorials
Written by: qiang
Last updated by: Yang He
Created on: Feb 25, 2009
Last updated: 5 years ago
Update Article

Revisions

View all history

Related Articles