If you are wondering about using multi-language feature in your Yii application, then you came to the right spot. In this tutorial, I will be showing you how to implement the multi-language using Message Translation feature in Yii.
In Yii, Message translation is done by calling Yii::t(). This method translates the given message from source language to target language. Also, when translating a message, its category has to be specified since a message may be translated differently under different contexts.
Message translation could be achieved using any three of the message sources.
In this tutorial, I will be focusing on CDbMessageSource. CDbMessageSource represents a message source that stores translated messages in database. Let us begin now.
Since we are using database to store the message, we need two tables; one is to store the Source Message and the other to store the Translated Message. Here is the schema for it.[http://www.yiiframework.com/doc/api/1.1/CDbMessageSource]
CREATE TABLE SourceMessage ( id INTEGER PRIMARY KEY, category VARCHAR(32), message TEXT ); CREATE TABLE Message ( id INTEGER, language VARCHAR(16), translation TEXT, PRIMARY KEY (id, language), CONSTRAINT FK_Message_SourceMessage FOREIGN KEY (id) REFERENCES SourceMessage (id) ON DELETE CASCADE ON UPDATE RESTRICT );
The ‘SourceMessage’ table stores the messages to be translated, and the ‘Message’ table stores the translated messages.