i18n - mymagic/open_hub GitHub Wiki

Yii has good support on multilingual by default. It should be seen at 3 level: database, interface, asset (like images etc).

User selected language

Use Yii::app()->language to get user selected language

Setting

First of all, you need to configure your app to support multilingual. Open protected/config/params.php and you can see the following lines:

'languages'=>array('en'=>'English', 'ms'=>'Bahasa', 'zh'=>'中文'),
'frontendLanguages'=>array('en'=>'English', 'ms'=>'Bahasa'),
'backendLanguages'=>array('en'=>'English', 'ms'=>'Bahasa'), 
  • languages are the group of language that supported system wide, think in database perspective
  • frontendLanguages are the language selection you like to make available at frontend for visitors
  • backendLanguages should similar to languages, but in case you have more supported languages in database but choose not to expose all of them to admin, you can set it here

Database

Open Hub use multiple columns method to support i18n in database. The pro of doing it is easier SQL query, while the cons is database structure modification is required.

For example, you may started with the system with column title in resource table. Then you decided to make it multilingual. You will need to rename title to title_en, then add few more columns depending on how many other language you like to support. eg: title_ms and title_zh

Then, you need to regenerate your model, controller and view.

More info can be found here on Naming Convention for i18n database column.

Retrieve value

ActiveRecord provides a function getAttrData($attribute, $lang = '') to easily retrieve multilingual value. For example, a model has multilingual attribute title_en and to retrieve it, use:

$model->getAttrData('title')

System will automatically use user's currently selected language if not specified.

Interface

To turn a text into multilingual form, for example 'Hello World', change it to <?php echo Yii::t('app', 'Hello World'). The first parameter is the grouping, and the second is the string to translate. Refer to Yii documentation for detail.

For module, it's advised to use the module code (eg: boilerplateStart) as the grouping.

Then, you will need to generate the php translation files stored at /protected/messages/[languageCode]:

  1. locate to magic_docker7 in terminal
  2. ./cmd.sh
  3. cd magic_hub/protected
  4. php yiic message config/message.php, run this command to generate the message php across the system
  5. Open generated php file and translate it, or do it thru backend Site\Lingual

Bigger chunk text

The above method works perfect for one or two line of text, however, when the entire file have paragraphs of text needed for translation, then a different approach should be use: PartialView

  1. create a subfolder named i18n in your views
  2. lets say the original view file is faq.php, and you need 2 languages (en & ms)
  3. in the i18n, create file:
  • i18n/_faq-en.php
  • i18n/_faq-ms.php
  1. edit faq.php to include the correct view base on user selected language
<?php $this->renderPartial(sprintf('i18n/_faq-%s', Yii::app()->language)); ?> 

Lingual functions at Backend

Lingual functions at Backend can be accessible from Site -> Lingual.

Translation can be done on the fly here. Changes submitted will be save to protected/messages directory.

Either PHP setting, Apache Security, Cloudflare, firewall or other form of security check may block you from submitting changes of the translation due the the large input size.