码迷,mamicode.com
首页 > 编程语言 > 详细

Yii框架配置语言包

时间:2018-05-06 13:43:03      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:翻译   log   idt   php   配置文件   技术分享   map   link   info   

  • 配置文件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框架配置语言包

标签:翻译   log   idt   php   配置文件   技术分享   map   link   info   

原文地址:https://www.cnblogs.com/wxdblog/p/8997780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!