Reputation: 107
I'm working on a multilingual website and I want to create multilingual routes.
example:
monsite.com/en/the-house, monsite.com/fr/la-maison and monsite.com/it/la-casa link to the "house" action of the "pages" controller
monsite.com/en/leisures, monsite.com/fr/loisirs and monsite.com/it/tempo-libero to action "leisures" of the "pages" controller...
that's my route:
Router::connect('/:lang/'.__("the-house"),
array('controller' => 'pages', 'action' => 'house'),
array('lang' => '[a-z-]{2}')
);
i find a "manuel" solution but i want to do it "automatically"
Router::connect('/:lang/:slug',
array('controller' => 'pages', 'action' => 'house'),
array('lang' => '[a-z-]{2}', 'slug' => 'la-maison|the-house|la-casa')
);
thanks ;)
Upvotes: 1
Views: 2841
Reputation: 943
You can also pass the lang
variable in router configuration and check this variable in your controller at beforeFilter()
event.
For example:
Router::connect('/:lang/:slug', array('controller' => 'pages', 'action' => 'view'), array('slug' => '[a-z0-9-]+', 'lang' => 'en|fr|it', 'pass' => array('slug', 'lang')));
And in your controller:
public function beforeFilter() {
parent::beforeFilter();
if (isset($this->request->params['lang'])) {
// do what you want with your language variable.
// Like setting Config.language or set your models locale property, etc.
}
}
Or uber-manual way:
public function view($slug = null, $lang = null) {
switch ($lang) {
case 'en': /* locale is english */ break;
case 'fr': /* locale is french */ break;
}
}
For reverse routing (ie, when using $this->Html->link()
in your views), you need to pass lang
parameter to routing array to make your link for your locale.
And also, it's better to use Translate
behaviour and i18n
tables to work on multi language records for your models easily.
Little warning: CakePHP uses 3 letter ISO codes for locales internally, like 'eng', 'ger', 'fra'.
Upvotes: 1