Changes
Title
unchanged
How to have truly multilingual URLs
Category
unchanged
How-tos
Yii version
changed
1.1
Tags
changed
URL,multiliangual, ge,multilainguage, url, multilingual url, multilanguage url
Content
changed
#Introduction
------------------
We can easily have a multilingual site if we put a "lang" parameter that comes via GET. For example:
```php
<?php
echo CHtml::link('
GameDownloads', array('/site/
gamedownloads', 'lang' => 'en'));
echo CHtml::link('
Juegos', array('/site/games', 'lang' => 'es'));
echo CHtml::link('Services', array('/site/services', 'lang' => 'en'));
echo CHtml::link('Servicios', array('/site/serviceDescargas', array('/site/downloads', 'lang' => 'es'));
?>
```[...]
Then, in Controller.php, we need to put this piece of code:
```php
public function beforeAction($action)
{[...]
Finally we can add the following rules:
```php
'urlManager'=>array(
'urlFormat'=>'path',[...]
```
#The problem
-----------
This leads to URLs like these:
-
http://www.oligalma.com/en/
games downloads (English)
-
http://www.oligalma.com/es/
games (Spanish)
- http://www.oligalma.com/en/services (English)
- http://www.oligalma.com/es/services downloads (Spanish)
This is fine to have a multilingual site. But, we can go one step further. What if we want URLs like these:
-
[http://www.oligalma.com/en/
games](http://www.oligalma.com/en/games "") downloads (English)
-
[http://www.oligalma.com/es/
juegos](http://www.oligalma.com/es/juegos "") (Spanish)
- [http://www.oligalma.com/en/services](http://www.oligalma.com/en/services "") (English)
- [http://www.oligalma.com/es/servicios](http://www.oligalma.com/es/servicios "") descargas (Spanish)
That is, every URL has its particular path. It changes the whole URL, not just the two initial letters (en/es).
#The solution
------------
```php
'urlManager'=>array(
'matchValue'=>true,[...]
'showScriptName'=>false,
'rules'=>array(
'<lang:\w+>'=>'site/index',
'<lang:e
sn>/
juegos'=> 'site/games',
'<lang:en>/games'=> 'site/games',
'<lang:es>/servicios'=> 'site/services',
'<lang:en>/services'=> 'site/services',
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.)[...]