language in url

Yii::community->hello($all);

i got this thing with multilanguage,

try to create application with language in Url.

try to use rules like

‘<lang:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’,

create URLmanager inset isue, real isue is how to get

<lang:\w+>, best is to get it in Controller init().

but, HOW? , try $_GET is empty, no parameters, and $this not very helpful.

for consistence i write here all codes




class AppUrlManager extends CUrlManager {


    public function createUrl($route,$params=array(),$ampersand='&')

       {

           if(isset($_POST['_lang']))

           {

               Yii::app()->setLanguage($_POST['_lang']);

               $route['language']=Yii::app()->getLanguage();

           }

           elseif (!isset($route['language']))

           {  $route['language']=Yii::app()->getLanguage(); }

           else

           {   Yii::app()->setLanguage($route['language']); }

           // var_dump($route);

           return parent::createUrl($route, $params, $ampersand);

    }


}



------------------------------- SOLVED SOLUTION --------------------------

rules like that:(thanks to alex-w)




<lang:\w+>/'=>'Site'

<lang:\w+>/<controller:\w+>/'=>'<controller>'

<lang:\w+>/<controller:\w+>/<action:\w+>/'=>'<controller>/<action>'



and aUrlManager extension like




   public function createUrl($route,$params=array(),$ampersand='&')

    $route = Yii::app()->getLanguage().'/'.$route;

// NOT LIKE This : $route['lang']=Yii::app()->getLanguage(); 

     return parent::createUrl($route, $params, $ampersand);

    }



effects :

will add your current app language to url any time call CreateUrl.

What url are you using?

I believe you need to make rules to account for all variants.

<lang:\w+>/’=>‘Site’

<lang:\w+>/<controller:\w+>/’=>’<controller>’

<lang:\w+>/<controller:\w+>/<action:\w+>/’=>’<controller>/<action>’

etc.

yes, and then what?

how can i read <lang:\w+> in constroller init()? or somehere else onoce to setup the language, like Yii:app()->setLanguage()?

Should be in $_GET[‘lang’] iirc.

some more manipulation a figure it out that when using it should be very careful especially with rules order.

now new question, i got this language at stage when i what it, how to automatically create paths with it

i use




class AppUrlManager extends CUrlManager {

 public function createUrl($route,$params=array(),$ampersand='&'){

$route['lang']=Yii::app()->getLanguage();

            return parent::createUrl($route, $params, $ampersand);

}

}



// rules




'rules'=>array(

                '<lang:\w+>'=>'site/index',

                '<lang:\w+>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			    '<lang:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

    		),




and in path i literary have

/index.php/eite/index

/index.php/eite/page/view/about

this eite is relay an anomaly, i figure out that

basically it is "site" with changed first letter of passed language (in this case en_us)

i try other languages and get some , ‘bite’,‘lite’, etc and so on.

next thing i noteces if i use generator without real path such as

array(‘label’=>‘dummyMenuItem’,‘url’=>’#’,‘active’=>true),

then its generate a genuine path with language inserted. like

/index.php/en_us/site/index#

what suggestions can be?

I have solved with this implementation of createUrl() method, that normally parse $_GET[language] and if find $_GET[new_lang] set new language to yii app, to permit to end users of change locale.




public function createUrl($route,$params=array(),$ampersand='&')

{

    if(isset($_GET['new_lang'])) {

        Yii::app()->setLanguage($_GET['new_lang']);

    } else {

        Yii::app()->setLanguage($_GET['language']);

    }

    $route = Yii::app()->getLanguage().'/'.$route;

    return parent::createUrl($route, $params, $ampersand);

}



I bumped into a problem with this technique.

Using the createAbsoluteUrl to create a url for an actual file on the server,

the language part interferes and makes the url invalid…

Lets say I want to make a url to:


http://www.mydomain.com/myfolder/myfilefordownload.zip

It will make it


http://www.mydomain.com/my_lang/myfolder/myfilefordownload.zip

And of course a 404 will rise.

So there are cases where the language must not be added to the url…

http://www.yiiframework.com/wiki/294/seo-conform-multilingual-urls-language-selector-widget-i18n/