2017 © Pedro Peláez
 

yii2-extension yii2-i18n-json-export

Export and import i18n JSON messages

image

vovan-ve/yii2-i18n-json-export

Export and import i18n JSON messages

  • Friday, July 27, 2018
  • by Vovan-VE
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Yii2 I18N JSON Export

Latest Stable Version License, (*1)

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., (*2)

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

// 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:, (*4)

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

or add to require section in your composer.json:, (*5)

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

Usage

Add app configuration like following:, (*6)

'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:, (*7)

// @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:, (*8)

$ 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, (*9)

The Versions

27/07 2018

dev-master

9999999-dev

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

27/07 2018

v1.0.5

1.0.5.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

27/07 2018

dev-devel

dev-devel

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

27/07 2018

v1.0.4

1.0.4.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

26/07 2018

v1.0.3

1.0.3.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

26/07 2018

v1.0.2

1.0.2.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

26/07 2018

v1.0.1

1.0.1.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import

25/07 2018

v1.0.0

1.0.0.0

Export and import i18n JSON messages

  Sources   Download

MIT

The Requires

 

by Avatar Vovan-VE

merge json yii2 export i18n import