@@ -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,30 +46,30 @@ 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
55
// Collect language files and build array with translations
56
- $ files = $ this ->findLanguageFiles ( resource_path ( 'lang ' ) );
56
+ $ files = $ this ->findLanguageFiles (resource_path ('lang ' ) );
57
57
58
58
// Parse translations and create final array
59
- array_walk ( $ files [ 'lang ' ], [ $ this , 'parseLangFiles ' ] );
60
- array_walk ( $ files [ 'vendor ' ], [ $ this , 'parseVendorFiles ' ] );
61
- array_walk ( $ files [ 'json ' ], [ $ this , 'parseJsonFiles ' ] );
59
+ array_walk ($ files ['lang ' ], [$ this , 'parseLangFiles ' ] );
60
+ array_walk ($ files ['vendor ' ], [$ this , 'parseVendorFiles ' ] );
61
+ array_walk ($ files ['json ' ], [$ this , 'parseJsonFiles ' ] );
62
62
63
63
// Trigger event for final translated array
64
- event ( new LaravelLocalizationExported ( $ this ->strings ) );
64
+ event (new LaravelLocalizationExported ($ this ->strings ) );
65
65
66
66
// If timeout > 0 save array to cache
67
- if ( config ( 'laravel-localization.caches.timeout ' , 0 ) > 0 ) {
68
- Cache::store ( config ( 'laravel-localization.caches.driver ' , 'file ' ) )
67
+ if (config ('laravel-localization.caches.timeout ' , 0 ) > 0 ) {
68
+ Cache::store (config ('laravel-localization.caches.driver ' , 'file ' ) )
69
69
->put (
70
- config ( 'laravel-localization.caches.key ' , 'localization.array ' ),
70
+ config ('laravel-localization.caches.key ' , 'localization.array ' ),
71
71
$ this ->strings ,
72
- config ( 'laravel-localization.caches.timeout ' , 60 )
72
+ config ('laravel-localization.caches.timeout ' , 60 )
73
73
);
74
74
}
75
75
@@ -83,61 +83,58 @@ public function export()
83
83
*
84
84
* @return array
85
85
*/
86
- protected function findLanguageFiles ( $ path )
86
+ protected function findLanguageFiles ($ path )
87
87
{
88
88
// Loop through directories
89
- $ dirIterator = new \RecursiveDirectoryIterator ( $ path , \RecursiveDirectoryIterator::SKIP_DOTS );
90
- $ recIterator = new \RecursiveIteratorIterator ( $ dirIterator );
89
+ $ dirIterator = new \RecursiveDirectoryIterator ($ path , \RecursiveDirectoryIterator::SKIP_DOTS );
90
+ $ recIterator = new \RecursiveIteratorIterator ($ dirIterator );
91
91
92
92
// Fetch only php files - skip others
93
93
$ phpFiles = array_values (
94
- array_map ( 'current ' ,
94
+ array_map ('current ' ,
95
95
iterator_to_array (
96
- new \RegexIterator ( $ recIterator , $ this ->phpRegex , \RecursiveRegexIterator::GET_MATCH )
96
+ new \RegexIterator ($ recIterator , $ this ->phpRegex , \RecursiveRegexIterator::GET_MATCH )
97
97
)
98
98
)
99
99
);
100
100
101
101
$ jsonFiles = array_values (
102
- array_map ( 'current ' ,
102
+ array_map ('current ' ,
103
103
iterator_to_array (
104
- new \RegexIterator ( $ recIterator , $ this ->jsonRegex , \RecursiveRegexIterator::GET_MATCH )
104
+ new \RegexIterator ($ recIterator , $ this ->jsonRegex , \RecursiveRegexIterator::GET_MATCH )
105
105
)
106
106
)
107
107
);
108
108
109
- $ files = array_merge ( $ phpFiles , $ jsonFiles );
110
-
109
+ $ files = array_merge ($ phpFiles , $ jsonFiles );
111
110
112
111
// Sort array by filepath
113
- sort ( $ files );
112
+ sort ($ files );
114
113
115
114
// Remove full path from items
116
- array_walk ( $ files , function ( &$ item ) {
117
- $ item = str_replace ( resource_path ( 'lang ' ), '' , $ item );
118
- } );
115
+ array_walk ($ files , function (&$ item ) {
116
+ $ item = str_replace (resource_path ('lang ' ), '' , $ item );
117
+ });
119
118
120
119
// Fetch non-vendor files from filtered php files
121
- $ nonVendorFiles = array_filter ( $ files , function ( $ file ) {
122
- return strpos ( $ file , $ this ->excludePath ) === false && strpos ( $ file , '.json ' ) === false ;
123
- } );
120
+ $ nonVendorFiles = array_filter ($ files , function ($ file ) {
121
+ return strpos ($ file , $ this ->excludePath ) === false && strpos ($ file , '.json ' ) === false ;
122
+ });
124
123
125
124
// Fetch vendor files from filtered php files
126
- $ vendorFiles = array_filter ( array_diff ( $ files , $ nonVendorFiles ), function ( $ file ) {
127
-
128
- return strpos ( $ file , 'json ' ) === false ;
129
- } );
125
+ $ vendorFiles = array_filter (array_diff ($ files , $ nonVendorFiles ), function ($ file ) {
126
+ return strpos ($ file , 'json ' ) === false ;
127
+ });
130
128
131
129
// Fetch .json files from filtered files
132
- $ jsonFiles = array_filter ( $ files , function ( $ file ) {
133
- return strpos ( $ file , '.json ' ) !== false ;
134
- } );
135
-
130
+ $ jsonFiles = array_filter ($ files , function ($ file ) {
131
+ return strpos ($ file , '.json ' ) !== false ;
132
+ });
136
133
137
134
return [
138
- 'lang ' => array_values ( $ nonVendorFiles ),
139
- 'vendor ' => array_values ( $ vendorFiles ),
140
- 'json ' => array_values ( $ jsonFiles ),
135
+ 'lang ' => array_values ($ nonVendorFiles ),
136
+ 'vendor ' => array_values ($ vendorFiles ),
137
+ 'json ' => array_values ($ jsonFiles ),
141
138
];
142
139
}
143
140
@@ -170,30 +167,22 @@ public function toArray()
170
167
*
171
168
* @return array
172
169
*/
173
- public function toFlat ( $ prefix = '. ' )
170
+ public function toFlat ($ prefix = '. ' )
174
171
{
175
172
$ results = [];
176
- foreach ( $ this ->strings as $ lang => $ strings ) {
177
-
178
- if ( $ lang !== 'json ' ) {
179
-
180
- foreach ( $ strings as $ lang_array => $ lang_messages ) {
181
- $ key = $ lang . $ prefix . $ lang_array ;
182
- $ results [ $ key ] = $ lang_messages ;
173
+ foreach ($ this ->strings as $ lang => $ strings ) {
174
+ if ($ lang !== 'json ' ) {
175
+ foreach ($ strings as $ lang_array => $ lang_messages ) {
176
+ $ key = $ lang .$ prefix .$ lang_array ;
177
+ $ results [$ key ] = $ lang_messages ;
183
178
}
184
-
185
179
} else {
186
-
187
- foreach ( $ strings as $ json_lang => $ json_strings ) {
188
-
189
- $ key = $ json_lang . $ prefix . '__JSON__ ' ;
190
- if ( array_key_exists ( $ key , $ results ) ) {
191
-
192
- $ results [ $ key ] = $ json_strings ;
193
-
180
+ foreach ($ strings as $ json_lang => $ json_strings ) {
181
+ $ key = $ json_lang .$ prefix .'__JSON__ ' ;
182
+ if (array_key_exists ($ key , $ results )) {
183
+ $ results [$ key ] = $ json_strings ;
194
184
} else {
195
-
196
- $ results [ $ key ] = $ json_strings ;
185
+ $ results [$ key ] = $ json_strings ;
197
186
}
198
187
/*foreach ( $json_strings as $jsLang => $json_messages ) {
199
188
$key = 'json' . $prefix . $json_lang . $prefix . $jsLang;
@@ -213,29 +202,29 @@ public function toFlat( $prefix = '.' )
213
202
*/
214
203
public function toCollection ()
215
204
{
216
- return collect ( $ this ->strings );
205
+ return collect ($ this ->strings );
217
206
}
218
207
219
208
/**
220
209
* Method to parse language files.
221
210
*
222
211
* @param string $file
223
212
*/
224
- protected function parseLangFiles ( $ file )
213
+ protected function parseLangFiles ($ file )
225
214
{
226
215
// Base package name without file ending
227
- $ packageName = basename ( $ file , '.php ' );
216
+ $ packageName = basename ($ file , '.php ' );
228
217
229
218
// Get package, language and file contents from language file
230
219
// /<language_code>/(<package/)<filename>.php
231
- $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 1 ];
232
- $ fileContents = require resource_path ( 'lang ' ) . DIRECTORY_SEPARATOR . $ file ;
220
+ $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 1 ];
221
+ $ fileContents = require resource_path ('lang ' ). DIRECTORY_SEPARATOR . $ file ;
233
222
234
223
// Check if language already exists in array
235
- if ( array_key_exists ( $ language , $ this ->strings ) ) {
236
- $ this ->strings [ $ language ][ $ packageName ] = $ fileContents ;
224
+ if (array_key_exists ($ language , $ this ->strings ) ) {
225
+ $ this ->strings [$ language][ $ packageName ] = $ fileContents ;
237
226
} else {
238
- $ this ->strings [ $ language ] = [
227
+ $ this ->strings [$ language ] = [
239
228
$ packageName => $ fileContents ,
240
229
];
241
230
}
@@ -246,27 +235,27 @@ protected function parseLangFiles( $file )
246
235
*
247
236
* @param string $file
248
237
*/
249
- protected function parseVendorFiles ( $ file )
238
+ protected function parseVendorFiles ($ file )
250
239
{
251
240
// Base package name without file ending
252
- $ packageName = basename ( $ file , '.php ' );
241
+ $ packageName = basename ($ file , '.php ' );
253
242
254
243
// Get package, language and file contents from language file
255
244
// /vendor/<package>/<language_code>/<filename>.php
256
- $ package = explode ( DIRECTORY_SEPARATOR , $ file )[ 2 ];
257
- $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 3 ];
258
- $ fileContents = require resource_path ( 'lang ' ) . DIRECTORY_SEPARATOR . $ file ;
245
+ $ package = explode (DIRECTORY_SEPARATOR , $ file)[ 2 ];
246
+ $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 3 ];
247
+ $ fileContents = require resource_path ('lang ' ). DIRECTORY_SEPARATOR . $ file ;
259
248
260
249
// Check if language already exists in array
261
- if ( array_key_exists ( $ language , $ this ->strings ) ) {
250
+ if (array_key_exists ($ language , $ this ->strings ) ) {
262
251
// Check if package already exists in language
263
- if ( array_key_exists ( $ package , $ this ->strings [ $ language ] ) ) {
264
- $ this ->strings [ $ language ][ $ package ][ $ packageName ] = $ fileContents ;
252
+ if (array_key_exists ($ package , $ this ->strings [$ language]) ) {
253
+ $ this ->strings [$ language][ $ package][ $ packageName ] = $ fileContents ;
265
254
} else {
266
- $ this ->strings [ $ language ][ $ package ] = [ $ packageName => $ fileContents ];
255
+ $ this ->strings [$ language][ $ package ] = [$ packageName => $ fileContents ];
267
256
}
268
257
} else {
269
- $ this ->strings [ $ language ] = [
258
+ $ this ->strings [$ language ] = [
270
259
271
260
$ package => [
272
261
@@ -276,25 +265,22 @@ protected function parseVendorFiles( $file )
276
265
}
277
266
}
278
267
279
- protected function parseJsonFiles ( $ file )
268
+ protected function parseJsonFiles ($ file )
280
269
{
281
270
// Base package name without file ending
282
- $ language = basename ( $ file , '.json ' );
271
+ $ language = basename ($ file , '.json ' );
283
272
284
273
// Get package, language and file contents from language file
285
274
// /<language_code>/(<package/)<filename>.php
286
- $ fileContents = json_decode ( file_get_contents ( resource_path ( 'lang ' ) . $ file ) );
275
+ $ fileContents = json_decode (file_get_contents (resource_path ('lang ' ). $ file) );
287
276
288
277
// Check if language already exists in array
289
- if ( array_key_exists ( 'json ' , $ this ->strings ) ) {
290
-
291
- if ( array_key_exists ( $ language , $ this ->strings [ 'json ' ] ) ) {
292
-
293
- $ this ->strings [ 'json ' ][ $ language ] = $ fileContents ;
294
-
278
+ if (array_key_exists ('json ' , $ this ->strings )) {
279
+ if (array_key_exists ($ language , $ this ->strings ['json ' ])) {
280
+ $ this ->strings ['json ' ][$ language ] = $ fileContents ;
295
281
}
296
282
} else {
297
- $ this ->strings [ 'json ' ] = [
283
+ $ this ->strings ['json ' ] = [
298
284
299
285
$ language => $ fileContents ,
300
286
];
0 commit comments