@@ -84,7 +84,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
84
84
}
85
85
86
86
if (isset ($ locales [$ noExt ])) {
87
- $ locales [$ noExt ] = array_merge_recursive ($ local , $ locales [$ noExt ]);
87
+ $ locales [$ noExt ] = array_merge ($ local , $ locales [$ noExt ]);
88
88
} else {
89
89
$ locales [$ noExt ] = $ local ;
90
90
}
@@ -93,7 +93,22 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
93
93
94
94
$ locales = $ this ->adjustVendor ($ locales );
95
95
96
- return $ this ->encodeJson ($ locales , $ format );
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 ;
97
112
}
98
113
99
114
/**
@@ -102,7 +117,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
102
117
* @return string
103
118
* @throws Exception
104
119
*/
105
- public function generateMultiple ($ path , $ format = 'es6 ' )
120
+ public function generateMultiple ($ path , $ format = 'es6 ' , $ multiLocales = false )
106
121
{
107
122
if (!is_dir ($ path )) {
108
123
throw new Exception ('Directory not found: ' . $ path );
@@ -127,7 +142,7 @@ public function generateMultiple($path, $format = 'es6')
127
142
$ this ->availableLocales [] = $ noExt ;
128
143
}
129
144
if ($ fileinfo ->isDir ()) {
130
- $ local = $ this ->allocateLocaleArray ($ fileinfo ->getRealPath ());
145
+ $ local = $ this ->allocateLocaleArray ($ fileinfo ->getRealPath (), $ multiLocales );
131
146
} else {
132
147
$ local = $ this ->allocateLocaleJSON ($ fileinfo ->getRealPath ());
133
148
if ($ local === null ) continue ;
@@ -141,10 +156,20 @@ public function generateMultiple($path, $format = 'es6')
141
156
}
142
157
}
143
158
}
144
- foreach ($ locales as $ fileName => $ data ) {
159
+ foreach ($ this -> filesToCreate as $ fileName => $ data ) {
145
160
$ fileToCreate = $ jsPath . $ fileName . '.js ' ;
146
161
$ createdFiles .= $ fileToCreate . PHP_EOL ;
147
- $ jsBody = $ this ->encodeJson ([$ fileName => $ data ], $ format );
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
+ }
148
173
149
174
if (!is_dir (dirname ($ fileToCreate ))) {
150
175
mkdir (dirname ($ fileToCreate ), 0777 , true );
@@ -178,7 +203,7 @@ private function allocateLocaleJSON($path)
178
203
* @param string $path
179
204
* @return array
180
205
*/
181
- private function allocateLocaleArray ($ path )
206
+ private function allocateLocaleArray ($ path, $ multiLocales = false )
182
207
{
183
208
$ data = [];
184
209
$ dir = new DirectoryIterator ($ path );
@@ -218,6 +243,11 @@ private function allocateLocaleArray($path)
218
243
if ($ filePath [0 ] === DIRECTORY_SEPARATOR ) {
219
244
$ filePath = substr ($ filePath , 1 );
220
245
}
246
+ if ($ multiLocales ) {
247
+ $ this ->filesToCreate [$ lastLocale ][$ lastLocale ][$ filePath ] = $ this ->adjustArray ($ tmp );
248
+ } else {
249
+ $ this ->filesToCreate [$ filePath ][$ lastLocale ] = $ this ->adjustArray ($ tmp );
250
+ }
221
251
}
222
252
223
253
$ data [$ noExt ] = $ this ->adjustArray ($ tmp );
@@ -374,19 +404,4 @@ private function getES6Module($body)
374
404
{
375
405
return "export default {$ body }" ;
376
406
}
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
- }
392
407
}
0 commit comments