I am trying to add a language to the url with following syntax:
http://www.example.com/en/site/page/view/about
What I have so far works with short urls like:
http://www.example.com/en/site/contact
but not with long once as in my first example
Here is what I have so far:
/config/main.php
'urlManager'=>array(
'class'=>'application.components.MyCUrlManager',
'urlFormat'=>'path', 'showScriptName'=>false,
'rules'=>array(
'<language:\w+>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<language:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<language:\w+>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
<?php // components/MyCUrlManager.php
class MyCUrlManager extends CUrlManager
{
public function createUrl($route,$params=array(),$ampersand='&')
{
if(isset($_POST['_lang'])){
Yii::app()->language = $_POST['_lang'];
}elseif (!isset($route['language']) && $controller != 'srbac'){
$route['language']=Yii::app()->language;
}else{
Yii::app()->language = $route['language'];
}
return parent::createUrl($route, $params, $ampersand);
}
}
?>
class Controller extends CController:
{
/// .....
function init() {
parent::init();
if (isset($_POST['_lang'])) {
Yii::app()->setLanguage($_POST['_lang']);
Yii::app()->session['_lang'] = Yii::app()->language;
}elseif (isset(Yii::app()->session['_lang'])) {
Yii::app()->setLanguage(Yii::app()->session['_lang']);
}
}
}
class LangBox extends CWidget {
public function run() {
$currentLang = Yii::app()->language;
require_once 'Zend/Locale.php';
$locale = new Zend_Locale();
//$siteLanguages = $this->getLang();
$siteLanguages = array('en','de','tr');
foreach($siteLanguages as $value){
$list[$value] = $locale->getTranslation($value, 'Language', $value);
}
asort($list);
$this->render('langBox', array('currentLang' => $currentLang, 'list'=>$list));
}
}