How to have truly multilingual URLs

You are viewing revision #31 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#30)

Introduction

We can easily have a multilingual site if we put a "lang" parameter that comes via GET. For example:

<?php 
echo CHtml::link('Downloads', array('/site/downloads', 'lang' => 'en')); 
echo CHtml::link('Descargas', array('/site/downloads', 'lang' => 'es'));
?>

Then, in Controller.php, we need to put this piece of code:

public function beforeAction($action)
{
      if(isset($_GET['lang']))
      {
	      Yii::app()->setLanguage($_GET['lang']);
      }
      else
      {
	      Yii::app()->setLanguage('en');
      }
      
      return true;
}

Finally we can add the following rules:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
	    '<lang:\w+>'=>'site/index',
	    '<lang:\w+>/<action>' => 'site/<action>',
     )
),

The problem

This leads to URLs like these:

  • www.oligalma.com/en/downloads (English)
  • www.oligalma.com/es/downloads (Spanish)

This is fine to have a multilingual site. But, we can go one step further. What if we want URLs like these:

That is, every URL has its particular path. It changes the whole URL, not just the two initial letters (en/es).

The solution

'urlManager'=>array(
    'matchValue'=>true,
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
		'<lang:\w+>'=>'site/index',
		'<lang:en>/downloads'=> 'site/downloads',
		'<lang:es>/descargas'=> 'site/downloads',
		'<lang:\w+>/<action>' => 'site/<action>',
     )
),

(Take note that we also added 'matchValue' => true to the urlManager array.)