It is a simple widget embedding a dropdownlist to redirect on a selected value. I used this to allow the user to change the language.
Tested with yii 1.1
Put the DropDownRedirect.php file in your components directory.
I'll show you the way I used this. In my main layout file, I wrote :
$this->widget('DropDownRedirect', array( 'data' => Yii::app()->user->avalaibleLanguages, // data od my dropdownlist 'url' => $this->createUrl($this->route, array_merge($_GET, array('lang' => '__value__'))), // the url (__value__ will be replaced by the selected value) 'select' => Yii::app()->user->language, //the preselected value ));
The dropdown is now placed. To handle the url redirection, I wrote in my controller :
public function init() { if (isset($_GET['lang'])) Yii::app()->user->language = $_GET['lang']; Yii::app()->user->applyPreferedLanguage(); Yii::app()->name = Yii::t('site', Yii::app()->name); parent::init(); }
The user functions used above are here, in a personnal WebUser :
class WebUser extends CWebUser { public function getLanguage() { if ($this->getState('lang') == null) { $lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)); $this->setLanguage($lang); } return $this->getState('lang'); } public function setLanguage($lang) { $this->setState('lang', $lang); } public function applyPreferedLanguage() { Yii::app()->language = $this->getLanguage(); } public function getAvalaibleLanguages() { return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'french')); } }
...external resources for this extension...
Total 5 comments
This version works in Yii 1.1.5:
For some reason, this extension stopped working by Yii 1.1.5. Would be great if someone could fix it. :)
Since we can't edit our comments, let me correct the criteria select statement:
'id' is not needed, of course. My Project and my Issue controller makes use of the 'name' parameter to determine the active project.
I've successfully integrated this with my application where I use it to allow my users to switch active project.
I my Project and Issue controller I have this function:
I use this code to place the widget:
Thanks a lot!
This is in my WebUser class :
Leave a comment
Please login to leave your comment.