后台学习

yii2-国际化-i18n组件的完全配置和使用

2016-10-27  本文已影响1407人  playwolf719

写文初衷


传送门(该链接为我借鉴的主要部分,但事实上写的也不全。)

正文


配置i18n到web.php


web.php部分内容

//全局语言设置
'language' => 'zh-CN',
'components' => [
    // ...
    'i18n' => [
        'translations' => [
            'app*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
                'sourceLanguage' => 'en-US',
                'fileMap' => [
                    'app' => 'app.php',
                    'app/error' => 'error.php',
                ],
            ],
        ],
    ],
],

使用yii生成i18n插件配置


./yii message/config @common/config/i18n.php

i18n.php

return [
    'color' => null,
    'interactive' => true,
    'sourcePath' => '@backend',
    'messagePath' => '@common/messages',
    'languages' => ['zh-CN'],
    'translator' => 'Yii::t',
    'sort' => false,
    'overwrite' => true,
    'removeUnused' => false,
    'markUnused' => true,
    'except' => [
        '.svn',
        '.git',
        '.gitignore',
        '.gitkeep',
        '.hgignore',
        '.hgkeep',
        '/messages',
        '/BaseYii.php',
    ],
    'only' => [
        '*.php',
    ],
    'format' => 'php',
    'db' => 'db',
    'sourceMessageTable' => '{{%source_message}}',
    'messageTable' => '{{%message}}',
    'catalog' => 'messages',
    'ignoreCategories' => [],
];

使用yii生成映射目录


./yii message/extract @app/config/i18n.php

app.php

return [
    'OpenId' => 'OpenId',
    'UserId' => '用户id',
    'UserName' => '用户名',
    'UserPortrait' => '用户头像',
    'About' => '关于我们',
    'Are you sure you want to delete this item?' => '确定要删除?',
    'Contact' => '联系方式',
    'Create' => '创建',
    'Create Myuser' => '创建用户',
    'Delete' => '删除',
    'Home' => '首页',
    'Login' => '登录',
    'My Company' => '我的网站',
    'Myusers' => '用户',
    'Reset' => '重置',
    'Search' => '搜索',
    'Update' => '更新',
    'Update {modelClass}: ' => '更新用户-',
];

注意


Yii::t('app', 'Myusers');

展示


中文版.jpg 英文版.jpg
上一篇下一篇

猜你喜欢

热点阅读