Detecting Browser Language Onbeginrequest

Hi. Need help on this

I have this




in config and

class StartupBehavior extends CBehavior{

    public function attach($owner){

        $owner->attachEventHandler('onBeginRequest', array($this, 'beginRequest'));


    public function beginRequest(CEvent $event){


        if ($language=='en_us')





The problem is that my application breaks and i can’t debug. No erors, no warnings,nothing.

Thanks for the help

Check your http server logs, if there is a fatal error in PHP it doesn’t get logged in application log and your PHP configuration may prevent it from displaying to the user.

Anyway, I do language detection like this, saved in components/ApplicationConfigBehavior.php:



 * ApplicationConfigBehavior is a behavior for the application.

 * It loads additional config parameters that cannot be statically 

 * written in config/main


class ApplicationConfigBehavior extends CBehavior



	 * Declares events and the event handler methods

	 * See yii documentation on behavior


	public function events() {

		return array_merge(parent::events(), array(




	public function beginRequest() {

                // or however you'd like to do it

		// $this->processOption('language', 'language', 'applicationLanguage', array_keys($availableLanguages), Yii::app()->getRequest()->getPreferredLanguage());

		return true;



And I register it in config/main.php like this:

'behaviors' => array('ApplicationConfigBehavior'),


I think you want to do this:

in config.main


// StartupBehaviour is a class and beginRequest is a [b]static[/b] method

The method is required static, dont forget that


Thanks. That is working, but

public function beginRequest(CEvent $event) {

        $language = Yii::app()->request->getPreferredLanguage();

        if ($language != 'pt_pt') {

            $language = 'de';




dont translate the website.

in protected/components/Controller.php create

public function init() {



just it

StartupBehavior and StartupBehaviour are different!

For advanced language selection, have a look at my wiki: