vovan-ve/yii2-i18n-json-export

导出和导入i18n JSON消息

安装次数: 10

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

类型:yii2-extension

v1.0.5 2018-07-27 00:40 UTC

This package is auto-updated.

Last update: 2024-09-18 02:49:07 UTC


README

Latest Stable Version License

想象一下,您的项目有多个部分,每个部分都有自己的I18N翻译。这些部分可能是后端、前端的服务器端和前端客户端。

当然,一些部分可能共享一些消息,并且您确信为这些共享消息提供相同的翻译

// frontend server side
\Yii::t('app/ui', 'Save')
// frontend client side
i18n.t('app/ui', 'Save')

解决方案

  1. 将所有源部分的翻译导出到每个语言的单个合并文件中。
  2. 在一个JSON文件中翻译特定语言的的消息。
  3. 将其导入以更新源部分的现有翻译。

安装

通过composer安装

composer require vovan-ve/yii2-i18n-json-export

或者在您的composer.json中的require部分添加

"vovan-ve/yii2-i18n-json-export": "~1.0.0"

使用方法

添加以下配置到应用中

'components' => [
    'i18nJsonExport' => [
        'class' => \VovanVE\Yii2I18nJsonExport\Manager::class,
        // list of source drivers
        'sourceDrivers' => [
            [
                'class' => \VovanVE\Yii2I18nJsonExport\drivers\SubdirCategoryPhpDriver::class,
                'path' => '@app/messages',
                // strip category prefix
                //'categoryPrefix' => 'app/',
            ],
        ],
        'exportDriver' => [
            'class' => \VovanVE\Yii2I18nJsonExport\drivers\FlatCategoryDriver::class,
            'path' => '@app/i18n',
            // turn on to bubble empty translations to top
            //'sortEmptyFirst' => true,
        ],
        // whether to import back in same files
        //'overwrite' => true,
    ],
],

例如从CLI控制器使用组件

// @app/commands/I18nDumpController.php:
<?php
namespace app\commands;

use VovanVE\Yii2I18nJsonExport\Manager;
use yii\console\Controller;
use yii\di\Instance;

class I18nDumpController extends Controller
{
    public function actionExport()
    {
        $this->getManager()->export();
    }

    public function actionImport()
    {
        $this->getManager()->import();
    }

    /** @var Manager */
    private $manager;

    /**
     * @return Manager
     */
    private function getManager()
    {
        return $this->manager ?? (
            $this->manager = Instance::ensure('i18nJsonExport', Manager::class)
        );
    }
}

您已准备就绪

$ cd /project

# assume you has already extracted messages under ./messages/
$ cat ./messages/ru-RU/category/subcategory.php
<?php
return [
    'Test message' => '',
];

# export to outsource
$ ./yii i18n-dump/export

# see the result
$ cat ./i18n/ru-RU.json
{
    "category/subcategory": {
        "Test message": ""
    }
}

# translate it somehow like so
$ cat ./i18n/ru-RU.json
{
    "category/subcategory": {
        "Test message": "Тестовое сообщение"
    }
}

# import back
$ ./yii i18n-dump/import

# see new file
# notice `.new` in the end which is covered by default 'overwrite' => false in Manager
$ cat ./messages/ru-RU/category/subcategory.php.new
<?php
return [
    'Test message' => 'Тестовое сообщение',
];

许可

此包受MIT许可约束