PHP code example of vovan-ve / yii2-i18n-json-export

1. Go to this page and download the library: Download vovan-ve/yii2-i18n-json-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vovan-ve / yii2-i18n-json-export example snippets


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

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

// @app/commands/I18nDumpController.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)
        );
    }
}
sh
$ cd /project

# assume you has already extracted messages under ./messages/
$ cat ./messages/ru-RU/category/subcategory.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

return [
    'Test message' => 'Тестовое сообщение',
];