/**
  * Resolve application language by checking user cookies, preferred language and profile settings
  */
 public function beforeRequest()
 {
     if (Yii::$app->getRequest()->getCookies()->has($this->cookieName) && !Yii::$app->session->hasFlash('forceUpdateLocale')) {
         $userLocale = Yii::$app->getRequest()->getCookies()->getValue($this->cookieName);
     } else {
         $userLocale = Yii::$app->language;
         if (!Yii::$app->user->isGuest && Yii::$app->user->identity->userProfile->locale && array_key_exists(Yii::$app->user->identity->userProfile->locale, Languages::getLanguages())) {
             $userLocale = Yii::$app->user->getIdentity()->userProfile->locale;
         } elseif ($this->enablePreferredLanguage) {
             $userLocale = Languages::getDefault();
         }
     }
     if (Yii::$app->getRequest()->get('lang_locale')) {
         $userLocale = Yii::$app->getRequest()->get('lang_locale');
     }
     Yii::$app->language = $userLocale;
 }
 /**
  * @inheritdoc
  */
 public function actions()
 {
     return ['error' => ['class' => 'yii\\web\\ErrorAction'], 'captcha' => ['class' => 'yii\\captcha\\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null], 'set-locale' => ['class' => 'common\\actions\\SetLocaleAction', 'locales' => array_keys(\common\models\Languages::getLanguages())]];
 }
Пример #3
0
<?php

use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
/* @var $this \yii\web\View */
/* @var $content string */
$this->beginContent('@frontend/views/layouts/_clear.php');
?>
    <div class="wrap">
        <?php 
NavBar::begin(['brandLabel' => Yii::$app->name, 'brandUrl' => Yii::$app->homeUrl, 'options' => ['class' => 'navbar-inverse navbar-fixed-top']]);
?>
        <?php 
echo Nav::widget(['options' => ['class' => 'navbar-nav navbar-right'], 'items' => \common\models\WidgetMenu::getMenuLinks('main', [['label' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->getPublicIdentity(), 'visible' => !Yii::$app->user->isGuest, 'items' => [['label' => Yii::t('frontend', 'Settings'), 'url' => ['/user/default/index']], ['label' => Yii::t('frontend', 'Backend'), 'url' => Yii::getAlias('@backendUrl'), 'visible' => Yii::$app->user->can('manager')], ['label' => Yii::t('frontend', 'Logout'), 'url' => ['/user/sign-in/logout'], 'linkOptions' => ['data-method' => 'post']]]], ['label' => Yii::t('frontend', 'Language'), 'items' => array_map(function ($code) {
    return ['label' => \common\models\Languages::getLanguages()[$code], 'url' => ['/site/set-locale', 'locale' => $code], 'active' => Yii::$app->language === $code];
}, array_keys(\common\models\Languages::getLanguages()))]])]);
?>
    <?php 
NavBar::end();
?>

    <?php 
echo $content;
?>

    </div>

    <footer class="footer">
        <div class="container">
            <p class="pull-left">&copy; My Company <?php 
echo date('Y');