Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit 13ec531

Browse files
rabrowne85martinlindhe
authored andcommitted
#56 Extended to generate multiple files per locale (#82)
1 parent 056293f commit 13ec531

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Locale from './vue-i18n-locales.generated';
5555

5656
Vue.use(VueInternationalization);
5757

58-
const lang = document.documentElement.lang.substr(0, 2);
58+
const lang = document.documentElement.lang.substr(0, 2);
5959
// or however you determine your current app locale
6060

6161
const i18n = new VueInternationalization({
@@ -93,7 +93,7 @@ Object.keys(Locales).forEach(function (lang) {
9393
9494
9595
## Using vuex-i18n
96-
96+
9797
### vuex-i18n
9898
```
9999
npm i --save vuex-i18n
@@ -152,13 +152,13 @@ php artisan vue-i18n:generate --format {es6,umd,json}
152152
```
153153
php artisan vue-i18n:generate --format umd
154154
```
155-
An UMD module can be imported into the browser, build system, node and etc.
155+
An UMD module can be imported into the browser, build system, node and etc.
156156
157157
Now you can include the generated script in the browser as a normal script and reference it with window.vuei18nLocales.
158158
```vue
159159
<script src="{{ asset('js/vue-i18n-locales.generated.js') }}"></script>
160160

161-
// in your js
161+
// in your js
162162
Vue.use(VueI18n)
163163
Vue.config.lang = Laravel.language
164164
Object.keys(window.vuei18nLocales).forEach(function (lang) {
@@ -169,12 +169,23 @@ You can still require/import it in your build system as stated above.
169169
170170
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.
171171
172+
## Generating Multiple Files
173+
174+
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.
175+
176+
There are two options:
177+
1. One file per laravel module language file using switch ```--multi```
178+
2. One file per locale using switch ```--multi-locales```
179+
180+
```
181+
php artisan vue-i18n:generate --multi{-locales}
182+
```
172183
173184
## Parameters
174185
175186
The generator adjusts the strings in order to work with vue-i18n's named formatting,
176187
so you can reuse your Laravel translations with parameters.
177-
188+
178189
resource/lang/message.php:
179190
```php
180191
return [

src/Commands/GenerateInclude.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GenerateInclude extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6}';
14+
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6} {--multi-locales}';
1515

1616
/**
1717
* The console command description.
@@ -37,6 +37,7 @@ public function handle()
3737
$fileName = $this->option('file-name');
3838
$langFiles = $this->option('lang-files');
3939
$format = $this->option('format');
40+
$multipleLocales = $this->option('multi-locales');
4041

4142
if ($umd) {
4243
// if the --umd option is set, set the $format to 'umd'
@@ -47,9 +48,9 @@ public function handle()
4748
throw new \RuntimeException('Invalid format passed: ' . $format);
4849
}
4950

50-
if ($multipleFiles) {
51+
if ($multipleFiles || $multipleLocales) {
5152
$files = (new Generator($config))
52-
->generateMultiple($root, $format);
53+
->generateMultiple($root, $format, $multipleLocales);
5354

5455
if ($config['showOutputMessages']) {
5556
$this->info("Written to : " . $files);

src/Generator.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
9191

9292
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
9393

94-
if(json_last_error() !== JSON_ERROR_NONE)
95-
{
94+
if (json_last_error() !== JSON_ERROR_NONE) {
9695
throw new Exception('Could not generate JSON, error code '.json_last_error());
9796
}
9897

9998
// formats other than 'es6' and 'umd' will become plain JSON
10099
if ($format === 'es6') {
101100
$jsBody = $this->getES6Module($jsonLocales);
102-
} elseif($format === 'umd') {
101+
} elseif ($format === 'umd') {
103102
$jsBody = $this->getUMDModule($jsonLocales);
104103
} else {
105104
$jsBody = $jsonLocales;
@@ -114,7 +113,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
114113
* @return string
115114
* @throws Exception
116115
*/
117-
public function generateMultiple($path, $format = 'es6')
116+
public function generateMultiple($path, $format = 'es6', $multiLocales = false)
118117
{
119118
if (!is_dir($path)) {
120119
throw new Exception('Directory not found: ' . $path);
@@ -139,7 +138,7 @@ public function generateMultiple($path, $format = 'es6')
139138
$this->availableLocales[] = $noExt;
140139
}
141140
if ($fileinfo->isDir()) {
142-
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
141+
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
143142
} else {
144143
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
145144
if ($local === null) continue;
@@ -157,13 +156,12 @@ public function generateMultiple($path, $format = 'es6')
157156
$fileToCreate = $jsPath . $fileName . '.js';
158157
$createdFiles .= $fileToCreate . PHP_EOL;
159158
$jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
160-
if(json_last_error() !== JSON_ERROR_NONE)
161-
{
159+
if (json_last_error() !== JSON_ERROR_NONE) {
162160
throw new Exception('Could not generate JSON, error code '.json_last_error());
163161
}
164162
if ($format === 'es6') {
165163
$jsBody = $this->getES6Module($jsonLocales);
166-
} elseif($format === 'umd') {
164+
} elseif ($format === 'umd') {
167165
$jsBody = $this->getUMDModule($jsonLocales);
168166
} else {
169167
$jsBody = $jsonLocales;
@@ -201,7 +199,7 @@ private function allocateLocaleJSON($path)
201199
* @param string $path
202200
* @return array
203201
*/
204-
private function allocateLocaleArray($path)
202+
private function allocateLocaleArray($path, $multiLocales = false)
205203
{
206204
$data = [];
207205
$dir = new DirectoryIterator($path);
@@ -238,11 +236,14 @@ private function allocateLocaleArray($path)
238236
if ($lastLocale !== false) {
239237
$root = realpath(base_path() . $this->config['langPath'] . '/' . $lastLocale);
240238
$filePath = $this->removeExtension(str_replace('\\', '_', ltrim(str_replace($root, '', realpath($fileName)), '\\')));
241-
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
239+
if ($multiLocales) {
240+
$this->filesToCreate[$lastLocale][$lastLocale][substr($filePath, 1)] = $this->adjustArray($tmp);
241+
} else {
242+
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
243+
}
242244
}
243245

244246
$data[$noExt] = $this->adjustArray($tmp);
245-
246247
}
247248
}
248249
return $data;
@@ -291,10 +292,10 @@ private function adjustArray(array $arr)
291292
*/
292293
private function adjustVendor($locales)
293294
{
294-
if(isset($locales['vendor'])) {
295-
foreach($locales['vendor'] as $vendor => $data) {
296-
foreach($data as $key => $group) {
297-
foreach($group as $locale => $lang) {
295+
if (isset($locales['vendor'])) {
296+
foreach ($locales['vendor'] as $vendor => $data) {
297+
foreach ($data as $key => $group) {
298+
foreach ($group as $locale => $lang) {
298299
$locales[$key]['vendor'][$vendor][$locale] = $lang;
299300
}
300301
}

0 commit comments

Comments
 (0)