Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii: Multi-language website - best practices

Tags:

I find Yii great framework, and the example website created with yiic shell is a good point to start... however it doesn't cover the topic of multi-language websites, unfortunately. The docs covers the topic of translating short messages, but not keeping the multi-lingual content ...

I'm about to start working on a website which needs to be in at least two languages, and I'm wondering what is the best way to keep content for that ... The problem is that the content is mixed extensively with common elements (like embedded video files).

I need to avoid duplicating those commons ... so far I used to have an array of arrays containing texts (usually no more than 1-2 short paragraphs), then the view file was just rendering the text from an array.

Now I'd like to avoid keeping it in arrays (which requires some attention when putting double quotations " " and is inconvenient in general...).

So, what is the best way to keep those short paragraphs? Should I keep them in DB like (id | msg_id | language | content ) and then select them by msg_id & language? That still requires me to create some msg_id's and embed them into view file ...

Is there any recommended paradigm for which Yii has some solutions?

Thanks, m.

like image 277
migajek Avatar asked Mar 23 '10 02:03

migajek


1 Answers

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 sources  TABLE 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'.

like image 169
ianaré Avatar answered Oct 21 '22 13:10

ianaré