Yii 1.1: Multilanguage and SEO friendly



In your application configuration (protected/config/main.php), set the sourceLanguage parameter to English:

return array(
, 'sourceLanguage'=>'en'

In a view

Use the Yii::t() function to translate strings:

echo Yii::t('codexamples_translator','the quick brown fox jumped over the lazy dog');
echo Yii::t('codexamples_translator','you have {count} new emails', array('{count}'=>5));
echo Yii::t('codexamples_translator','n==1#one book|n>1#many books', array(2));

The return value will default to the English value in the quotes, but if the appropriate translation file is found, then the translated value will be returned. Note the use of {parameters}. You can also format dates and numbers:

echo Yii::app()->dateFormatter->formatDateTime(time());
echo Yii::app()->numberFormatter->formatDecimal(3.14);

Add a folder in code directory

Add a folder "fr" under /protected/messages (if not exist, create /protected/messages/ folder), and a file called "codexamples_translator.php". The content of codexamples_translator.php should be this:

return array(
'the quick brown fox jumped over the lazy dog' => 'le renard marron agile saute par dessus le chien  paresseux'
, 'you have {count} new emails' => 'vous avez {count} nouveaux e-mails'
, 'n==1#one book|n>1#many books '=> 'n==1#un livre|n>1#de nombreux livres'

It's an associative array where the first value is the key, and the second value is the translated version. It's easier just to use the English value as the key than to use a numbering system.

Then, you can set the language. This is best done in the beginRequest event handler based on user preferences, but here's how to do it manually:


Here are the results in 'en':

Ouput "en":

the quick brown fox jumped over the lazy dog
you have 5 new emails
many books
Dec 17, 2010 11:27:23 PM

Ouput "fr":

le renard marron agile saute par dessus le chien paresseux
vous avez 5 nouveaux e-mails
de nombreux livres
17 déc. 2010 23:24:12

Support SEO friendly URLs

In your configuration url manager

add <lang:\w+> to each rule, such as:

    'showScriptName' => false,
                '<lang:\w+>/post-<id:\d+>/<title:.*?>' => array('post/view', 'urlSuffix' => '/', 'caseSensitive' => false),

Create frendly url, 2 cases:

Case 1: Simple case

In view, create a link, add lang parameter such as:

//Suppose that $data is post object
echo CHtml::link('post/view',
$model = Post::model()->findByPk(1);
echo CHtml::link('post/view',

Case 2: Better solution

If you create a url in model like this solution: in Post.php model, create function called getUrl()

public function getUrl()
    return Yii::app()->createUrl('post/view', array(
        'id' => $this->id,
        'title' => $this->title,
        'lang' => Yii::app()->language,

Now, in view, you only call (object) $post->url then it will return a friendly url like this:



Conclusion, You can also create views specific to a language. Under protected/views/site/, create a 'fr' folder, and add a page index.php . This will be the French index page for the site, and can be entirely different from the English index page if you desire.

Total 1 comment

#15298 report it
trond at 2013/10/25 04:37am
Great post

I think it is also worth mentioning that the yiic message command is a great tool to create and maintain the translation files in the messages folder.

Leave a comment

Please to leave your comment.

Write new article