定制 vovan-ve/yii2-i18n-json-export 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vovan-ve/yii2-i18n-json-export

Composer 安装命令:

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

包简介

Export and import i18n JSON messages

README 文档

README

Latest Stable Version License

Imagine your project has multiple parts with its own separate I18N translations. That parts may be for example backend, server side of frontend, and client side of frontend.

Of cause, some parts may share some messages, and you sure to provide the same translations for such shared messages:

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

Solution

  1. Export all translations from all source parts into single merged file per each language.
  2. Translate messages for specific language in a single JSON file.
  3. Import it back to update existing translations in source parts.

Installation

Install through composer:

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

or add to require section in your composer.json:

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

Usage

Add app configuration like following:

'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,
    ],
],

Use component for example from CLI controller:

// @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)
        );
    }
}

You are ready:

$ 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' => 'Тестовое сообщение',
];

License

This package is under MIT License

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固