Detecting Browser Language Onbeginrequest

Hi. Need help on this

I have this


  'behaviors'=>array(

    'onBeginRequest'=>array('class'=>'StartupBehaviour'),

),

in config and


class StartupBehavior extends CBehavior{

    public function attach($owner){

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

    }


    public function beginRequest(CEvent $event){

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

        if ($language=='en_us')

            $language='de';

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

    }

}

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:




<?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(

			'onBeginRequest'=>'beginRequest',

		));

	}


	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'),



Hi,

I think you want to do this:

in config.main




  'onBeginRequest'=>array('StartupBehaviour','beginRequest'),

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



The method is required static, dont forget that

cheers

Thanks. That is working, but


public function beginRequest(CEvent $event) {


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

        if ($language != 'pt_pt') {


            $language = 'de';

            Yii::app()->setLanguage($language);

        }

    }

dont translate the website.

in protected/components/Controller.php create

public function init() {

Yii::app()-&gt;setLanguage(Yii::app()-&gt;request-&gt;getPreferredLanguage());

}

just it

StartupBehavior and StartupBehaviour are different!

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

http://www.yiiframework.com/wiki/792/selecting-best-language-based-on-browser-language-and-available-languages/