<?php

class Application extends CWebApplication
{
    public function init()
    {
        $app = $this;
        preg_match("#^(en|ru)#", $app->request->pathInfo, $langMatches);
        if (0 != count($langMatches)) { // set language from `lang` param
            if (count(array_intersect(array($langMatches[0]), Yii::app()->params['languages'])) == 0)
                $this->request->redirect('/');
            $app->language = $langMatches[0];
            $app->request->cookies['lang'] = new CHttpCookie('lang', Yii::app()->language);
        } else if (isset($app->request->cookies['lang'])) { // set last remembered language
            $app->language = $app->request->cookies['lang']->value;
        } else {
            // todo: set language by request headers
            // $al =  $_SERVER["HTTP_ACCEPT_LANGUAGE"];
        }
        parent::init();

        // todo: warning! remove this handler on production(after messages translation complete).
        Yii::app()->messages->onMissingTranslation = array(new MissingTranslationEventHandler(), 'missingTranslation');
        Yii::app()->messages->forceTranslation = true; // allow ru->ru translation
    }
}
