Yii框架配置语言包

Posted 微醺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii框架配置语言包相关的知识,希望对你有一定的参考价值。

  • 配置文件frontend\\config\\main.php
    \'language\' => \'zh-CN\',
    //配置语言包
            \'i18n\' =>[
                \'translations\' => [
                    \'*\' => [
                        \'class\' => \'yii\\i18n\\phpMessageSource\',
                        //\'basePath\' => \'/messages\',
                        \'fileMap\' => [
                            \'common\' => \'common.php\'
                        ],
                    ]
                ],
            ],

  • 在frontend\\messages\\zh-CN\\common.php中编写中文翻译(没有则创建相应的目录及文件)

    <?php
    
    return [
        \'Blog\' => \'博客\',
        \'Home\'    => \'首页\',
        \'About\'    => \'关于我们\',
        \'Contact\'    => \'联系我们\',
        \'Signup\'    => \'注册\',
        \'Login\'    => \'登录\',
    ];
  • 在前端frontend\\views\\layouts\\main.php中使用
    <?php
                NavBar::begin([
                    \'brandLabel\' => Yii::t(\'common\', \'Blog\'),
                    \'brandUrl\' => Yii::$app->homeUrl,
                    \'options\' => [
                        \'class\' => \'navbar-inverse navbar-fixed-top\',
                    ],
                ]);
                $menuItems = [
                    [\'label\' => Yii::t(\'yii\', \'Home\'), \'url\' => [\'/site/index\']],
                    [\'label\' => Yii::t(\'common\', \'About\'), \'url\' => [\'/site/about\']],
                    [\'label\' => Yii::t(\'common\', \'Contact\'), \'url\' => [\'/site/contact\']],
                ];
                if (Yii::$app->user->isGuest) {
                    $menuItems[] = [\'label\' => Yii::t(\'common\', \'Signup\'), \'url\' => [\'/site/signup\']];
                    $menuItems[] = [\'label\' => Yii::t(\'common\', \'Login\'), \'url\' => [\'/site/login\']];
                } else {
                    $menuItems[] = [
                        \'label\' => \'Logout (\' . Yii::$app->user->identity->username . \')\',
                        \'url\' => [\'/site/logout\'],
                        \'linkOptions\' => [\'data-method\' => \'post\']
                    ];
                }
                echo Nav::widget([
                    \'options\' => [\'class\' => \'navbar-nav navbar-right\'],
                    \'items\' => $menuItems,
                ]);
                NavBar::end();
            ?>

     

以上是关于Yii框架配置语言包的主要内容,如果未能解决你的问题,请参考以下文章

yii2框架消息语言

Yii2.0实现语言包切换功能

使用yii2开发基础配置

Yii2多语言

YII框架之soap协议的配置跟使用(Webservice)

yii2 yii,console 怎么执行