Social Icons

Pages

Friday, November 16, 2012

Yii internationalization (i18n)


below example, the default language English (en), and we will add some French (fr) components.

1. application configuration (protected/config/main.php), set the sourceLanguage parameter to English(en):

return array(
...
, 'sourceLanguage'=>'en'
...
)


2. In  view, use the Yii::t() function to translate strings:

echo Yii::t('filename','this is fine tutorial for yii internationalization').'<br/>';

echo Yii::t('filename','you have {count} new emails', array('{count}'=>5)).'<br/>';

3. Add a folder "fr" under /protected/messages, and a file called "filename.php". The content of filename.php should be this:


<?php

return array(
'this is fine tutorial for yii internationalization' => 'ce n\'est qu\'un tutoriel très bien pour yu internationalisation'
, 'you have {count} new emails' => 'vous avez {count} nouveaux e-mails'

);

?> 


Then, you can set the language. This is best done in the beginRequest event handler based on user preferences, but here's how to do it manually:

Yii::app()->language='fr';






No comments:

Post a Comment