From 13ec5314f2fb8a33216dddeda2b1c65e7dda2fc8 Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Wed, 1 May 2019 18:42:36 +0100 Subject: [PATCH] #56 Extended to generate multiple files per locale (#82) --- README.md | 21 ++++++++++++++++----- src/Commands/GenerateInclude.php | 7 ++++--- src/Generator.php | 31 ++++++++++++++++--------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a417422..73348e5 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ import Locale from './vue-i18n-locales.generated'; Vue.use(VueInternationalization); -const lang = document.documentElement.lang.substr(0, 2); +const lang = document.documentElement.lang.substr(0, 2); // or however you determine your current app locale const i18n = new VueInternationalization({ @@ -93,7 +93,7 @@ Object.keys(Locales).forEach(function (lang) { ## Using vuex-i18n - + ### vuex-i18n ``` npm i --save vuex-i18n @@ -152,13 +152,13 @@ php artisan vue-i18n:generate --format {es6,umd,json} ``` php artisan vue-i18n:generate --format umd ``` -An UMD module can be imported into the browser, build system, node and etc. +An UMD module can be imported into the browser, build system, node and etc. Now you can include the generated script in the browser as a normal script and reference it with window.vuei18nLocales. ```vue -// in your js +// in your js Vue.use(VueI18n) Vue.config.lang = Laravel.language Object.keys(window.vuei18nLocales).forEach(function (lang) { @@ -169,12 +169,23 @@ You can still require/import it in your build system as stated above. One advantage of doing things like this is you are not obligated to do a build of your javascript each time a the translation files get changed/saved. A good example is if you have a backend that can read and write to your translation files (like Backpack). You can listen to a save event there and call vue-i18n-generator. +## Generating Multiple Files + +Sometimes you may want to generate multiple files as you want to make use of lazy loading. As such, you can specify that the generator produces multiple files within the destination directory. + +There are two options: +1. One file per laravel module language file using switch ```--multi``` +2. One file per locale using switch ```--multi-locales``` + +``` +php artisan vue-i18n:generate --multi{-locales} +``` ## Parameters The generator adjusts the strings in order to work with vue-i18n's named formatting, so you can reuse your Laravel translations with parameters. - + resource/lang/message.php: ```php return [ diff --git a/src/Commands/GenerateInclude.php b/src/Commands/GenerateInclude.php index 763dbfb..30fb23d 100644 --- a/src/Commands/GenerateInclude.php +++ b/src/Commands/GenerateInclude.php @@ -11,7 +11,7 @@ class GenerateInclude extends Command * * @var string */ - protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6}'; + protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6} {--multi-locales}'; /** * The console command description. @@ -37,6 +37,7 @@ public function handle() $fileName = $this->option('file-name'); $langFiles = $this->option('lang-files'); $format = $this->option('format'); + $multipleLocales = $this->option('multi-locales'); if ($umd) { // if the --umd option is set, set the $format to 'umd' @@ -47,9 +48,9 @@ public function handle() throw new \RuntimeException('Invalid format passed: ' . $format); } - if ($multipleFiles) { + if ($multipleFiles || $multipleLocales) { $files = (new Generator($config)) - ->generateMultiple($root, $format); + ->generateMultiple($root, $format, $multipleLocales); if ($config['showOutputMessages']) { $this->info("Written to : " . $files); diff --git a/src/Generator.php b/src/Generator.php index 52e0ddf..adcd143 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -91,15 +91,14 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l $jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; - if(json_last_error() !== JSON_ERROR_NONE) - { + if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception('Could not generate JSON, error code '.json_last_error()); } // formats other than 'es6' and 'umd' will become plain JSON if ($format === 'es6') { $jsBody = $this->getES6Module($jsonLocales); - } elseif($format === 'umd') { + } elseif ($format === 'umd') { $jsBody = $this->getUMDModule($jsonLocales); } else { $jsBody = $jsonLocales; @@ -114,7 +113,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l * @return string * @throws Exception */ - public function generateMultiple($path, $format = 'es6') + public function generateMultiple($path, $format = 'es6', $multiLocales = false) { if (!is_dir($path)) { throw new Exception('Directory not found: ' . $path); @@ -139,7 +138,7 @@ public function generateMultiple($path, $format = 'es6') $this->availableLocales[] = $noExt; } if ($fileinfo->isDir()) { - $local = $this->allocateLocaleArray($fileinfo->getRealPath()); + $local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales); } else { $local = $this->allocateLocaleJSON($fileinfo->getRealPath()); if ($local === null) continue; @@ -157,13 +156,12 @@ public function generateMultiple($path, $format = 'es6') $fileToCreate = $jsPath . $fileName . '.js'; $createdFiles .= $fileToCreate . PHP_EOL; $jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; - if(json_last_error() !== JSON_ERROR_NONE) - { + if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception('Could not generate JSON, error code '.json_last_error()); } if ($format === 'es6') { $jsBody = $this->getES6Module($jsonLocales); - } elseif($format === 'umd') { + } elseif ($format === 'umd') { $jsBody = $this->getUMDModule($jsonLocales); } else { $jsBody = $jsonLocales; @@ -201,7 +199,7 @@ private function allocateLocaleJSON($path) * @param string $path * @return array */ - private function allocateLocaleArray($path) + private function allocateLocaleArray($path, $multiLocales = false) { $data = []; $dir = new DirectoryIterator($path); @@ -238,11 +236,14 @@ private function allocateLocaleArray($path) if ($lastLocale !== false) { $root = realpath(base_path() . $this->config['langPath'] . '/' . $lastLocale); $filePath = $this->removeExtension(str_replace('\\', '_', ltrim(str_replace($root, '', realpath($fileName)), '\\'))); - $this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp); + if ($multiLocales) { + $this->filesToCreate[$lastLocale][$lastLocale][substr($filePath, 1)] = $this->adjustArray($tmp); + } else { + $this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp); + } } $data[$noExt] = $this->adjustArray($tmp); - } } return $data; @@ -291,10 +292,10 @@ private function adjustArray(array $arr) */ private function adjustVendor($locales) { - if(isset($locales['vendor'])) { - foreach($locales['vendor'] as $vendor => $data) { - foreach($data as $key => $group) { - foreach($group as $locale => $lang) { + if (isset($locales['vendor'])) { + foreach ($locales['vendor'] as $vendor => $data) { + foreach ($data as $key => $group) { + foreach ($group as $locale => $lang) { $locales[$key]['vendor'][$vendor][$locale] = $lang; } }