yii2.0 - Yii::t()的使用 -- 语言包的使用

第一种情况:在控制器和试图中:

第一步:在protected/config文件夹下的main.php中做如下设置:??

  1  return [
  2     'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
  3     'language' => 'zh-CN',
  4     'components' => [
  5         'cache' => [
  6             'class' => 'yii\caching\FileCache',
  7         ],
  8         'i18n' => [
  9             'translations' => [
 10                 '*' => [
 11                     'class' => 'yii\i18n\PhpMessageSource',
 12                     //'basePath' => '@app/messages',
 13                     //'sourceLanguage' => 'en',
 14                     'fileMap' => [
 15                         'app' => 'app.php',
 16                         'app/error' => 'error.php',
 17                     ],
 18                 ],
 19             ],
 20         ],
 21         /*'yii' => [
 22             'class' => 'yii\i18n\PhpMessageSource',
 23             'sourceLanguage' => 'zh-CN',
 24             'basePath' => '@app/messages'
 25         ],*/
 26         
 27         
 28         
 29         
 30         
 31 //=========== 其他配置 开始====================        
 32         'request' => [
 33             'cookieValidationKey' => '_csrf-ssss',
 34             'parsers' => [
 35                 'application/json' => 'yii\web\JsonParser',
 36             ]
 37         ],
 38        /* 'session' => [
 39             'class' => 'yii\web\DbSession',
 40             // 'db' => 'mydb',  // 数据库连接的应用组件ID,默认为'db'.
 41             'sessionTable' => 'pur_session', // session 数据表名,默认为'session'.
 42         ],*/
 43         
 44         'user' => [
 45             'identityClass' => 'app\models\User',
 46             // 'enableAutoLogin' => true,
 47             'authTimeout'     =>'6000',
 48 
 49 
 50 
 51         ],
 52 
 53         'errorHandler' => [
 54             'errorAction' => 'site/error',
 55         ],
 56         'authManager' => [
 57             'class' => 'yii\rbac\DbManager',
 58 
 59         ],
 60 
 61 
 62         /*'mailer' => [
 63             'class' => 'yii\swiftmailer\Mailer',
 64             // send all mails to a file by default. You have to set
 65             // 'useFileTransport' to false and configure a transport
 66             // for the mailer to send real emails.
 67             'useFileTransport' => YII_DEBUG ? true : false,
 68             'transport' => [
 69                 'class' => 'Swift_SmtpTransport',
 70                 'host' => 'smtp.qq.com',  //每种邮箱的host配置不一样
 71 //                'username' => '897034908@qq.com',//邮箱账号
 72 //                'password' => 'qcldtoyqtdbrbdhf',//邮箱密码(填授权码!!!)
 73                 'username' => '1159240689@qq.com',//邮箱账号
 74                 'password' => 'wxcyqpawsecogbbj',//邮箱密码(填授权码!!!)
 75                 'port' => '465',
 76                 'encryption' => 'ssl',
 77 
 78             ],
 79             'messageConfig'=>[
 80                 'charset'=>'UTF-8',
 81                 'from'=>['1159240689@qq.com'=>'admin']
 82 //                'from'=>['897034908@qq.com'=>'admin']
 83             ],
 84         ],
 85         'log' => [
 86             'traceLevel' => YII_DEBUG ? 3 : 0,
 87             'targets' => [
 88                 'email' => [
 89                     'class' => 'yii\log\EmailTarget',
 90                     'levels' => ['error', 'warning'],
 91                     'message' => [
 92                         'to' => ['982171680@qq.com','361870038@qq.com','897034908@qq.com'],
 93                         'subject' => '少年,采购系统报错了啊!快去处理啊',
 94                     ],
 95                 ],
 96             ],
 97         ],*/
 98         'db' => require(__DIR__ . '/db.php'),
 99 
100         'urlManager' => [
101             'enablePrettyUrl' => true,
102             'showScriptName' => false,
103 
104             'rules' => [
105                 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
106                 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
107             ],
108         ],
109 //================其他配置  结束 ========================
110     ],
111  ]
config

 

第二步:定义翻译文件

在根目录的【messages-》zh-CN】文件夹下,创建app.php文件
1 return array(
2     'My Web Application'=>'我的web站点',
3     'Home'=>'首页',
4     'About'=>'关于',
5     'Contact'=>'联系我们',
6     'Login'=>'登录',
7     'Logout'=>'退出',
8  );
app.php中写入语言包

 

第三步:使用翻译:在控制器和视图中都可使用

//通过内置的Yii::t()方法调用即可 
Yii::t('app','Home'); //返回:【首页】文字

 

 

第二种情况:在mode中??不是很懂

1 public function attributeLabels()
2  {
3         return [
4             'id' => 'ID',
5             'pur_number' => Yii::t('app', '采购单号'),  //第一种
6             'purchase_warehouse' => '仓库',  //第二种
7         ];
8  }
在model中

在视图使用:

1 <?= $form->field($model, 'pur_number') ?>
2 <?= $form->field($model, 'purchase_warehouse') ?>
3 
4 显示:采购单号、仓库
views中显示

 

posted @ 2018-04-26 15:03  liuweipcs  阅读(522)  评论(0)    收藏  举报