Quick Start to Internationalize your application in Yii Framework

As the title mentioned here is a quick start to internationalize your application. For more details you can read on the Yii API Documentation, what’s all about Yii::t() function:

Set your targeted application language:

In the main application configuration file which usually located at protected/config/main.php, set this:

<?php


return array(


   'language' => 'en', // the language that the user is using and the application should be targeted to


);

Create a folder named messages in your protected folder:

Quote

protected/messages

Within the messages folder, create folders to store your language files by naming the folder with the language code (en, de, id):

Quote

protected/messages/en

protected/messages/id

protected/messages/de

Now, before continue, we must understand the minimum params for Yii::t() method that actually translates the language. here is the minimum format:

<?php


   Yii::t('param1', 'param2');

Explanation on what the params mean: param1 is the category, refers to the filename where the translations are stored. param2 is the string constant to be translated.

Within your view for example:

<?php


   echo Yii::t('contentForm', 'LABELSELECTTITLE');

In order for that to work, you need to create a new php file named contentForm.php under the protected/messages/[your language]/contentForm.php.

In your protected/messages/en/contentForm.php:

<?php


return array (


     'LABELSELECTTITLE' => 'Select Title'


);

And in your protected/messages/id/contentForm.php for example:

<?php


return array (


     'LABELSELECTTITLE' => 'Pilih Judul'


);

And that's all it has to be done. Congrats, your application is now just internationalized.

Is this the only way to internationalize my application?

For blocks of text, translated views would be better.

Create view subdirectories for user languages (Yii::app()->language) different from the application language (Yii::app()->sourceLanguage).

For the site views in protected/views/site that would become protected/views/site/<your language>.

Save the translated views to these new directories. If a translated file is missing, the untranslated version will be used, that is, you have the choice to translate views selectively.

Edit: For attributelabels you don’t need to add Yii:t() calls to the views, you can place them in the model similar to this:




public function attributeLabels() {

  return array(

    'attribute1'      =>  Yii::t('tri', 'source language label1'),

    'attribute2'      =>  Yii::t('tri', 'source language label2'),

    ...

  }



or, like I did a while ago, always have the attributes translated.

Default source language is ‘en_us’, which I don’t use. I select ‘en’ or ‘sv’).




public function attributeLabels() {

  return array(

    'attribute1'      =>  Yii::t('tri', 'attribute1'),

    'attribute2'      =>  Yii::t('tri', 'attribute2'),

    ...

  }



Read more here:

http://www.yiiframework.com/doc/guide/topics.i18n

http://www.yiiframework.com/doc/cookbook/26/

/Tommy

Just curious about your question though, why would you need another way to internationalize your application ?

I’d like to use data base instead of files, so it will be easy to edit by admin without a programer help. Is it possible?

See CDbMessageSource. Also CApplication->messages.

I’d like to use poedit files… same reason.

Thank you all. It is clear for me now.

In app config, do the following:




'components'=>array(

    'messages'=>array(

        'class'=>'CGettextMessageSource',

        //...other initial property values for this class..check API...

    ),

),