配置环境语言 [ 2.0 版本 ]
1.在入口文件配置
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(DIR . '/../../vendor/autoload.php');
require(DIR . '/../../vendor/yiisoft/yii2/Yii.php');
require(DIR . '/../../vendor/thirdparty/dump.php');
require(DIR . '/../../common/config/bootstrap.php');
require(DIR . '/../config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../../common/config/db.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
//入口文件配置
$application->language=isset(\Yii::$app->session['language'])?\Yii::$app->session['language']:'zh-CN';
$application->run();
2.config main.php配置
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-manageend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'defaultRoute'=>'index/index',
'controllerNamespace' => 'manageend\controllers',
//配置环境
'on beforeRequest' => function ($event) {
$l_saved = null;
if (true){
# use cookie to store language
$l_saved = Yii::$app->request->cookies->get('locale');
}else{
# use session to store language
$l_saved = Yii::$app->session['locale'];
}
$l = ($l_saved)?$l_saved:'zh-CN';
Yii::$app->sourceLanguage = 'zh';
Yii::$app->language = $l;
return;
},
//配置环境
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
15807105408 武汉
注册时间:2015-12-13
最后登录:2020-09-27
在线时长:14小时36分
最后登录:2020-09-27
在线时长:14小时36分
- 粉丝9
- 金钱1755
- 威望30
- 积分2195
共 0 条评论