i18n - how to get it to work

I have read the guide several times:

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

There is a "hint" for getting the language like the user preferres. But I cannot figure out what to do exactly to get translation working.

In my index.php :


$app = Yii::createWebApplication($config);

$app->setLanguage(Yii::app()->request->preferredLanguage);

$app->run();



But nothing happening. The "previous" and "next"-links in the pager are still in english.

What do I have to do exactly to get i18n work?

Thanks for any help!

I assume you are inquiring specifically about the translation features of i18n? Translations have to be done explicitly…no real magic is available to just automatically translate copy in the application from one language to another. Yii comes with locale data for nearly all languages and regions…but this data is to help assist in things like currency and numeric symbols; number, date, and time names and formats; etc.

Yii provides for both message translation and file translation. The former is for translating a single message, the latter for translating entire view files.

To translate a simple snippet of copy that is being displayed in the application from one language to another, you typically have to explicitly invoke the Yii::t() method (for "t"ranslation).

Assuming the use of the default CPhpMessageSource as the implementation to house message translations, and assuming you want to translate to a language called Mantus to be identified by a locale/language ID of ‘man’ within a category called, ‘default’, you could take the following steps:

  1. create a new file that will house your message translations: /protected/messages/man/default.php

  2. Add to that file your desired translations:


<?php

     return array(

          'Source Language Message' => 'Target Language Message (i.e. the Mantus translation)',

     );

  1. Set the application target language to be Mantus (again identified by ‘man’):

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

  1. Make your calls to the t() method

echo Yii::t('default', 'Source Language Message');

should display "Target Language Message (i.e. the Mantus translation)"

Hope this helps outlines how to begin to play with the message translation features in Yii.

Seems the topic quite similar with mine, so I’ll ask at here too… after read Jeff’s post. I’ve tried to translate my web to Indonesia Languange (id) and Chinese Traditional (zh_tw). Something very strange though…

if I set language=>‘zh_tw’ it successfully translate to chinese character. But when I set the language=>‘id’, it won’t translate.

Later, I try to change the Indonesia folder name from ‘id’ to ‘idn’. And then set ‘language’=>‘idn’. And it work! I just don’t understand. Why if I use ‘id’ it won’t work? My friend use ‘id’ and got no problem at all… But why mine do?

aah I know why…

that’s because I’ve accidentally put language in here


'language'=>'id',

'components'=>array(

		....

                 'coreMessages'=>array(

                         'basePath'=>'protected/messages',

                          'language'=>'id'//<== culprit

                ),

So embrassing :-[

Ok. Thanks a lot for making my belief stronger. Thanks for the detailed explanation, jefftulsa. I understand the custom part of translation. My question was why the yii-elements didn’t translate. With your help I found out now: yii has no “de_de” files! Only “de”. So this is false:


$app = Yii::createWebApplication($config);

$app->setLanguage(Yii::app()->request->preferredLanguage);

$app->run();



This works:


$app = Yii::createWebApplication($config);

$languageParts = explode('_', Yii::app()->request->preferredLanguage);

$app->setLanguage($languageParts[0]);

$app->run();



What I don’t understand is: how can I customize the yii-translations without editing the files directly under framework? I’ve tried to copy the files to my messages folder, then setting the path like this:




$i18nDataPath=dirname(__FILE__).'/protected/messages';

$app = Yii::createWebApplication($config);

$languageParts = explode('_', Yii::app()->request->preferredLanguage);

$app->setLocaleDataPath($i18nDataPath);

$app->setLanguage($languageParts[0]);

$app->run();



But that does not work. Yii always seems to take the messages from its own folder. Is there a way? Any ideas?

See here:

http://www.yiiframework.com/forum/index.php?/topic/10823-change-default-yii-messages/page__view__findpost__p__53743

Thank you! Works like a charm.