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

Commit f546c3f

Browse files
murrantmartinlindhe
authored andcommitted
Fix multi-file output (#96)
1 parent 0f88ac8 commit f546c3f

File tree

3 files changed

+139
-40
lines changed

3 files changed

+139
-40
lines changed

src/Commands/GenerateInclude.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function handle()
5050

5151
if ($multipleFiles || $multipleLocales) {
5252
$files = (new Generator($config))
53-
->generateMultiple($root, $format, $multipleLocales);
53+
->generateMultiple($root, $format);
5454

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

src/Generator.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
8484
}
8585

8686
if (isset($locales[$noExt])) {
87-
$locales[$noExt] = array_merge($local, $locales[$noExt]);
87+
$locales[$noExt] = array_merge_recursive($local, $locales[$noExt]);
8888
} else {
8989
$locales[$noExt] = $local;
9090
}
@@ -93,22 +93,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
9393

9494
$locales = $this->adjustVendor($locales);
9595

96-
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
97-
98-
if (json_last_error() !== JSON_ERROR_NONE) {
99-
throw new Exception('Could not generate JSON, error code '.json_last_error());
100-
}
101-
102-
// formats other than 'es6' and 'umd' will become plain JSON
103-
if ($format === 'es6') {
104-
$jsBody = $this->getES6Module($jsonLocales);
105-
} elseif ($format === 'umd') {
106-
$jsBody = $this->getUMDModule($jsonLocales);
107-
} else {
108-
$jsBody = $jsonLocales;
109-
}
110-
111-
return $jsBody;
96+
return $this->encodeJson($locales, $format);
11297
}
11398

11499
/**
@@ -117,7 +102,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
117102
* @return string
118103
* @throws Exception
119104
*/
120-
public function generateMultiple($path, $format = 'es6', $multiLocales = false)
105+
public function generateMultiple($path, $format = 'es6')
121106
{
122107
if (!is_dir($path)) {
123108
throw new Exception('Directory not found: ' . $path);
@@ -142,7 +127,7 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
142127
$this->availableLocales[] = $noExt;
143128
}
144129
if ($fileinfo->isDir()) {
145-
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
130+
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
146131
} else {
147132
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
148133
if ($local === null) continue;
@@ -156,20 +141,10 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
156141
}
157142
}
158143
}
159-
foreach ($this->filesToCreate as $fileName => $data) {
144+
foreach ($locales as $fileName => $data) {
160145
$fileToCreate = $jsPath . $fileName . '.js';
161146
$createdFiles .= $fileToCreate . PHP_EOL;
162-
$jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
163-
if (json_last_error() !== JSON_ERROR_NONE) {
164-
throw new Exception('Could not generate JSON, error code '.json_last_error());
165-
}
166-
if ($format === 'es6') {
167-
$jsBody = $this->getES6Module($jsonLocales);
168-
} elseif ($format === 'umd') {
169-
$jsBody = $this->getUMDModule($jsonLocales);
170-
} else {
171-
$jsBody = $jsonLocales;
172-
}
147+
$jsBody = $this->encodeJson([$fileName => $data], $format);
173148

174149
if (!is_dir(dirname($fileToCreate))) {
175150
mkdir(dirname($fileToCreate), 0777, true);
@@ -203,7 +178,7 @@ private function allocateLocaleJSON($path)
203178
* @param string $path
204179
* @return array
205180
*/
206-
private function allocateLocaleArray($path, $multiLocales = false)
181+
private function allocateLocaleArray($path)
207182
{
208183
$data = [];
209184
$dir = new DirectoryIterator($path);
@@ -243,11 +218,6 @@ private function allocateLocaleArray($path, $multiLocales = false)
243218
if($filePath[0] === DIRECTORY_SEPARATOR) {
244219
$filePath = substr($filePath, 1);
245220
}
246-
if ($multiLocales) {
247-
$this->filesToCreate[$lastLocale][$lastLocale][$filePath] = $this->adjustArray($tmp);
248-
} else {
249-
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
250-
}
251221
}
252222

253223
$data[$noExt] = $this->adjustArray($tmp);
@@ -404,4 +374,19 @@ private function getES6Module($body)
404374
{
405375
return "export default {$body}";
406376
}
377+
378+
private function encodeJson($data, $format = 'es6')
379+
{
380+
$jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
381+
if (json_last_error() !== JSON_ERROR_NONE) {
382+
throw new Exception('Could not generate JSON, error code '.json_last_error());
383+
}
384+
if ($format === 'es6') {
385+
return $this->getES6Module($jsonLocales);
386+
} elseif ($format === 'umd') {
387+
return $this->getUMDModule($jsonLocales);
388+
}
389+
390+
return $jsonLocales;
391+
}
407392
}

tests/GenerateTest.php

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44

55
class GenerateTest extends \PHPUnit_Framework_TestCase
66
{
7-
private function generateLocaleFilesFrom(array $arr)
7+
/**
8+
* @return string
9+
*/
10+
private function getRootDir()
811
{
912
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand());
10-
13+
1114
if (!is_dir($root)) {
1215
mkdir($root, 0777, true);
1316
}
17+
return $root;
18+
}
19+
20+
private function generateLocaleFilesFrom(array $arr, $root = null)
21+
{
22+
$root = $root ?: $this->getRootDir();
1423

1524
foreach ($arr as $key => $val) {
1625

@@ -27,6 +36,18 @@ private function generateLocaleFilesFrom(array $arr)
2736
return $root;
2837
}
2938

39+
private function generateJsonLocaleFilesFrom(array $arr, $root = null)
40+
{
41+
$root = $root ?: $this->getRootDir();
42+
43+
foreach ($arr as $lang => $data) {
44+
$outFile = $root . '/' . $lang . '.json';
45+
file_put_contents($outFile, json_encode($data));
46+
}
47+
48+
return $root;
49+
}
50+
3051
private function destroyLocaleFilesFrom(array $arr, $root)
3152
{
3253
foreach ($arr as $key => $val) {
@@ -38,6 +59,11 @@ private function destroyLocaleFilesFrom(array $arr, $root)
3859
}
3960
}
4061

62+
$jsonFile = $root . '/'. $key . '.json';
63+
if (file_exists($jsonFile)) {
64+
unlink($jsonFile);
65+
}
66+
4167
if (is_dir($root . '/' . $key)) {
4268
rmdir($root . '/' . $key);
4369
}
@@ -86,6 +112,43 @@ function testBasic()
86112
$this->destroyLocaleFilesFrom($arr, $root);
87113
}
88114

115+
function testBasicJsonFiles()
116+
{
117+
$arr = [
118+
'en' => [
119+
'help' => [
120+
'yes' => 'yes',
121+
'no' => 'no',
122+
]
123+
],
124+
'sv' => [
125+
'help' => [
126+
'yes' => 'ja',
127+
'no' => 'nej',
128+
]
129+
]
130+
];
131+
132+
$root = $this->generateJsonLocaleFilesFrom($arr);
133+
$this->assertEquals(
134+
'export default {' . PHP_EOL
135+
. ' "en": {' . PHP_EOL
136+
. ' "help": {' . PHP_EOL
137+
. ' "yes": "yes",' . PHP_EOL
138+
. ' "no": "no"' . PHP_EOL
139+
. ' }' . PHP_EOL
140+
. ' },' . PHP_EOL
141+
. ' "sv": {' . PHP_EOL
142+
. ' "help": {' . PHP_EOL
143+
. ' "yes": "ja",' . PHP_EOL
144+
. ' "no": "nej"' . PHP_EOL
145+
. ' }' . PHP_EOL
146+
. ' }' . PHP_EOL
147+
. '}' . PHP_EOL,
148+
(new Generator([]))->generateFromPath($root));
149+
$this->destroyLocaleFilesFrom($arr, $root);
150+
}
151+
89152
function testBasicES6Format()
90153
{
91154
$format = 'es6';
@@ -544,4 +607,55 @@ function testPluralization()
544607

545608
$this->destroyLocaleFilesFrom($arr, $root);
546609
}
610+
611+
function testBothJsonAndPhpFiles()
612+
{
613+
$root = $this->getRootDir();
614+
$jsonArr = [
615+
'en' => [
616+
'help' => [
617+
'no' => 'no',
618+
]
619+
],
620+
'sv' => [
621+
'help' => [
622+
'no' => 'nej',
623+
]
624+
]
625+
];
626+
$this->generateJsonLocaleFilesFrom($jsonArr, $root);
627+
628+
$phpArr = [
629+
'en' => [
630+
'help' => [
631+
'yes' => 'yes',
632+
]
633+
],
634+
'sv' => [
635+
'help' => [
636+
'yes' => 'ja',
637+
]
638+
]
639+
];
640+
$this->generateLocaleFilesFrom($phpArr, $root);
641+
642+
$this->assertEquals(
643+
'export default {' . PHP_EOL
644+
. ' "en": {' . PHP_EOL
645+
. ' "help": {' . PHP_EOL
646+
. ' "no": "no",' . PHP_EOL
647+
. ' "yes": "yes"' . PHP_EOL
648+
. ' }' . PHP_EOL
649+
. ' },' . PHP_EOL
650+
. ' "sv": {' . PHP_EOL
651+
. ' "help": {' . PHP_EOL
652+
. ' "no": "nej",' . PHP_EOL
653+
. ' "yes": "ja"' . PHP_EOL
654+
. ' }' . PHP_EOL
655+
. ' }' . PHP_EOL
656+
. '}' . PHP_EOL,
657+
(new Generator([]))->generateFromPath($root));
658+
659+
$this->destroyLocaleFilesFrom($jsonArr, $root);
660+
}
547661
}

0 commit comments

Comments
 (0)