there is a good wiki that describe how to have a multi language site with using behaviour. if i change post method to get method it works too but what do i add to url manager to see enabled language in all page’s url like www.site.com/en/page1
there is a good wiki that describe how to have a multi language site with using behaviour. if i change post method to get method it works too but what do i add to url manager to see enabled language in all page’s url like www.site.com/en/page1
have some thing like following and you can access it with $_GET[‘lang’]
'<lang:\w+>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
Dear Shayan,
I read those 2 wiki articles through your links.
What i have learnt is that application default language is changed based user’s request and stored as
session variable.
For entire session we can get this by calling Yii::app()->language;
I tried this in my localhost.
I modified rules in urlManager.(looks uglier though).
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>true,
'rules'=>array(
Yii::app()->language.'/'.'<controller:\w+>s'=>'<controller>/index',
Yii::app()->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
Yii::app()->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Yii::app()->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
),),
These are the things I am getting in navigation bar
http://localhost/blog/index.php/en_us/posts
http://localhost/blog/index.php/en_us/post/view/7
http://localhost/blog/index.php/en_us/post/update/7
http://localhost/blog/index.php/en_us/post/create
http://localhost/blog/index.php/en_us/comment/approval
http://localhost/blog/index.php/en_us/post/admin
thank u for suggestion. but i dont know why i see en_us for different languages. it seems yii::app()->language doesn’t change in main.php . although i check yii::app()->language in view and it shows right value (selected language). can you test in localhost please?
thank u.i try it but it doesn’t work for me.
Dear Friend,
You are exactly correct.I am really sorry.
What I thought was Yii::app()->language is readily available as session variable and this can be used
in url management.
The situation is really difficult. Since application is configured, even before analyzing user request,
any runtime variable attached to url rules throws errors. Only Yii::app()->language works.
But it always defaults to us_en..
To find a solution for this, I have overrided the init method of CWebApplication.
I have to override the createWebApplication method of Yiibase in Yii.php.
<?php
require(dirname(__FILE__).'/YiiBase.php');
require ('/var/www/blog/protected/components/CCustomWebApplication.php');
class Yii extends YiiBase
{
public static function createWebApplication($config=null)
{
return self::createApplication('CCustomWebApplication',$config);
}
}
I have created CCustomWebApplication.php and placed inside the components directory.
<?php
class CCustomWebApplication extends CWebApplication
{
protected function init()
{
parent::init();
// preload 'request' so that it has chance to respond to onBeginRequest event.
$this->getRequest();
//CHttpRequest::getParam is able to handle both GET and POST variables.
if($this->request->getParam('lang')!==null)
{
$this->user->setState('language',$this->request->getParam('lang'));
$this->language=$this->user->getState('language');
}
if($this->user->hasState('language'))
$this->language=$this->user->getState('language');
//Here only we are declaring url rules not in main config file.
$this->urlManager->addRules(array(
$this->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
));
}
}
?>
In main configuration file, I have not declared any Url rules.The idea here is to declare the url rules
after analysing the user request. That is why we are declaring the url rules in CCustomWebApplication::init()
method, at the end, after analysing the user request.There I have added only one rule.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>true,
),
I have added following two links in views/site/index.php.
<a href="http://localhost/blog/index.php?r=site/index&lang=en_us">English</a>
<a href="http://localhost/blog/index.php?r=site/index&lang=fr_fr">French</a>
If I click either one of them, all the urls following that will have that language code following
index.php.
http://localhost/blog/index.php/fr_fr/site/index
http://localhost/blog/index.php/fr_fr/post/index
http://localhost/blog/index.php/fr_fr/post/view?id=7
http://localhost/blog/index.php/en_us/site/index
http://localhost/blog/index.php/en_us/post/index
http://localhost/blog/index.php/en_us/post/view?id=7
I do not know whether this is a pragmatic approach.
expecting your inputs…
Regards.