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

Commit 63dc695

Browse files
kenfdevmartinlindhe
authored andcommitted
Add format option and JSON format (#74)
This change allows for a new --format option. Available options are `es6`, `umd` and `json`. The option defaults to `es6`. Signed-off-by: Ken Fukuyama <[email protected]>
1 parent 55c2d33 commit 63dc695

File tree

4 files changed

+193
-22
lines changed

4 files changed

+193
-22
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,18 @@ var app = new Vue({
139139
});
140140
```
141141
142-
## UMD module
142+
## Output Formats
143+
144+
You can specify the output formats from `es6`, `umd`, or `json` with the `--format` option. (defaults to `es6`)
145+
146+
```
147+
php artisan vue-i18n:generate --format {es6,umd,json}
148+
```
149+
150+
### Use case example for UMD module
143151
144-
If you want to generate an UMD style export, you can with the `--umd` option
145152
```
146-
php artisan vue-i18n:generate --umd
153+
php artisan vue-i18n:generate --format umd
147154
```
148155
An UMD module can be imported into the browser, build system, node and etc.
149156

src/Commands/GenerateInclude.php

+27-6
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=}';
14+
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6}';
1515

1616
/**
1717
* The console command description.
@@ -23,6 +23,7 @@ class GenerateInclude extends Command
2323
/**
2424
* Execute the console command.
2525
* @return mixed
26+
* @throws \Exception
2627
*/
2728
public function handle()
2829
{
@@ -35,13 +36,23 @@ public function handle()
3536
$withVendor = $this->option('with-vendor');
3637
$fileName = $this->option('file-name');
3738
$langFiles = $this->option('lang-files');
39+
$format = $this->option('format');
40+
41+
if ($umd) {
42+
// if the --umd option is set, set the $format to 'umd'
43+
$format = 'umd';
44+
}
45+
46+
if (!$this->isValidFormat($format)) {
47+
throw new \RuntimeException('Invalid format passed: ' . $format);
48+
}
3849

3950
if ($multipleFiles) {
4051
$files = (new Generator($config))
41-
->generateMultiple($root, $umd);
52+
->generateMultiple($root, $format);
4253

4354
if ($config['showOutputMessages']) {
44-
$this->info("Written to : ".$files);
55+
$this->info("Written to : " . $files);
4556
}
4657

4758
return;
@@ -52,14 +63,14 @@ public function handle()
5263
}
5364

5465
$data = (new Generator($config))
55-
->generateFromPath($root, $umd, $withVendor, $langFiles);
66+
->generateFromPath($root, $format, $withVendor, $langFiles);
5667

5768

5869
$jsFile = $this->getFileName($fileName);
5970
file_put_contents($jsFile, $data);
6071

6172
if ($config['showOutputMessages']) {
62-
$this->info("Written to : ".$jsFile);
73+
$this->info("Written to : " . $jsFile);
6374
}
6475
}
6576

@@ -73,6 +84,16 @@ private function getFileName($fileNameOption)
7384
return base_path() . $fileNameOption;
7485
}
7586

76-
return base_path() . config('vue-i18n-generator.jsFile');
87+
return base_path() . config('vue-i18n-generator.jsFile');
88+
}
89+
90+
/**
91+
* @param string $format
92+
* @return boolean
93+
*/
94+
private function isValidFormat($format)
95+
{
96+
$supportedFormats = ['es6', 'umd', 'json'];
97+
return in_array($format, $supportedFormats);
7798
}
7899
}

src/Generator.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public function __construct($config = [])
3030

3131
/**
3232
* @param string $path
33-
* @param boolean $umd
33+
* @param string $format
3434
* @param boolean $withVendor
3535
* @return string
3636
* @throws Exception
3737
*/
38-
public function generateFromPath($path, $umd = null, $withVendor = false, $langFiles = [])
38+
public function generateFromPath($path, $format = 'es6', $withVendor = false, $langFiles = [])
3939
{
4040
if (!is_dir($path)) {
4141
throw new Exception('Directory not found: ' . $path);
@@ -83,27 +83,31 @@ public function generateFromPath($path, $umd = null, $withVendor = false, $langF
8383
$locales = $this->adjustVendor($locales);
8484

8585
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
86-
86+
8787
if(json_last_error() !== JSON_ERROR_NONE)
8888
{
8989
throw new Exception('Could not generate JSON, error code '.json_last_error());
9090
}
91-
92-
if (!$umd) {
91+
92+
// formats other than 'es6' and 'umd' will become plain JSON
93+
if ($format === 'es6') {
9394
$jsBody = $this->getES6Module($jsonLocales);
94-
} else {
95+
} elseif($format === 'umd') {
9596
$jsBody = $this->getUMDModule($jsonLocales);
97+
} else {
98+
$jsBody = $jsonLocales;
9699
}
100+
97101
return $jsBody;
98102
}
99103

100104
/**
101105
* @param string $path
102-
* @param boolean $umd
106+
* @param string $format
103107
* @return string
104108
* @throws Exception
105109
*/
106-
public function generateMultiple($path, $umd = null)
110+
public function generateMultiple($path, $format = 'es6')
107111
{
108112
if (!is_dir($path)) {
109113
throw new Exception('Directory not found: ' . $path);
@@ -149,10 +153,12 @@ public function generateMultiple($path, $umd = null)
149153
{
150154
throw new Exception('Could not generate JSON, error code '.json_last_error());
151155
}
152-
if (!$umd) {
156+
if ($format === 'es6') {
153157
$jsBody = $this->getES6Module($jsonLocales);
154-
} else {
158+
} elseif($format === 'umd') {
155159
$jsBody = $this->getUMDModule($jsonLocales);
160+
} else {
161+
$jsBody = $jsonLocales;
156162
}
157163

158164
if (!is_dir(dirname($fileToCreate))) {
@@ -270,9 +276,9 @@ private function adjustArray(array $arr)
270276

271277
/**
272278
* Adjus vendor index placement.
273-
*
279+
*
274280
* @param array $locales
275-
*
281+
*
276282
* @return array
277283
*/
278284
private function adjustVendor($locales)

tests/GenerateTest.php

+138-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,143 @@ function testBasic()
8686
$this->destroyLocaleFilesFrom($arr, $root);
8787
}
8888

89+
function testBasicES6Format()
90+
{
91+
$format = 'es6';
92+
93+
$arr = [
94+
'en' => [
95+
'help' => [
96+
'yes' => 'yes',
97+
'no' => 'no',
98+
]
99+
],
100+
'sv' => [
101+
'help' => [
102+
'yes' => 'ja',
103+
'no' => 'nej',
104+
]
105+
]
106+
];
107+
108+
$root = $this->generateLocaleFilesFrom($arr);
109+
$this->assertEquals(
110+
'export default {' . PHP_EOL
111+
. ' "en": {' . PHP_EOL
112+
. ' "help": {' . PHP_EOL
113+
. ' "yes": "yes",' . PHP_EOL
114+
. ' "no": "no"' . PHP_EOL
115+
. ' }' . PHP_EOL
116+
. ' },' . PHP_EOL
117+
. ' "sv": {' . PHP_EOL
118+
. ' "help": {' . PHP_EOL
119+
. ' "yes": "ja",' . PHP_EOL
120+
. ' "no": "nej"' . PHP_EOL
121+
. ' }' . PHP_EOL
122+
. ' }' . PHP_EOL
123+
. '}' . PHP_EOL,
124+
(new Generator([]))->generateFromPath($root, $format));
125+
$this->destroyLocaleFilesFrom($arr, $root);
126+
}
127+
128+
function testBasicWithUMDFormat()
129+
{
130+
$format = 'umd';
131+
$arr = [
132+
'en' => [
133+
'help' => [
134+
'yes' => 'yes',
135+
'no' => 'no',
136+
]
137+
],
138+
'sv' => [
139+
'help' => [
140+
'yes' => 'ja',
141+
'no' => 'nej',
142+
]
143+
]
144+
];
145+
146+
$root = $this->generateLocaleFilesFrom($arr);
147+
$this->assertEquals(
148+
'(function (global, factory) {' . PHP_EOL
149+
. ' typeof exports === \'object\' && typeof module !== \'undefined\' ? module.exports = factory() :' . PHP_EOL
150+
. ' typeof define === \'function\' && define.amd ? define(factory) :' . PHP_EOL
151+
. ' (global.vuei18nLocales = factory());' . PHP_EOL
152+
. '}(this, (function () { \'use strict\';' . PHP_EOL
153+
. ' return {' . PHP_EOL
154+
. ' "en": {' . PHP_EOL
155+
. ' "help": {' . PHP_EOL
156+
. ' "yes": "yes",' . PHP_EOL
157+
. ' "no": "no"' . PHP_EOL
158+
. ' }' . PHP_EOL
159+
. ' },' . PHP_EOL
160+
. ' "sv": {' . PHP_EOL
161+
. ' "help": {' . PHP_EOL
162+
. ' "yes": "ja",' . PHP_EOL
163+
. ' "no": "nej"' . PHP_EOL
164+
. ' }' . PHP_EOL
165+
. ' }' . PHP_EOL
166+
. '}' . PHP_EOL
167+
. PHP_EOL
168+
. '})));',
169+
(new Generator([]))->generateFromPath($root, $format));
170+
$this->destroyLocaleFilesFrom($arr, $root);
171+
}
172+
173+
function testBasicWithJSONFormat()
174+
{
175+
$format = 'json';
176+
$arr = [
177+
'en' => [
178+
'help' => [
179+
'yes' => 'yes',
180+
'no' => 'no',
181+
]
182+
],
183+
'sv' => [
184+
'help' => [
185+
'yes' => 'ja',
186+
'no' => 'nej',
187+
]
188+
]
189+
];
190+
191+
$root = $this->generateLocaleFilesFrom($arr);
192+
$this->assertEquals(
193+
'{' . PHP_EOL
194+
. ' "en": {' . PHP_EOL
195+
. ' "help": {' . PHP_EOL
196+
. ' "yes": "yes",' . PHP_EOL
197+
. ' "no": "no"' . PHP_EOL
198+
. ' }' . PHP_EOL
199+
. ' },' . PHP_EOL
200+
. ' "sv": {' . PHP_EOL
201+
. ' "help": {' . PHP_EOL
202+
. ' "yes": "ja",' . PHP_EOL
203+
. ' "no": "nej"' . PHP_EOL
204+
. ' }' . PHP_EOL
205+
. ' }' . PHP_EOL
206+
. '}' . PHP_EOL,
207+
(new Generator([]))->generateFromPath($root, $format));
208+
$this->destroyLocaleFilesFrom($arr, $root);
209+
}
210+
211+
function testInvalidFormat()
212+
{
213+
$format = 'es5';
214+
$arr = [];
215+
216+
$root = $this->generateLocaleFilesFrom($arr);
217+
try {
218+
(new Generator([]))->generateFromPath($root, $format);
219+
} catch(RuntimeException $e) {
220+
$this->assertEquals('Invalid format passed: ' . $format, $e->getMessage());
221+
222+
}
223+
$this->destroyLocaleFilesFrom($arr, $root);
224+
}
225+
89226
function testBasicWithTranslationString()
90227
{
91228
$arr = [
@@ -171,7 +308,7 @@ function testBasicWithVendor()
171308
. ' }' . PHP_EOL
172309
. ' }' . PHP_EOL
173310
. '}' . PHP_EOL,
174-
(new Generator([]))->generateFromPath($root, false, true));
311+
(new Generator([]))->generateFromPath($root, 'es6', true));
175312

176313
$this->destroyLocaleFilesFrom($arr, $root);
177314
}

0 commit comments

Comments
 (0)