Skip to content

Commit 0e49474

Browse files
carestadandywer
authored andcommitted
Support splitting up messages.js to locale-based .js files (andywer#48)
* Add support for splitting up the messages.js file into language/locale based .js files as well. * Correct indenting in readme * Correct typo in README * Improve README for the static generation part a bit
1 parent 2ca47fe commit 0e49474

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ return [
8181
* ]
8282
*/
8383

84-
// Set the keys of config properties you want to use in javascript.
85-
// Caution: Do not expose any configuration values that should be kept privately!
86-
'config' => [
87-
'app.debug'
88-
],
84+
// Set the keys of config properties you want to use in javascript.
85+
// Caution: Do not expose any configuration values that should be kept privately!
86+
'config' => [
87+
'app.debug'
88+
],
8989

90-
// Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh`
91-
// each time you change configuration files.
92-
// Attention: Should not be used in production mode due to decreased performance.
93-
'disable_config_cache' => false,
90+
// Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh`
91+
// each time you change configuration files.
92+
// Attention: Should not be used in production mode due to decreased performance.
93+
'disable_config_cache' => false,
94+
95+
// Split up the exported messages.js file into separate files for each locale.
96+
// This is to ensue faster loading times so one doesn't have to load translations for _all_ languages.
97+
'split_export_files' => true,
9498
];
9599
```
96100

@@ -155,9 +159,18 @@ The files can then be generated using the artisan command:
155159
`php artisan js-localization:export`
156160

157161
This will generate two files in your target directory:
158-
* `messags.js` contains your translation strings
162+
* `messages.js` contains your translation strings
159163
* `config.js` contains your exported config values
160164

165+
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:
166+
167+
```
168+
'split_export_files' => true,
169+
```
170+
171+
This will in turn _also_ generate the following file(s) in your target directory:
172+
* `lang-{locale}.js` contains one language's translation strings, if the `split_export_files` config option is set to true
173+
161174
Remember that the files needs to be regenerated using `php artisan js-localization:export` every time any translation strings are edited, added or removed.
162175

163176
Features

config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@
6060
*/
6161
'disable_config_cache' => false,
6262

63+
/*
64+
|--------------------------------------------------------------------------
65+
| Whether or not to split up the exported messages.js file into separate
66+
| lang-{locale}.js files.
67+
|--------------------------------------------------------------------------
68+
*/
69+
'split_export_files' => false,
6370
];

src/Console/ExportCommand.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,43 @@ public function createPath($filename)
8888
*/
8989
public function generateMessagesFile($path)
9090
{
91+
$splitFiles = Config::get('js-localization.split_export_files');
9192
$messages = MessageCachingService::getMessagesJson();
9293

93-
$contents = 'Lang.addMessages(' . $messages . ');';
94+
if ($splitFiles) {
95+
$this->generateMessageFiles(File::dirname($path), $messages);
96+
}
97+
98+
$contents = 'Lang.addMessages(' . $messages . ');';
9499

95100
File::put($path, $contents);
96101

97102
$this->line("Generated $path");
98103
}
99104

105+
/**
106+
* Generate the lang-{locale}.js files
107+
*
108+
* @param string $path Directory to where we will store the files
109+
* @param string $messages JSON string of messages
110+
*/
111+
protected function generateMessageFiles(string $path, string $messages)
112+
{
113+
$locales = Config::get('js-localization.locales');
114+
$messages = json_decode($messages, true);
115+
116+
foreach ($locales as $locale) {
117+
$fileName = $path . "/lang-{$locale}.js";
118+
119+
if (key_exists($locale, $messages)) {
120+
$content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');';
121+
}
122+
123+
File::put($fileName, $content);
124+
$this->line("Generated $fileName");
125+
}
126+
}
127+
100128
/**
101129
* Generage the config file.
102130
*

0 commit comments

Comments
 (0)