Yii2 I18N JSON Export
, (*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
- Export all translations from all source parts into single merged file per each language.
- Translate messages for specific language in a single JSON file.
- 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)