@@ -31,7 +31,7 @@ class ExportLocalizations implements \JsonSerializable
31
31
/**
32
32
* @var string
33
33
*/
34
- protected $ excludePath = DIRECTORY_SEPARATOR . 'vendor ' . DIRECTORY_SEPARATOR ;
34
+ protected $ excludePath = DIRECTORY_SEPARATOR . 'vendor ' . DIRECTORY_SEPARATOR ;
35
35
36
36
/**
37
37
* @var string
@@ -46,41 +46,37 @@ class ExportLocalizations implements \JsonSerializable
46
46
public function export ()
47
47
{
48
48
// Check if value is cached and set array to cached version
49
- if ( Cache::has ( config ( 'laravel-localization.caches.key ' ) ) ) {
50
- $ this ->strings = Cache::get ( config ( 'laravel-localization.caches.key ' ) );
49
+ if (Cache::has (config ('laravel-localization.caches.key ' )) ) {
50
+ $ this ->strings = Cache::get (config ('laravel-localization.caches.key ' ) );
51
51
52
52
return $ this ;
53
53
}
54
54
55
- foreach ( config ( 'laravel-localization.paths.lang_dirs ' ) as $ dir ) {
56
-
55
+ foreach (config ('laravel-localization.paths.lang_dirs ' ) as $ dir ) {
57
56
try {
58
57
59
58
// Collect language files and build array with translations
60
- $ files = $ this ->findLanguageFiles ( $ dir );
59
+ $ files = $ this ->findLanguageFiles ($ dir );
61
60
62
61
// Parse translations and create final array
63
- array_walk ( $ files [ 'lang ' ], [ $ this , 'parseLangFiles ' ], $ dir );
64
- array_walk ( $ files [ 'vendor ' ], [ $ this , 'parseVendorFiles ' ], $ dir );
65
- array_walk ( $ files [ 'json ' ], [ $ this , 'parseJsonFiles ' ], $ dir );
66
-
67
- } catch ( \Exception $ exception ) {
68
-
69
- \Log::critical ( 'Can \'t read lang directory ' . $ dir . ', error: ' . $ exception ->getMessage () );
62
+ array_walk ($ files ['lang ' ], [$ this , 'parseLangFiles ' ], $ dir );
63
+ array_walk ($ files ['vendor ' ], [$ this , 'parseVendorFiles ' ], $ dir );
64
+ array_walk ($ files ['json ' ], [$ this , 'parseJsonFiles ' ], $ dir );
65
+ } catch (\Exception $ exception ) {
66
+ \Log::critical ('Can \'t read lang directory ' .$ dir .', error: ' .$ exception ->getMessage ());
70
67
}
71
-
72
68
}
73
69
74
70
// Trigger event for final translated array
75
- event ( new LaravelLocalizationExported ( $ this ->strings ) );
71
+ event (new LaravelLocalizationExported ($ this ->strings ) );
76
72
77
73
// If timeout > 0 save array to cache
78
- if ( config ( 'laravel-localization.caches.timeout ' , 0 ) > 0 ) {
79
- Cache::store ( config ( 'laravel-localization.caches.driver ' , 'file ' ) )
74
+ if (config ('laravel-localization.caches.timeout ' , 0 ) > 0 ) {
75
+ Cache::store (config ('laravel-localization.caches.driver ' , 'file ' ) )
80
76
->put (
81
- config ( 'laravel-localization.caches.key ' , 'localization.array ' ),
77
+ config ('laravel-localization.caches.key ' , 'localization.array ' ),
82
78
$ this ->strings ,
83
- config ( 'laravel-localization.caches.timeout ' , 60 )
79
+ config ('laravel-localization.caches.timeout ' , 60 )
84
80
);
85
81
}
86
82
@@ -94,58 +90,58 @@ public function export()
94
90
*
95
91
* @return array
96
92
*/
97
- protected function findLanguageFiles ( $ path )
93
+ protected function findLanguageFiles ($ path )
98
94
{
99
95
// Loop through directories
100
- $ dirIterator = new \RecursiveDirectoryIterator ( $ path , \RecursiveDirectoryIterator::SKIP_DOTS );
101
- $ recIterator = new \RecursiveIteratorIterator ( $ dirIterator );
96
+ $ dirIterator = new \RecursiveDirectoryIterator ($ path , \RecursiveDirectoryIterator::SKIP_DOTS );
97
+ $ recIterator = new \RecursiveIteratorIterator ($ dirIterator );
102
98
103
99
// Fetch only php files - skip others
104
100
$ phpFiles = array_values (
105
- array_map ( 'current ' ,
101
+ array_map ('current ' ,
106
102
iterator_to_array (
107
- new \RegexIterator ( $ recIterator , $ this ->phpRegex , \RecursiveRegexIterator::GET_MATCH )
103
+ new \RegexIterator ($ recIterator , $ this ->phpRegex , \RecursiveRegexIterator::GET_MATCH )
108
104
)
109
105
)
110
106
);
111
107
112
108
$ jsonFiles = array_values (
113
- array_map ( 'current ' ,
109
+ array_map ('current ' ,
114
110
iterator_to_array (
115
- new \RegexIterator ( $ recIterator , $ this ->jsonRegex , \RecursiveRegexIterator::GET_MATCH )
111
+ new \RegexIterator ($ recIterator , $ this ->jsonRegex , \RecursiveRegexIterator::GET_MATCH )
116
112
)
117
113
)
118
114
);
119
115
120
- $ files = array_merge ( $ phpFiles , $ jsonFiles );
116
+ $ files = array_merge ($ phpFiles , $ jsonFiles );
121
117
122
118
// Sort array by filepath
123
- sort ( $ files );
119
+ sort ($ files );
124
120
125
121
// Remove full path from items
126
- array_walk ( $ files , function ( &$ item ) use ( $ path ) {
127
- $ item = str_replace ( $ path , '' , $ item );
128
- } );
122
+ array_walk ($ files , function (&$ item ) use ($ path ) {
123
+ $ item = str_replace ($ path , '' , $ item );
124
+ });
129
125
130
126
// Fetch non-vendor files from filtered php files
131
- $ nonVendorFiles = array_filter ( $ files , function ( $ file ) {
132
- return strpos ( $ file , $ this ->excludePath ) === false && strpos ( $ file , '.json ' ) === false ;
133
- } );
127
+ $ nonVendorFiles = array_filter ($ files , function ($ file ) {
128
+ return strpos ($ file , $ this ->excludePath ) === false && strpos ($ file , '.json ' ) === false ;
129
+ });
134
130
135
131
// Fetch vendor files from filtered php files
136
- $ vendorFiles = array_filter ( array_diff ( $ files , $ nonVendorFiles ), function ( $ file ) {
137
- return strpos ( $ file , 'json ' ) === false ;
138
- } );
132
+ $ vendorFiles = array_filter (array_diff ($ files , $ nonVendorFiles ), function ($ file ) {
133
+ return strpos ($ file , 'json ' ) === false ;
134
+ });
139
135
140
136
// Fetch .json files from filtered files
141
- $ jsonFiles = array_filter ( $ files , function ( $ file ) {
142
- return strpos ( $ file , '.json ' ) !== false ;
143
- } );
137
+ $ jsonFiles = array_filter ($ files , function ($ file ) {
138
+ return strpos ($ file , '.json ' ) !== false ;
139
+ });
144
140
145
141
return [
146
- 'lang ' => array_values ( $ nonVendorFiles ),
147
- 'vendor ' => array_values ( $ vendorFiles ),
148
- 'json ' => array_values ( $ jsonFiles ),
142
+ 'lang ' => array_values ($ nonVendorFiles ),
143
+ 'vendor ' => array_values ($ vendorFiles ),
144
+ 'json ' => array_values ($ jsonFiles ),
149
145
];
150
146
}
151
147
@@ -178,43 +174,43 @@ public function toArray()
178
174
*
179
175
* @return array
180
176
*/
181
- public function toFlat ( $ prefix = '. ' )
177
+ public function toFlat ($ prefix = '. ' )
182
178
{
183
- $ results = [];
184
- $ default_locale = config ( 'laravel-localization.js.default_locale ' );
179
+ $ results = [];
180
+ $ default_locale = config ('laravel-localization.js.default_locale ' );
185
181
$ default_json_strings = null ;
186
182
187
- foreach ( $ this ->strings as $ lang => $ strings ) {
188
- if ( $ lang !== 'json ' ) {
189
- foreach ( $ strings as $ lang_array => $ lang_messages ) {
190
- $ key = $ lang . $ prefix . $ lang_array ;
191
- $ results [ $ key ] = $ lang_messages ;
183
+ foreach ($ this ->strings as $ lang => $ strings ) {
184
+ if ($ lang !== 'json ' ) {
185
+ foreach ($ strings as $ lang_array => $ lang_messages ) {
186
+ $ key = $ lang. $ prefix. $ lang_array ;
187
+ $ results [$ key ] = $ lang_messages ;
192
188
}
193
189
} else {
194
- foreach ( $ strings as $ json_lang => $ json_strings ) {
195
- $ key = $ json_lang . $ prefix . '__JSON__ ' ;
196
- if ( array_key_exists ( $ key , $ results ) ) {
197
- $ results [ $ key ] = $ json_strings ;
190
+ foreach ($ strings as $ json_lang => $ json_strings ) {
191
+ $ key = $ json_lang. $ prefix. '__JSON__ ' ;
192
+ if (array_key_exists ($ key , $ results) ) {
193
+ $ results [$ key ] = $ json_strings ;
198
194
} else {
199
- $ results [ $ key ] = $ json_strings ;
195
+ $ results [$ key ] = $ json_strings ;
200
196
}
201
197
202
198
// Pick only the first $json_strings
203
- if ( ! $ default_json_strings ) {
199
+ if (! $ default_json_strings ) {
204
200
$ default_json_strings = $ json_strings ;
205
201
}
206
202
}
207
203
}
208
204
}
209
205
210
206
// Create a JSON key value pair for the default language
211
- $ default_key = $ default_locale . $ prefix . '__JSON__ ' ;
212
- if ( ! array_key_exists ( $ default_key , $ results ) ) {
207
+ $ default_key = $ default_locale. $ prefix. '__JSON__ ' ;
208
+ if (! array_key_exists ($ default_key , $ results) ) {
213
209
$ buffer = array_keys (
214
- $ default_json_strings ? get_object_vars ( $ default_json_strings ) : []
210
+ $ default_json_strings ? get_object_vars ($ default_json_strings ) : []
215
211
);
216
212
217
- $ results [ $ default_key ] = array_combine ( $ buffer , $ buffer );
213
+ $ results [$ default_key ] = array_combine ($ buffer , $ buffer );
218
214
}
219
215
220
216
return $ results ;
@@ -227,39 +223,35 @@ public function toFlat( $prefix = '.' )
227
223
*/
228
224
public function toCollection ()
229
225
{
230
- return collect ( $ this ->strings );
226
+ return collect ($ this ->strings );
231
227
}
232
228
233
229
/**
234
230
* Method to parse language files.
235
231
*
236
232
* @param string $file
237
233
*/
238
- protected function parseLangFiles ( $ file , $ key , $ dir )
234
+ protected function parseLangFiles ($ file , $ key , $ dir )
239
235
{
240
236
// Base package name without file ending
241
- $ packageName = basename ( $ file , '.php ' );
237
+ $ packageName = basename ($ file , '.php ' );
242
238
243
239
// Get package, language and file contents from language file
244
240
// /<language_code>/(<package/)<filename>.php
245
- $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 1 ];
246
- $ fileContents = require $ dir . DIRECTORY_SEPARATOR . $ file ;
241
+ $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 1 ];
242
+ $ fileContents = require $ dir. DIRECTORY_SEPARATOR . $ file ;
247
243
248
244
// Check if language already exists in array
249
- if ( array_key_exists ( $ language , $ this ->strings ) ) {
250
-
251
- if ( array_key_exists ( $ packageName , $ this ->strings [ $ language ] ) ) {
252
-
253
- $ this ->strings [ $ language ][ $ packageName ] =
254
- array_replace_recursive ( (array ) $ this ->strings [ $ language ][ $ packageName ], (array )
255
- $ fileContents );
245
+ if (array_key_exists ($ language , $ this ->strings )) {
246
+ if (array_key_exists ($ packageName , $ this ->strings [$ language ])) {
247
+ $ this ->strings [$ language ][$ packageName ] =
248
+ array_replace_recursive ((array ) $ this ->strings [$ language ][$ packageName ], (array )
249
+ $ fileContents );
256
250
} else {
257
-
258
- $ this ->strings [ $ language ][ $ packageName ] = $ fileContents ;
251
+ $ this ->strings [$ language ][$ packageName ] = $ fileContents ;
259
252
}
260
-
261
253
} else {
262
- $ this ->strings [ $ language ] = [
254
+ $ this ->strings [$ language ] = [
263
255
$ packageName => $ fileContents ,
264
256
];
265
257
}
@@ -270,39 +262,34 @@ protected function parseLangFiles( $file, $key, $dir )
270
262
*
271
263
* @param string $file
272
264
*/
273
- protected function parseVendorFiles ( $ file , $ key , $ dir )
265
+ protected function parseVendorFiles ($ file , $ key , $ dir )
274
266
{
275
267
// Base package name without file ending
276
- $ packageName = basename ( $ file , '.php ' );
268
+ $ packageName = basename ($ file , '.php ' );
277
269
278
270
// Get package, language and file contents from language file
279
271
// /vendor/<package>/<language_code>/<filename>.php
280
- $ package = explode ( DIRECTORY_SEPARATOR , $ file )[ 2 ];
281
- $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 3 ];
282
- $ fileContents = require $ dir . DIRECTORY_SEPARATOR . $ file ;
272
+ $ package = explode (DIRECTORY_SEPARATOR , $ file)[ 2 ];
273
+ $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 3 ];
274
+ $ fileContents = require $ dir. DIRECTORY_SEPARATOR . $ file ;
283
275
284
276
// Check if language already exists in array
285
- if ( array_key_exists ( $ language , $ this ->strings ) ) {
277
+ if (array_key_exists ($ language , $ this ->strings ) ) {
286
278
// Check if package already exists in language
287
- if ( array_key_exists ( $ package , $ this ->strings [ $ language ] ) ) {
288
-
289
- if ( array_key_exists ( $ packageName , $ this ->strings [ $ language ][ $ package ] ) ) {
290
-
291
- $ this ->strings [ $ language ][ $ package ][ $ packageName ] =
292
- array_replace_recursive ( (array ) $ this ->strings [ $ language ][ $ package ][ $ packageName ],
279
+ if (array_key_exists ($ package , $ this ->strings [$ language ])) {
280
+ if (array_key_exists ($ packageName , $ this ->strings [$ language ][$ package ])) {
281
+ $ this ->strings [$ language ][$ package ][$ packageName ] =
282
+ array_replace_recursive ((array ) $ this ->strings [$ language ][$ package ][$ packageName ],
293
283
(array )
294
- $ fileContents );
295
-
284
+ $ fileContents );
296
285
} else {
297
-
298
- $ this ->strings [ $ language ][ $ package ][ $ packageName ] = $ fileContents ;
286
+ $ this ->strings [$ language ][$ package ][$ packageName ] = $ fileContents ;
299
287
}
300
288
} else {
301
-
302
- $ this ->strings [ $ language ][ $ package ] = [ $ packageName => $ fileContents ];
289
+ $ this ->strings [$ language ][$ package ] = [$ packageName => $ fileContents ];
303
290
}
304
291
} else {
305
- $ this ->strings [ $ language ] = [
292
+ $ this ->strings [$ language ] = [
306
293
307
294
$ package => [
308
295
@@ -312,28 +299,25 @@ protected function parseVendorFiles( $file, $key, $dir )
312
299
}
313
300
}
314
301
315
- protected function parseJsonFiles ( $ file , $ key , $ dir )
302
+ protected function parseJsonFiles ($ file , $ key , $ dir )
316
303
{
317
304
// Base package name without file ending
318
- $ language = basename ( $ file , '.json ' );
305
+ $ language = basename ($ file , '.json ' );
319
306
320
307
// Get package, language and file contents from language file
321
308
// /<language_code>/(<package/)<filename>.php
322
- $ fileContents = json_decode ( file_get_contents ( $ dir . $ file ) );
309
+ $ fileContents = json_decode (file_get_contents ($ dir. $ file) );
323
310
324
311
// Check if language already exists in array
325
- if ( array_key_exists ( 'json ' , $ this ->strings ) ) {
326
-
327
- if ( array_key_exists ( $ language , $ this ->strings [ 'json ' ] ) ) {
328
-
329
- $ this ->strings [ 'json ' ][ $ language ] =
330
- array_replace_recursive ( (array ) $ this ->strings [ 'json ' ][ $ language ], (array ) $ fileContents );
331
-
312
+ if (array_key_exists ('json ' , $ this ->strings )) {
313
+ if (array_key_exists ($ language , $ this ->strings ['json ' ])) {
314
+ $ this ->strings ['json ' ][$ language ] =
315
+ array_replace_recursive ((array ) $ this ->strings ['json ' ][$ language ], (array ) $ fileContents );
332
316
} else {
333
- $ this ->strings [ 'json ' ][ $ language ] = $ fileContents ;
317
+ $ this ->strings ['json ' ][ $ language ] = $ fileContents ;
334
318
}
335
319
} else {
336
- $ this ->strings [ 'json ' ] = [
320
+ $ this ->strings ['json ' ] = [
337
321
338
322
$ language => $ fileContents ,
339
323
];
0 commit comments