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

Commit 5342682

Browse files
committed
rework internals, make tests more robust
1 parent 09124cd commit 5342682

File tree

6 files changed

+62
-155
lines changed

6 files changed

+62
-155
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ yarn add vuex-i18n vuex
8787

8888
Next, open `config/vue-i18n-generator.php` and do the following changes:
8989

90-
```php
91-
- 'i18nLib' => \MartinLindhe\VueInternationalizationGenerator\Generator::VUE_I18N,
92-
+ 'i18nLib' => \MartinLindhe\VueInternationalizationGenerator\Generator::VUEX_I18N,
90+
```diff
91+
- 'i18nLib' => 'vue-i18n',
92+
+ 'i18nLib' => 'vuex-i18n',
9393
```
9494

9595
Then generate the include file with
@@ -183,12 +183,7 @@ Vue template:
183183

184184
- The generated file is an ES6 module.
185185

186-
- One note on [Pluralization](http://laravel.com/docs/5.5/localization#pluralization). This used not to work with vue-i18n but as mentioned at [#12](https://github.com/martinlindhe/laravel-vue-i18n-generator/issues/12)
187-
they might work since vue-i18n uses the same syntax for separation of singular and plural form as Laravel does. So far this is not confirmed.
188-
189-
On the other hand it has been tested that pluralization work with vuex-i18n for simple singular / plural forms. However, the
190-
more sophisticated localization as described [here](https://laravel.com/docs/5.5/localization#pluralization) is not supported since
191-
vuex-i18n does not support this.
186+
The more sophisticated pluralization localization as described [here](https://laravel.com/docs/5.5/localization#pluralization) is not supported since neither vue-i18n or vuex-i18n support this.
192187

193188
# License
194189

src/Commands/GenerateInclude.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,23 @@ class GenerateInclude extends Command
2727
public function handle()
2828
{
2929
$root = base_path() . config('vue-i18n-generator.langPath');
30+
$config = config('vue-i18n-generator');
3031

32+
// options
3133
$umd = $this->option('umd');
32-
3334
$multipleFiles = $this->option('multi');
34-
3535
$withVendor = $this->option('with-vendor');
3636

37-
$i18nLib = config('vue-i18n-generator.i18nLib');
38-
3937
if ($multipleFiles) {
40-
$files = (new Generator($i18nLib))
38+
$files = (new Generator($config))
4139
->generateMultiple($root, $umd);
4240
echo "Written to :" . PHP_EOL . $files . PHP_EOL;
4341
exit();
4442
}
4543

46-
$data = (new Generator($i18nLib))
44+
$data = (new Generator($config))
4745
->generateFromPath($root, $umd, $withVendor);
4846

49-
50-
5147
$jsFile = base_path() . config('vue-i18n-generator.jsFile');
5248
file_put_contents($jsFile, $data);
5349

src/Generator.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66

77
class Generator
88
{
9+
private $config;
910

1011
private $availableLocales = [];
1112
private $filesToCreate = [];
1213

1314
const VUEX_I18N = 'vuex-i18n';
1415
const VUE_I18N = 'vue-i18n';
1516

16-
private $i18nLib;
17-
1817
/**
1918
* The constructor
2019
*
21-
* @param string $i18nLib
20+
* @param array $config
2221
*/
23-
public function __construct($i18nLib = self::VUE_I18N)
22+
public function __construct($config = [])
2423
{
25-
$this->i18nLib = $i18nLib;
24+
if (!isset($config['i18nLib'])) {
25+
$config['i18nLib'] = self::VUE_I18N;
26+
}
27+
$this->config = $config;
2628
}
2729

2830
/**
@@ -39,6 +41,7 @@ public function generateFromPath($path, $umd = null, $withVendor = false)
3941
}
4042

4143
$locales = [];
44+
$files = [];
4245
$dir = new DirectoryIterator($path);
4346
$jsBody = '';
4447
foreach ($dir as $fileinfo) {
@@ -47,20 +50,27 @@ public function generateFromPath($path, $umd = null, $withVendor = false)
4750
continue;
4851
}
4952

50-
$noExt = $this->removeExtension($fileinfo->getFilename());
53+
$files[] = $fileinfo->getRealPath();
54+
}
55+
}
56+
asort($files);
5157

52-
if ($fileinfo->isDir()) {
53-
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
54-
} else {
55-
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
56-
if ($local === null) continue;
57-
}
58+
foreach ($files as $fileName) {
59+
$fileinfo = new \SplFileInfo($fileName);
5860

59-
if (isset($locales[$noExt])) {
60-
$locales[$noExt] = array_merge($local, $locales[$noExt]);
61-
} else {
62-
$locales[$noExt] = $local;
63-
}
61+
$noExt = $this->removeExtension($fileinfo->getFilename());
62+
63+
if ($fileinfo->isDir()) {
64+
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
65+
} else {
66+
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
67+
if ($local === null) continue;
68+
}
69+
70+
if (isset($locales[$noExt])) {
71+
$locales[$noExt] = array_merge($local, $locales[$noExt]);
72+
} else {
73+
$locales[$noExt] = $local;
6474
}
6575
}
6676

@@ -92,7 +102,7 @@ public function generateMultiple($path, $umd = null)
92102
if (!is_dir($path)) {
93103
throw new Exception('Directory not found: ' . $path);
94104
}
95-
$jsPath = base_path() . config('vue-i18n-generator.jsPath');
105+
$jsPath = base_path() . $this->config['jsPath'];
96106
$locales = [];
97107
$fileToCreate = '';
98108
$createdFiles = '';
@@ -183,8 +193,7 @@ private function allocateLocaleArray($path)
183193
if ($fileinfo->isDir()) {
184194
// Recursivley iterate through subdirs, until everything is allocated.
185195

186-
$data[$fileinfo->getFilename()] =
187-
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
196+
$data[$fileinfo->getFilename()] = $this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
188197
} else {
189198
$noExt = $this->removeExtension($fileinfo->getFilename());
190199
$fileName = $path . '/' . $fileinfo->getFilename();
@@ -194,8 +203,7 @@ private function allocateLocaleArray($path)
194203
continue;
195204
}
196205

197-
$langFiles = config('vue-i18n-generator.langFiles');
198-
if(! is_null($langFiles) && ! empty($langFiles) && ! in_array($noExt, $langFiles)) {
206+
if (!empty($langFiles) && !in_array($noExt, $langFiles)) {
199207
continue;
200208
}
201209

@@ -205,8 +213,8 @@ private function allocateLocaleArray($path)
205213
throw new Exception('Unexpected data while processing ' . $fileName);
206214
continue;
207215
}
208-
if($lastLocale !== false){
209-
$root = realpath(base_path() . config('vue-i18n-generator.langPath') . '/' . $lastLocale);
216+
if ($lastLocale !== false) {
217+
$root = realpath(base_path() . $this->config['langPath'] . '/' . $lastLocale);
210218
$filePath = $this->removeExtension(str_replace('\\', '_', ltrim(str_replace($root, '', realpath($fileName)), '\\')));
211219
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
212220
}
@@ -261,8 +269,7 @@ private function adjustVendor($locales)
261269
}
262270

263271
/**
264-
* Turn Laravel style ":link" into vue-i18n style "{link}" and
265-
* turn Laravel style "|" into vuex-i18n style ":::" when using vuex-i18n.
272+
* Turn Laravel style ":link" into vue-i18n style "{link}" or vuex-i18n style ":::".
266273
*
267274
* @param string $s
268275
* @return string
@@ -273,10 +280,9 @@ private function adjustString($s)
273280
return $s;
274281
}
275282

276-
if ($this->i18nLib === self::VUEX_I18N) {
283+
if ($this->config['i18nLib'] === self::VUEX_I18N) {
277284
$searchPipePattern = '/(\s)*(\|)(\s)*/';
278285
$threeColons = ' ::: ';
279-
280286
$s = preg_replace($searchPipePattern, $threeColons, $s);
281287
}
282288

src/GeneratorProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ class GeneratorProvider extends ServiceProvider
1818
*/
1919
public function boot()
2020
{
21-
$this->app->singleton('vue-i18n.generate', function () {
22-
return new Commands\GenerateInclude;
23-
});
24-
2521
$this->commands(
2622
'vue-i18n.generate'
2723
);
@@ -43,6 +39,9 @@ public function boot()
4339
*/
4440
public function register()
4541
{
42+
$this->app->singleton(Generator::class, function ($app) {
43+
return new Generator($app['config']['vue-i18n-generator']);
44+
});
4645
}
4746

4847
/**

src/config/vue-i18n-generator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
|
5050
| Specify the library you use for localization.
5151
| Options are vue-i18n or vuex-i18n.
52-
| You can also use Generator::VUE_I18N or Generator::VUEX_I18N
5352
|
5453
*/
55-
'i18nLib' => \MartinLindhe\VueInternationalizationGenerator\Generator::VUE_I18N,
54+
'i18nLib' => 'vue-i18n',
5655
];

0 commit comments

Comments
 (0)