Can we do the language translation in Yii ? The answer is YES, we can do it by using message and view translation.

Here is the way to do the yii multi language

Scenarios: I got “Hello World”, and now I want to translate it to Vietnamese, French, Korean and Japanese.
First of all, I got to make sure that my default website language is English, go to config file and add following code:
protected/config/main.php
'sourceLanguage'=>'en',

Just put it in anywhere you like in the return array

As you may know, yii read the language in message folder, so I go to protected/messages and create folders name: vi (Vietnamese), fr (French), kr (Korean), jp (Japanese).

In each folder, I create a php file name “ dukaweb_translator.php “; And in each file, I put the translation of “Hello World” in different language.
vi/dukaweb_translator.php.
<?php
return array(
  'Hello World' => 'Xin chào thế giới'
);

kr/dukaweb_translator.php

<?php
return array(
  'Hello World' => '안녕하세요'
);

jp/dukaweb_translator.php

<?php
return array(
  'Hello World' => 'ハローワールド'
);

I don’t know it correct or not. I use google translate.

So, the final step, in views file, I use yii translator to display “Hello world” in each language.
index.php

<?php
  //Default English
  echo '<p>'.Yii::t('dukaweb_translator','Hello World').'</p>';

  //Vietnamese
  Yii::app()->language='vi';
  echo '<p>'.Yii::t('dukaweb_translator', 'Hello World').'</p>';

  //Korean
  Yii::app()->language='kr';
  echo '<p>'.Yii::t('dukaweb_translator', 'Hello World').'</p>';

  //Japanese
  Yii::app()->language='jp';
  echo '<p>'.Yii::t('dukaweb_translator', 'Hello World').'</p>';

  //French
  Yii::app()->language='fr';
  echo '<p>'.Yii::t('dukaweb_translator', 'Hello World').'</p>';
?>

So the output looks like

Fmi, if you wanna set default language is french, simple go to config change sourceLanguage to fr .
The Yii::app()->language=’vi’ is for you to manually translate language everywhere you want.