- Getting Started
- Fundamentals
- Working with Forms
- Working with Databases
- Caching
- Extending Yii
- Testing
- Special Topics
Internationalization (I18N) refers to the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. For Web applications, this is of particular importance because the potential users may be from worldwide.
Yii provides support for I18N in several aspects.
In the following subsections, we will elaborate each of the above aspects.
Locale is a set of parameters that defines the user's language, country
and any special variant preferences that the user wants to see in their
user interface. It is usually identified by an ID consisting of a language
ID and a region ID. For example, the ID en_US stands for the locale of
English and United States. For consistency, all locale IDs in Yii are
canonicalized to the format of LanguageID or LanguageID_RegionID
in lower case (e.g. en, en_us).
Locale data is represented as a CLocale instance. It provides locale-dependent information, including currency symbols, number symbols, currency formats, number formats, date and time formats, and date-related names. Since the language information is already implied in the locale ID, it is not provided by CLocale. For the same reason, we often interchangeably using the term locale and language.
Given a locale ID, one can get the corresponding CLocale instance by
CLocale::getInstance($localeID) or CApplication::getLocale($localeID).
Info: Yii comes with locale data for nearly every language and region. The data is obtained from Common Locale Data Repository (CLDR). For each locale, only a subset of the CLDR data is provided as the original data contains a lot of rarely used information. Starting from version 1.1.0, users can also supply their own customized locale data. To do so, configure the CApplication::localeDataPath property with the directory that contains the customized locale data. Please refer to the locale data files under
framework/i18n/datain order to create customized locale data files.
For a Yii application, we differentiate its target language from source language. The target language is the language (locale) of the users that the application is targeted at, while the source language refers to the language (locale) that the application source files are written in. Internationalization occurs only when the two languages are different.
One can configure target language in the application configuration, or change it dynamically before any internationalization occurs.
Tip: Sometimes, we may want to set the target language as the language preferred by a user (specified in user's browser preference). To do so, we can retrieve the user preferred language ID using CHttpRequest::preferredLanguage.
The most needed I18N feature is perhaps translation, including message translation and view translation. The former translates a text message to the desired language, while the latter translates a whole file to the desired language.
A translation request consists of the object to be translated, the source language that the object is in, and the target language that the object needs to be translated to. In Yii, the source language is default to the application source language while the target language is default to the application language. If the source and target languages are the same, translation will not occur.
Message translation is done by calling Yii::t(). The method translates the given message from source language to target language.
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, the type of this
message source is CPhpMessageSource and the base path for storing the PHP
translation files is protected/messages.
In summary, in order to use message translation, the following steps are needed:
Call Yii::t() at appropriate places;
Create PHP translation files as
protected/messages/LocaleID/CategoryName.php. Each file simply returns an
array of message translations. Note, this assumes you are using the default
CPhpMessageSource to store the translated messages.
Configure CApplication::sourceLanguage and CApplication::language.
Tip: The
yiictool in Yii can be used to manage message translations when CPhpMessageSource is used as the message source. Itsmessagecommand can automatically extract messages to be translated from selected source files and merge them with existing translations if necessary.
Starting from version 1.0.10, when using CPhpMessageSource to manage message source,
messages for an extension class (e.g. a widget, a module) can be specially managed and used. In particular, if a message
belongs to an extension whose class name is Xyz, then the message category can be specified
in the format of Xyz.categoryName. The corresponding message file will be assumed to be
BasePath/messages/LanguageID/categoryName.php, where BasePath refers to
the directory that contains the extension class file. And when using Yii::t() to
translate an extension message, the following format should be used, instead:
Yii::t('Xyz.categoryName', 'message to be translated')
Since version 1.0.2, Yii has added the support for choice format. Choice format refers to choosing a translated according to a given number value. For example, in English the word 'book' may either take a singular form or a plural form depending on the number of books, while in other languages, the word may not have different form (such as Chinese) or may have more complex plural form rules (such as Russian). Choice format solves this problem in a simple yet effective way.
To use choice format, a translated message must consist of a sequence of
expression-message pairs separated by |, as shown below:
'expr1#message1|expr2#message2|expr3#message3'
where exprN refers to a valid PHP expression which evaluates to a boolean value
indicating whether the corresponding message should be returned. Only the message
corresponding to the first expression that evaluates to true will be returned.
An expression can contain a special variable named n (note, it is not $n)
which will take the number value passed as the first message parameter. For example,
assuming a translated message is:
'n==1#one book|n>1#many books'
and we are passing a number value 2 in the message parameter array when
calling Yii::t(), we would obtain many books as the final
translated message.
As a shortcut notation, if an expression is a number, it will be treated as
n==Number. Therefore, the above translated message can be also be written as:
'1#one book|n>1#many books'
File translation is accomplished by calling
CApplication::findLocalizedFile(). Given the path of a file to be
translated, the method will look for a file with the same name under the
LocaleID subdirectory. If found, the file path will be returned;
otherwise, the original file path will be returned.
File translation is mainly used when rendering a view. When calling one of
the render methods in a controller or widget, the view files will be
translated automatically. For example, if the target
language is zh_cn while the source
language is en_us, rendering a view named
edit would resulting in searching for the view file
protected/views/ControllerID/zh_cn/edit.php. If the file is found, this
translated version will be used for rendering; otherwise, the file
protected/views/ControllerID/edit.php will be rendered instead.
File translation may also be used for other purposes, for example, displaying a translated image or loading a locale-dependent data file.
Date and time are often in different formats in different countries or regions. The task of date and time formatting is thus to generate a date or time string that fits for the specified locale. Yii provides CDateFormatter for this purpose.
Each CDateFormatter instance is associated with a target locale. To get the formatter associated with the target locale of the whole application, we can simply access the dateFormatter property of the application.
The CDateFormatter class mainly provides two methods to format a UNIX timestamp.
format: this method formats the given UNIX
timestamp into a string according to a customized pattern (e.g.
$dateFormatter->format('yyyy-MM-dd',$timestamp)).
formatDateTime: this method formats
the given UNIX timestamp into a string according to a pattern predefined in
the target locale data (e.g. short format of date, long format of
time).
Like data and time, numbers may also be formatted differently in different countries or regions. Number formatting includes decimal formatting, currency formatting and percentage formatting. Yii provides CNumberFormatter for these tasks.
To get the number formatter associated with the target locale of the whole application, we can access the numberFormatter property of the application.
The following methods are provided by CNumberFormatter to format an integer or double value.
format: this method formats the given
number into a string according to a customized pattern (e.g.
$numberFormatter->format('#,##0.00',$number)).
formatDecimal: this method formats the given number using the decimal pattern predefined in the target locale data.
formatCurrency: this method formats the given number and currency code using the currency pattern predefined in the target locale data.
formatPercentage: this method formats the given number using the percentage pattern predefined in the target locale data.
I have been trying and trying but i got no idea how it works. I believe example is the best and easiest way to understand. Below are those created files:
/protected/config/main.php
'urlManager'=>array(
'class'=>'application.components.MyCUrlManager',
'urlFormat'=>'path',
'rules'=>array(
'<language:(en_us|zh_cn)>/<route:\w\/+>' => ''
),
),
/protected/components/LangBox.php
<?php
class LangBox extends CWidget
{
public function run()
{
$currentLang = Yii::app()->language;
$this->render('langBox', array('currentLang' => $currentLang));
}
}
?>
/protected/components/views/langBox.php
<?php echo CHtml::form(); ?>
<?php
echo CHtml::dropDownList('lang', $currentLang,
array('en_us' => 'English', 'zh_cn' => 'Chinese'),
array('submit' => ''));
?>
/protected/components/MyCUrlManager.php
<?php
class MyCUrlManager extends CUrlManager
{
public function createUrl($route,$params=array(),$ampersand='&')
{
if (!isset($params['language']))
$params['language']=Yii::app()->language;
return parent::createUrl($route, $params, $ampersand);
}
}
?>
/protected/components/MyController.php
<?php
class MyController extends CController
{
function init()
{
parent::init();
$app = Yii::app();
if (isset($_POST['_lang']))
{
$app->language = $_POST['_lang'];
$app->session['_lang'] = $app->language;
}
else if (isset($app->session['_lang']))
{
$app->language = $app->session['_lang'];
}
}
}
?>
Now i got stuck here, how could i proceed with the multi-language support?
eg. I want to eat apple. --> I want to 吃 apple.
I do not know what to put in:-
/protected/messages/en_us/index.php
/protected/messages/zh_cn/index.php
/protected/views/site/index.php
Or am i missing something besides above?
Please advise.
Sincerely,
Kenny
How to store translations in DB: http://www.yiiframework.com/forum/index.php?topic=402.0
To get Poedit working, you need to modify its keywords (in the menu: "Catalog" --> "Settings", then the tab "Keywords"). For matching yiis translation method, I entered "t:1c,2".
I understand perfectly all these concepts. But I don't know where these things fit into YII. Don't know if this user-provided example is any good or applies to me. I need sample code of EXACTLY STEP BY STEP like "put blablabla in /protected/configs/main.php" like in the blog guide. So I can see EXACTLY. If you want me understand how machine works don't describe about all the different parts, point me WHERE it is in the machine, say tweak this, and then I can SEE how all the other parts are affected. I don't know WHERE to do this stuff that you are talking about.
Could anyone lend the benefit of their experience for what sort of categories are important on a multi-lingual, social website?
I'm guessing in the dark as to whether I should base the categories solely on grammatical features (e.g. female-plural-adj.) Are other sorts of context important to include?
The examples provided in this guide do not fit the whole picture. Please, give us an application example.
I created a /modules/news/messages/nl/articles.php category file, and called: Yii::t('newsModule.articles','message'); but it just doesnt work.
I'm new to Yiii, so read at your own risk.
Here are the steps I took to get i18n working
1) if your source language is not 'en_us' then in your config file add this line in the return array: 'sourceLanguage'=>'es_en', // or whatever the locale
2) You also need to specify the target language. This also can be specified in the config file 'language'=>'es_us', // again, whatever the locale
3) Now create your translation file. In the /protected/messages directory you need to create another directory for each locale you will have translations for. (NOTICE:: Yii has translations for some of it's content. If you want to take advantage of these translations, you need to use a locale that coincides with the locales they used. For example I initially set up a directory of es_us for us Spanish. But then system messages were not working because Yii uses es.)
So, in my case, I created a file in /protected/messages/es/primary.php the name of the file is important as you will need to reference it when you call Yii::t('primary','Hello World');
The contents of the translation file look like this: <? return array( 'Hello World'=> 'Hola Mundo', ); ?> (NOTICE: Save your file as utf-8 and not ascii. Otherwise, your translations will show up funny.
4) Now call <?=Yii::t('primary','Hello World');?> wherever you need to.
One last note you can find Yii's locales in framework/messages.
Hope this helps.
If you need to translate inside your module, just follow the steps (example uses module named Dictionary): 1. Ensure your module class name, like: DictionaryModule 2. Edit main config /protected/config/main.php - add sourceLanguage and language, ie: 'sourceLanguage' => 'en_us', 'language' => 'pl_pl', 3. Inside module folder (protected/modules/dictionary) create file: messages/pl_pl/translation.php 4. Edit translation.php and create array like: return array ( 'it works' => 'To dziala', ); 5. In your module view code just use: <?php echo Yii::t('DictionaryModule.translation','it works') ?>
If you want to use translation, but not inside module, just follow the steps: 1. Edit main config /protected/config/main.php - add sourceLanguage and language, ie: 'sourceLanguage' => 'en_us', 'language' => 'pl_pl', 2. Inside application folder (protected) create file: messages/pl_pl/translation.php 3. Edit translation.php and create array like: return array ( 'it works too' => 'To tez dziala', ); 4. In your module code just use: <?php echo Yii::t('translation','it works too') ?>
Enjoy :) Aleksy Goroszko
I see I need to format message a bit :)
If you need to translate inside your module, just follow the steps (example uses module named Dictionary):
'sourceLanguage' => 'en_us',
'language' => 'pl_pl',
return array (
'it works' => 'To dziala',
);
<?php echo Yii::t('DictionaryModule.translation','it works') ?>
If you want to use translation, but not inside module, just follow the steps:
'sourceLanguage' => 'en_us',
'language' => 'pl_pl',
return array (
'it works too' => 'To tez dziala',
);
<?php echo Yii::t('translation','it works too') ?>
Enjoy :)
Aleksy Goroszko
Poedit is a very convenient tool for editing PO files. It can search the source code for the messages to be translated, it groups out the untranslated messages, remembers messages that had been used before but then removed, and it can guess the translation based on similar messages.
I was very eager to use it with Yii, however it turns out that it does not support message context (yet). It requires that the message is the first parameter of the translation function, but for Yii the first parameter is the context. Is there a solution you could suggest for me?
I could still use Poedit if I used the same context in Yii for all messages, but then Yii should take the message as the first parameter and the context as the second, which would be ignored by Poedit.
Or maybe you could suggest another (but as much convenient) PO editor altogether?
Thank you!