As seen in this post, Yii doesn't enforce how language is set and maintained within the session.
According to that post, the preferred method is to create a base controller which implements code for maintaining the language state. That Controller is then used in your code instead of CControler (by defing your classes with "class ... extends MyController")
Here's my method of implementing this idea:
components/MyController.php
class MyController extends CController { function init() { parent::init(); $app = Yii::app(); if (isset($_POST['_lang'])) { $app->language = $_POST['_lang']; $app->session['_lang'] = $app->language; } else if (isset($app->session['_lang'])) { $app->language = $app->session['_lang']; } } }
You can use a custom made Widget to let the user select language:
components/LangBox.php
class LangBox extends CWidget { public function run() { $currentLang = Yii::app()->language; $this->render('langBox', array('currentLang' => $currentLang)); } }
components/views/langBox.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();
Total 9 comments
Thanks for this wiki, very helpful. In addition, I defaulted the language with the preferred language (= first value coming from the HTTP_ACCEPT_LANGUAGE element) :
I use a more elaborate version of the code posted here to also do stuff like language validation etc. The concept is still a model/component/behavior bundle
validateLang() is just a function that returns true if the passed argument is a valid language tag. setDisplayedLangProperties just gets a lang model and sets it as a user state, this way everything in the application can get a refference to the displayed language model and use it ( because some times the two char iso code just isnt enough )
the original forum post isn't available anymore.
After re-reading this wiki, I advice anyone to use a mixed approach:
Use behavior instead of init of the model, but use the method displayed in the article (reading the input directly in the onInit) instead of following my example of another action.
This approach is quite better than mine.
If anyone will do this "mixed approach" with success and test it, can please update the article with a definitive, tested, version?
Just for other folks, who maybe was struggling with Yii's documentation too when they were trying to find out how and where to put application behavior. So 1st step is to put file with class ApplicationConfigBehavior to the protected/components directory. And 2nd step is to add to your config in protected/config/main.php
Easy, huh? But it took me quite a while to figure this out...
p.s. Thanks to zaccaria for the hint on how to run some piece of code before all requests without overriding controller class.
You can enhance this language managment.
Is possible to write a behavior for the application that will set the language, without any needs to create a masterclass for all controllers
As you see, I took the actual language by the user state. For save the language I created a special action in SIteController:
Because the file is auto required from /components, whereas it isn't in /controllers
You could also store the preferred language in a cookie, instead of the session, and then retrieve it on every page load and on the language selection page.
Leave a comment
Please login to leave your comment.