20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

When translating a message, its category has to be specified since a message may be<br />

translated differently under different categories (contexts). The category yii is reserved for<br />

messages used by the <strong>Yii</strong> framework core code.<br />

Messages can contain parameter placeholders which will be replaced with the actual<br />

parameter values when calling <strong>Yii</strong>::t(). For example, the following message translation<br />

request would replace the {alias} placeholder in the original message with the actual alias<br />

value.<br />

<strong>Yii</strong>::t('app', 'Path alias "{alias}" is redefined.',<br />

array('{alias}'=>$alias))<br />

Note: Messages to be translated must be constant strings. They should not contain<br />

variables that would change message content (e.g. "Invalid {$message} content."). Use<br />

parameter placeholders if a message needs to vary according to some parameters.<br />

Translated messages are stored in a repository called message source. A message source<br />

is represented as an instance of CMessageSource or its child class. When <strong>Yii</strong>::t() is<br />

invoked, it will look for the message in the message source and return its translated<br />

version if it is found.<br />

<strong>Yii</strong> comes with the following types of message sources. You may also extend<br />

CMessageSource to create your own message source type.<br />

• CPhpMessageSource: the message translations are stored as key-value pairs in a<br />

PHP array. The original message is the key and the translated message is the value.<br />

Each array represents the translations for a particular category of messages and is<br />

stored in a separate PHP script file whose name is the category name. The PHP<br />

translation files for the same language are stored under the same directory named as<br />

the locale ID. And all these directories are located under the directory specified by<br />

basePath.<br />

• CGettextMessageSource: the message translations are stored as GNU Gettext files.<br />

• CDbMessageSource: the message translations are stored in database tables. For<br />

more details, see the API <strong>do</strong>cumentation for CDbMessageSource.<br />

A message source is loaded as an application component. <strong>Yii</strong> pre-declares an application<br />

component named messages to store messages that are used in user application. By<br />

default, the type of this message source isCPhpMessageSource and the base path for<br />

storing the PHP translation files is protected/messages.<br />

In summary, in order to use message translation, the following steps are needed:<br />

• Call <strong>Yii</strong>::t() at appropriate places;<br />

• Create PHP translation files as protected/messages/LocaleID/CategoryName.php.<br />

Each file simply returns an array of message translations. Note, this assumes you are<br />

using the default CPhpMessageSource to store the translated messages.<br />

• Configure CApplication::sourceLanguage and CApplication::language.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!