You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
In this tutorial will be explained a method for manage some configuration runtime. This excellent tutorial follows a similar approach, but requires to write a masterclass wich all controllers are supposed to extend, following this wiki you can achive the same editing only the configuration.
This is often needed for manage internationalization, themes and other confugration that depends from the user.
The most confortable way is to user an application behavior. In this example we save the language in the session, but can also be saved in database.
Create a file in compoments named ApplicationConfigBehavior and copy this code in it:
<?php
/**
* ApplicationConfigBehavior is a behavior for the application.
* It loads additional config paramenters that cannot be statically
* written in config/main
*/
class ApplicationConfigBehavior extends CBehavior
{
/**
* Declares events and the event handler methods
* See yii documentation on behaviour
*/
public function events()
{
return array_merge(parent::events(), array(
'onBeginRequest'=>'beginRequest',
));
}
/**
* Load configuration that cannot be put in config/main
*/
public function beginRequest()
{
if (isset($_POST['lang']))
$this->owner->user->setState('applicationLanguage', $_POST['lang']);
if ($this->owner->user->getState('applicationLanguage'))
$this->owner->language=$this->owner->user->getState('applicationLanguage');
else
$this->owner->language='en';
}
}
This file must be included in config/main.php:
'behaviors' => array('ApplicationConfigBehavior')
For update the language we can use a widget (to be included in layout/main), for example:
components/LangBox.php
<?php
class LangBox extends CWidget
{
public function run()
{
$currentLang = Yii::app()->language;
$this->render('langBox', array('currentLang' => $currentLang));
}
}
?>
components/views/langBox.php
<?php echo CHtml::form(); ?>
<div id="langdrop">
<?php echo CHtml::dropDownList('_lang', $currentLang, array(
'en_us' => 'English', 'is_is' => 'Icelandic'), array('submit' => '')); ?>
</div>
<?php echo CHtml::endForm(); ?>
I couldn`t make widget work
hi zaccaria, I followed this wiki. But do not make it work.
I see I cannot make dropDownList( , , , array('submit'=>'')); This button didn`t make me take to function in widget or ApplicationConfigBehavior. where I am really confuse in.
In action\controller when we use form, the form`s submit button knows which action to render. But in widget I can not show which function has to render for clicking submit button.
Please, help me.
this is the best approach
I have looked at 3 other approaches
http://www.yiiframework.com/wiki/210/multilanguage-web-site-controlling-by-get-request-and-database-allowed-languages/
http://www.yiiframework.com/extension/langhandler/
http://www.yiiframework.com/wiki/26/setting-and-maintaining-the-language-in-application-i18n/
anyhow, this tutorial is the best, the neater and the straighter solution.
thank you zaccaria
drop down id does not match $_POST id in ApplicationConfigBehavior
Hey guys, just a quick heads up. If this widget is not functioning properly, it could be because the id of the drop down is '_lang' and we refer to the post data as 'lang'. Just make them they have same name and it should now operate fine.
Would there be any reason we would want to name the id _lang, with the underscore?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.