Yii: Multi-language website - best practices Yii: Multi-language website - best practices php php

Yii: Multi-language website - best practices


Gettext is good for its ease of translation, but the default PHP implementation is not thread safe. Yii therefore uses its own unpacker, dramatically increasing processing time compared to php arrays.

Since I was setting up a high volume, high transaction site, the performance hit was not acceptable. Also, by using APC, we could cache the PHP translation further increasing performance.

My approach was therefore to use PHP arrays but to keep the translations in a DB for ease of translation, generating the needed files when translations are changed.

The DB is similar to this :

TABLE Message            // stores source language, updated by script id INT UNSIGNED category VARCHAR(20)         // first argument to Yii::t() key TEXT                     // second argument to Yii::t() occurences TINYINT UNSIGNED  // number of times found in sourcesTABLE MessageTranslation // stores target language, translated by human   id INT UNSIGNED language VARCHAR(3)          // ISO 639-1 or 639-3, as used by Yii messageId INT UNSIGNED       // foreign key on Message table value TEXT version VARCHAR(15) creationTime TIMESTAMP DEFAULT NOW() lastModifiedTime TIMESTAMP DEFAULT NULL lastModifiedUserId INT UNSIGNED

I then modified the CLI tool yiic 'message' command to dump the collected strings into the DB.

http://www.yiiframework.com/wiki/41/how-to-extend-yiic-shell-commands/

Once in the DB, a simple CMS can be setup to provide translators an easy way to translate and at the same time providing versioning information, reverting to older versions, checking quality of translators, etc ...

Another script, also modified from yiic, then takes the DB info and compiles it into PHP arrays. Basically a JOIN of the two tables for each language, then build an array using 'Message'.'key' and 'MessageTranslation'.'value' as (what else?) key => value ... saving to file named from 'Message'.'category' in folder specified by language.

The generated files are loaded as normal by Yii CPhpMessageSource.

For images, this was as simple as placing them in folders with the proper language and getting the app language when linking.

<img src="/images/<?php echo Yii::app()->language; ?>/help_button.png">

Note that in real life, I wrote a little helper method to strip off the country from the language string, 'en_us' should be 'en'.


A Yii application by default uses yii::t() method for translating text messages and there are 3 different types for message sources:

  1. CPhpMessageSource : Translations are stored as key-value pairs in a PHP array.
  2. CGettextMessageSource : Translations are stored as GNU Gettext files. (PO Files)
  3. CDbMessageSource : Message translations are stored in database tables.

If i don't misunderstand, you are using classic arrays for translations. I recommend to you using GetText and PO files with Yii for translation operations.

You can find lot of information about translation and i18n with yii in this official documentation page.


Well I think what is concerned here is how to translate static text/messages on the page and Yii solves it pretty well using Yii:t() and Edigu's answer is for it.

I check out the post on FlexicaCMS about translating dynamic content in database, well ultimately that will be the next after you solve static text/message problem, and that is a truly good approach using Yii's behavior. Not sure if FlexicaCMS authors are too ambitious in supporting translation that way as it would make content translation a worry-free thing - really great.

One thing they don't mention is the url of translated page. For example your.site.com/fr/translated_article_title.html. I mean the url must has /language_id/ part in it so it can help with SEO.