diff --git a/README.md b/README.md index 308db31..a7f0b83 100644 --- a/README.md +++ b/README.md @@ -85,16 +85,20 @@ return [ * ] */ - // Set the keys of config properties you want to use in javascript. - // Caution: Do not expose any configuration values that should be kept privately! - 'config' => [ - 'app.debug' - ], + // Set the keys of config properties you want to use in javascript. + // Caution: Do not expose any configuration values that should be kept privately! + 'config' => [ + 'app.debug' + ], - // Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh` - // each time you change configuration files. - // Attention: Should not be used in production mode due to decreased performance. - 'disable_config_cache' => false, + // Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh` + // each time you change configuration files. + // Attention: Should not be used in production mode due to decreased performance. + 'disable_config_cache' => false, + + // Split up the exported messages.js file into separate files for each locale. + // This is to ensue faster loading times so one doesn't have to load translations for _all_ languages. + 'split_export_files' => true, ]; ``` @@ -159,9 +163,18 @@ The files can then be generated using the artisan command: `php artisan js-localization:export` This will generate two files in your target directory: - * `messags.js` contains your translation strings + * `messages.js` contains your translation strings * `config.js` contains your exported config values +If you want to automatically split up the `messages.js` file into separate .js files for each locale, you can set the following to true in your `config/js-localization.php` config file: + +``` + 'split_export_files' => true, +``` + +This will in turn _also_ generate the following file(s) in your target directory: + * `lang-{locale}.js` contains one language's translation strings, if the `split_export_files` config option is set to true + Remember that the files needs to be regenerated using `php artisan js-localization:export` every time any translation strings are edited, added or removed. Features diff --git a/config/config.php b/config/config.php index 4e7d39e..accc82f 100644 --- a/config/config.php +++ b/config/config.php @@ -60,4 +60,11 @@ */ 'disable_config_cache' => false, + /* + |-------------------------------------------------------------------------- + | Whether or not to split up the exported messages.js file into separate + | lang-{locale}.js files. + |-------------------------------------------------------------------------- + */ + 'split_export_files' => false, ]; diff --git a/src/Console/ExportCommand.php b/src/Console/ExportCommand.php index df1895b..992fa41 100644 --- a/src/Console/ExportCommand.php +++ b/src/Console/ExportCommand.php @@ -88,15 +88,43 @@ public function createPath($filename) */ public function generateMessagesFile($path) { + $splitFiles = Config::get('js-localization.split_export_files'); $messages = MessageCachingService::getMessagesJson(); - $contents = 'Lang.addMessages(' . $messages . ');'; + if ($splitFiles) { + $this->generateMessageFiles(File::dirname($path), $messages); + } + + $contents = 'Lang.addMessages(' . $messages . ');'; File::put($path, $contents); $this->line("Generated $path"); } + /** + * Generate the lang-{locale}.js files + * + * @param string $path Directory to where we will store the files + * @param string $messages JSON string of messages + */ + protected function generateMessageFiles(string $path, string $messages) + { + $locales = Config::get('js-localization.locales'); + $messages = json_decode($messages, true); + + foreach ($locales as $locale) { + $fileName = $path . "/lang-{$locale}.js"; + + if (key_exists($locale, $messages)) { + $content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');'; + } + + File::put($fileName, $content); + $this->line("Generated $fileName"); + } + } + /** * Generage the config file. *