Skip to content
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
```

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
30 changes: 29 additions & 1 deletion src/Console/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down