uran1980/yii2-translate-panel

Yii2 Translate Panel让你的应用程序翻译变得出色!

安装量: 18 181

依赖项: 1

建议者: 0

安全性: 0

星标: 25

关注者: 7

分支: 16

开放性问题: 3

类型:yii2-extension

0.1.33 2015-03-11 17:30 UTC

This package is auto-updated.

Last update: 2024-09-13 04:09:45 UTC


README

Yii2 Translate Panel让你的应用程序翻译变得出色!

本模块基于i18n (国际化) 模块,着重提高了易用性(见下面的屏幕截图)。

Yii2 Translate Panel screens

安装

安装Yii2

安装并配置Yii2 App Advanced 模板

Composer

通过Composer安装此扩展是首选方式。

运行以下命令之一

php composer.phar require uran1980/yii2-translate-panel "dev-master"

或者将以下内容添加到你的composer.json文件的require部分

"uran1980/yii2-translate-panel": "dev-master"

用法

common/config/main.php中配置urlManager和"Yii2 Translate Panel"组件

return [
    ...
    'components' => [
        ...
        'urlManager' => [
            'class'             => yii\web\UrlManager::className(),
            'enablePrettyUrl'   => true,
            'showScriptName'    => false, // false - means that index.php will not be part of the URLs
        ],
        'i18n' => [
            'class'      => uran1980\yii\modules\i18n\components\I18N::className(),
            'languages'  => ['en', 'de', 'fr', 'it', 'es', 'pt', 'ru'],
            // Or, if you manage languages in database
            //'languages'  => function() {
            //    /* /!\ Make sure the result is a mere list of language codes, and the
            //     * one used in views is the first one */
            //    return \namespace\of\your\LanguageClass::find()->where(['active' => true'])->orderBy('default' => SORT_DESC])->select('code')->column();
            //},
            'format'     => 'db',
            'sourcePath' => [
                __DIR__ . '/../../frontend',
                __DIR__ . '/../../backend',
                __DIR__ . '/../../common',
            ],
            'messagePath' => __DIR__  . '/../../messages',
            // Whether database messages are to be used instead of view ones.
            // Enables editing messages in locale specified by
            // Yii::$app->sourceLanguage
            // Can be set per translation category too
            //'forceTranslation' => true,
            'translations' => [
                '*' => [
                    'class'           => yii\i18n\DbMessageSource::className(),
                    'enableCaching'   => true,
                    'cachingDuration' => 60 * 60 * 2, // cache on 2 hours
                    // Whether database messages are to be used instead of view
                    // ones. Enables editing messages in view code locale.
                    // Can be set globally too.
                    //'forceTranslation' => true,
                ],
            ],
        ],
        ...
    ],
    ...
];

backend/config/main.php中配置"Yii2 Translate Panel"模块

return [
    ...
    'modules' => [
        ...
        'i18n' => [
            'class' => uran1980\yii\modules\i18n\Module::className(),
            'controllerMap' => [
                'default' => uran1980\yii\modules\i18n\controllers\DefaultController::className(),
            ],
            // example for set access control to module (if required):
            'as access' => [
                'class' => yii\filters\AccessControl::className(),
                'rules' => [
                    [
                        'controllers'   => ['i18n/default'],
                        'actions'       => ['index', 'save', 'update', 'rescan', 'clear-cache', 'delete', 'restore', 'clear-deleted'],
                        'allow'         => true,
                        'roles'         => ['translator'],
                    ]
                ],
            ],
        ],
        ...
    ],
    ...
]

运行

php yii migrate --migrationPath=@uran1980/yii/modules/i18n/migrations

访问http://backend.yourdomain.com/translations进行消息翻译

PHP到数据库导入

如果你有一个基于PHP的旧项目,你可以通过控制台迁移到DbSource。

运行

php yii i18n/import @common/messages

其中@common/messages是应用程序翻译的路径

数据库到PHP导出

运行

php yii i18n/export @uran1980/yii/modules/i18n/messages uran1980/modules/i18n

其中@uran1980/yii/modules/i18n/messages是应用程序翻译的路径,uran1980/modules/i18n是数据库中的翻译类别

使用数据库中的yii类别

从PHP文件导入翻译

php yii i18n/import @yii/messages

信息

组件使用yii\i18n\MissingTranslationEvent自动将缺少的翻译添加到数据库

参阅Yii2 i18n指南

作者

Ivan Yakovlev,电子邮件:uran1980@gmail.com