yii guide

Page 194

180

8. Special Topics

When translating a message, its category has to be specified since a message may be translated differently under different categories (contexts). The category yii is reserved for messages used by the Yii framework core code. Messages can contain parameter placeholders which will be replaced with the actual parameter values when calling Yii::t(). For example, the following message translation request would replace the {alias} placeholder in the original message with the actual alias value. Yii::t(’app’, ’Path alias "{alias}" is redefined.’, array(’{alias}’=>$alias))

Note: Messages to be translated must be constant strings. They should not contain variables that would change message content (e.g. "Invalid {$message} content. "). Use parameter placeholders if a message needs to vary according to some parameters.

Translated messages are stored in a repository called message source. A message source is represented as an instance of CMessageSource or its child class. When Yii::t() is invoked, it will look for the message in the message source and return its translated version if it is found. Yii comes with the following types of message sources. You may also extend CMessageSource to create your own message source type. • CPhpMessageSource: the message translations are stored as key-value pairs in a PHP array. The original message is the key and the translated message is the value. Each array represents the translations for a particular category of messages and is stored in a separate PHP script file whose name is the category name. The PHP translation files for the same language are stored under the same directory named as the locale ID. And all these directories are located under the directory specified by basePath. • CGettextMessageSource: the message translations are stored as GNU Gettext files. • CDbMessageSource: the message translations are stored in database tables. For more details, see the API documentation for CDbMessageSource. A message source is loaded as an application component. Yii pre-declares an application component named messages to store messages that are used in user application. By default,


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.