From 1baa6c194c33f6b3785f6d6c2ca1d85611c7ff5e Mon Sep 17 00:00:00 2001 From: Jose Vicente Orts Romero Date: Mon, 8 Jan 2024 12:16:40 +0100 Subject: [PATCH] New export options for PHP files --- .gitignore | 4 +- composer.json | 106 +++++++++++++++++---------------- config/translation-manager.php | 9 ++- src/Manager.php | 52 ++++++++++++---- 4 files changed, 108 insertions(+), 63 deletions(-) diff --git a/.gitignore b/.gitignore index 07773269..00d4ce65 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ composer.phar composer.lock .DS_Store .idea -.phpunit.result.cache \ No newline at end of file +.phpunit.cache +.phpunit.result.cache +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index 3edb320f..78e569e5 100644 --- a/composer.json +++ b/composer.json @@ -1,53 +1,59 @@ { - "name": "barryvdh/laravel-translation-manager", - "description": "Manage Laravel Translations", - "keywords": [ - "laravel", - "translations", - "translator" - ], - "license": "MIT", - "authors": [ - { - "name": "Barry vd. Heuvel", - "email": "barryvdh@gmail.com" - } - ], - "require": { - "php": ">=7.2", - "illuminate/support": "^9|^10", - "illuminate/translation": "^9|^10", - "symfony/finder": "^6" - }, - "require-dev": { - "orchestra/testbench": "^7|^8" - }, - "suggest": { - "tanmuhittin/laravel-google-translate": "If you want to translate using Google API" - }, - "autoload": { - "psr-4": { - "Barryvdh\\TranslationManager\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Barryvdh\\TranslationManager\\Tests\\": "tests/" - } - }, - "extra": { - "branch-alias": { - "dev-master": "0.6-dev" + "name": "barryvdh/laravel-translation-manager", + "description": "Manage Laravel Translations", + "keywords": [ + "laravel", + "translations", + "translator" + ], + "license": "MIT", + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + }, + { + "name": "Jose Vicente Orts Romero", + "email": "jvortsromero@gmail.com" + } + ], + "require": { + "php": ">=7.2", + "brick/varexporter": "^0.3.0", + "illuminate/support": "^9|^10", + "illuminate/translation": "^9|^10", + "symfony/finder": "^6" }, - "laravel": { - "providers": [ - "Barryvdh\\TranslationManager\\ManagerServiceProvider" - ] - } - }, - "scripts": { - "test": "phpunit" - }, - "minimum-stability": "dev", - "prefer-stable": true + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.46.0", + "orchestra/testbench": "^7|^8" + }, + "suggest": { + "tanmuhittin/laravel-google-translate": "If you want to translate using Google API" + }, + "autoload": { + "psr-4": { + "Barryvdh\\TranslationManager\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Barryvdh\\TranslationManager\\Tests\\": "tests/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "0.6-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\TranslationManager\\ManagerServiceProvider" + ] + } + }, + "scripts": { + "test": "phpunit" + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/config/translation-manager.php b/config/translation-manager.php index 8768907a..83ff0afe 100644 --- a/config/translation-manager.php +++ b/config/translation-manager.php @@ -66,9 +66,16 @@ '$trans.get', ], - /** + /* * Database connection name to allow for different db connection for the translations table. */ 'db_connection' => env('TRANSLATION_MANAGER_DB_CONNECTION', null), + /* + * Extra options during the exportations + */ + 'export-options' => [ + 'use-old-format' => false, + 'has-sub-folders' => true, + ], ]; diff --git a/src/Manager.php b/src/Manager.php index b33530a3..95dfab86 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Brick\VarExporter\VarExporter; use Symfony\Component\Finder\Finder; use Illuminate\Filesystem\Filesystem; use Illuminate\Contracts\Events\Dispatcher; @@ -13,13 +14,21 @@ class Manager { - const JSON_GROUP = '_json'; + public const JSON_GROUP = '_json'; - /** @var \Illuminate\Contracts\Foundation\Application */ + /** + * @var Application + */ protected $app; - /** @var \Illuminate\Filesystem\Filesystem */ + + /** + * @var Filesystem + */ protected $files; - /** @var \Illuminate\Contracts\Events\Dispatcher */ + + /** + * @var Dispatcher + */ protected $events; protected $config; @@ -93,7 +102,7 @@ public function importTranslations($replace = false, $base = null, $import_group $group = $subLangPath.'/'.$group; } - if (! $vendor) { + if (!$vendor) { $translations = \Lang::getLoader()->load($locale, $group); } else { $translations = include $file; @@ -252,7 +261,9 @@ public function missingKey($namespace, $group, $key) public function exportTranslations($group = null, $json = false) { - $group = basename($group); + if (Arr::get($this->config, 'export.options.has-sub-folder', false)) { + $group = basename($group); + } $basePath = $this->app['path.lang']; if (! is_null($group) && ! $json) { @@ -295,12 +306,18 @@ public function exportTranslations($group = null, $json = false) } if ($vendor) { - $path = $path.DIRECTORY_SEPARATOR.'messages.php'; + $path .= DIRECTORY_SEPARATOR.'messages.php'; } else { - $path = $path.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$group.'.php'; + $path .= DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$group.'.php'; } - $output = "config, 'export.options.use-old-format', false)) { + $output .= 'return '.var_export($translations, true).';'.\PHP_EOL; + } else { + $this->recurKSort($translations); + $output .= VarExporter::export($translations, VarExporter::ADD_RETURN); + } $this->files->put($path, $output); } } @@ -349,8 +366,11 @@ protected function makeTree($translations, $json = false) foreach ($translations as $translation) { // For JSON and sentences, do not use dotted notation if ($json || Str::contains($translation->key, [' ']) || Str::endsWith($translation->key, ['.'])) { - $this->jsonSet($array[$translation->locale][$translation->group], $translation->key, - $translation->value); + $this->jsonSet( + $array[$translation->locale][$translation->group], + $translation->key, + $translation->value + ); } else { Arr::set($array[$translation->locale][$translation->group], $translation->key, $translation->value); @@ -438,4 +458,14 @@ public function getConfig($key = null) return $this->config[$key]; } } + + private function recurKSort(&$array): void + { + foreach ($array as &$value) { + if (is_array($value)) { + $this->recurKSort($value); + } + } + ksort($array); + } }