@@ -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,58 +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 );
109
+ $ files = array_merge ( $ phpFiles , $ jsonFiles );
110
110
111
111
// Sort array by filepath
112
- sort ($ files );
112
+ sort ( $ files );
113
113
114
114
// Remove full path from items
115
- array_walk ($ files , function (&$ item ) {
116
- $ item = str_replace (resource_path ('lang ' ), '' , $ item );
117
- });
115
+ array_walk ( $ files , function ( &$ item ) {
116
+ $ item = str_replace ( resource_path ( 'lang ' ), '' , $ item );
117
+ } );
118
118
119
119
// Fetch non-vendor files from filtered php files
120
- $ nonVendorFiles = array_filter ($ files , function ($ file ) {
121
- return strpos ($ file , $ this ->excludePath ) === false && strpos ($ file , '.json ' ) === false ;
122
- });
120
+ $ nonVendorFiles = array_filter ( $ files , function ( $ file ) {
121
+ return strpos ( $ file , $ this ->excludePath ) === false && strpos ( $ file , '.json ' ) === false ;
122
+ } );
123
123
124
124
// Fetch vendor files from filtered php files
125
- $ vendorFiles = array_filter (array_diff ($ files , $ nonVendorFiles ), function ($ file ) {
126
- return strpos ($ file , 'json ' ) === false ;
127
- });
125
+ $ vendorFiles = array_filter ( array_diff ( $ files , $ nonVendorFiles ), function ( $ file ) {
126
+ return strpos ( $ file , 'json ' ) === false ;
127
+ } );
128
128
129
129
// Fetch .json files from filtered files
130
- $ jsonFiles = array_filter ($ files , function ($ file ) {
131
- return strpos ($ file , '.json ' ) !== false ;
132
- });
130
+ $ jsonFiles = array_filter ( $ files , function ( $ file ) {
131
+ return strpos ( $ file , '.json ' ) !== false ;
132
+ } );
133
133
134
134
return [
135
- 'lang ' => array_values ($ nonVendorFiles ),
136
- 'vendor ' => array_values ($ vendorFiles ),
137
- 'json ' => array_values ($ jsonFiles ),
135
+ 'lang ' => array_values ( $ nonVendorFiles ),
136
+ 'vendor ' => array_values ( $ vendorFiles ),
137
+ 'json ' => array_values ( $ jsonFiles ),
138
138
];
139
139
}
140
140
@@ -167,27 +167,23 @@ public function toArray()
167
167
*
168
168
* @return array
169
169
*/
170
- public function toFlat ($ prefix = '. ' )
170
+ public function toFlat ( $ prefix = '. ' )
171
171
{
172
172
$ results = [];
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 ;
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 ;
178
178
}
179
179
} else {
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 ;
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 ;
184
184
} else {
185
- $ results [$ key ] = $ json_strings ;
185
+ $ results [ $ key ] = $ json_strings ;
186
186
}
187
- /*foreach ( $json_strings as $jsLang => $json_messages ) {
188
- $key = 'json' . $prefix . $json_lang . $prefix . $jsLang;
189
- $results[ $key ] = $json_messages;
190
- }*/
191
187
}
192
188
}
193
189
}
@@ -202,29 +198,29 @@ public function toFlat($prefix = '.')
202
198
*/
203
199
public function toCollection ()
204
200
{
205
- return collect ($ this ->strings );
201
+ return collect ( $ this ->strings );
206
202
}
207
203
208
204
/**
209
205
* Method to parse language files.
210
206
*
211
207
* @param string $file
212
208
*/
213
- protected function parseLangFiles ($ file )
209
+ protected function parseLangFiles ( $ file )
214
210
{
215
211
// Base package name without file ending
216
- $ packageName = basename ($ file , '.php ' );
212
+ $ packageName = basename ( $ file , '.php ' );
217
213
218
214
// Get package, language and file contents from language file
219
215
// /<language_code>/(<package/)<filename>.php
220
- $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 1 ];
221
- $ fileContents = require resource_path ('lang ' ). DIRECTORY_SEPARATOR . $ file ;
216
+ $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 1 ];
217
+ $ fileContents = require resource_path ( 'lang ' ) . DIRECTORY_SEPARATOR . $ file ;
222
218
223
219
// Check if language already exists in array
224
- if (array_key_exists ($ language , $ this ->strings ) ) {
225
- $ this ->strings [$ language][ $ packageName ] = $ fileContents ;
220
+ if ( array_key_exists ( $ language , $ this ->strings ) ) {
221
+ $ this ->strings [ $ language ][ $ packageName ] = $ fileContents ;
226
222
} else {
227
- $ this ->strings [$ language ] = [
223
+ $ this ->strings [ $ language ] = [
228
224
$ packageName => $ fileContents ,
229
225
];
230
226
}
@@ -235,27 +231,27 @@ protected function parseLangFiles($file)
235
231
*
236
232
* @param string $file
237
233
*/
238
- protected function parseVendorFiles ($ file )
234
+ protected function parseVendorFiles ( $ file )
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
// /vendor/<package>/<language_code>/<filename>.php
245
- $ package = explode (DIRECTORY_SEPARATOR , $ file)[ 2 ];
246
- $ language = explode (DIRECTORY_SEPARATOR , $ file)[ 3 ];
247
- $ fileContents = require resource_path ('lang ' ). DIRECTORY_SEPARATOR . $ file ;
241
+ $ package = explode ( DIRECTORY_SEPARATOR , $ file )[ 2 ];
242
+ $ language = explode ( DIRECTORY_SEPARATOR , $ file )[ 3 ];
243
+ $ fileContents = require resource_path ( 'lang ' ) . DIRECTORY_SEPARATOR . $ file ;
248
244
249
245
// Check if language already exists in array
250
- if (array_key_exists ($ language , $ this ->strings ) ) {
246
+ if ( array_key_exists ( $ language , $ this ->strings ) ) {
251
247
// Check if package already exists in language
252
- if (array_key_exists ($ package , $ this ->strings [$ language]) ) {
253
- $ this ->strings [$ language][ $ package][ $ packageName ] = $ fileContents ;
248
+ if ( array_key_exists ( $ package , $ this ->strings [ $ language ] ) ) {
249
+ $ this ->strings [ $ language ][ $ package ][ $ packageName ] = $ fileContents ;
254
250
} else {
255
- $ this ->strings [$ language][ $ package ] = [$ packageName => $ fileContents ];
251
+ $ this ->strings [ $ language ][ $ package ] = [ $ packageName => $ fileContents ];
256
252
}
257
253
} else {
258
- $ this ->strings [$ language ] = [
254
+ $ this ->strings [ $ language ] = [
259
255
260
256
$ package => [
261
257
@@ -265,22 +261,22 @@ protected function parseVendorFiles($file)
265
261
}
266
262
}
267
263
268
- protected function parseJsonFiles ($ file )
264
+ protected function parseJsonFiles ( $ file )
269
265
{
270
266
// Base package name without file ending
271
- $ language = basename ($ file , '.json ' );
267
+ $ language = basename ( $ file , '.json ' );
272
268
273
269
// Get package, language and file contents from language file
274
270
// /<language_code>/(<package/)<filename>.php
275
- $ fileContents = json_decode (file_get_contents (resource_path ('lang ' ). $ file) );
271
+ $ fileContents = json_decode ( file_get_contents ( resource_path ( 'lang ' ) . $ file ) );
276
272
277
273
// Check if language already exists in array
278
- if (array_key_exists ('json ' , $ this ->strings ) ) {
279
- if (array_key_exists ($ language , $ this ->strings ['json ' ]) ) {
280
- $ this ->strings ['json ' ][ $ language ] = $ fileContents ;
274
+ if ( array_key_exists ( 'json ' , $ this ->strings ) ) {
275
+ if ( array_key_exists ( $ language , $ this ->strings [ 'json ' ] ) ) {
276
+ $ this ->strings [ 'json ' ][ $ language ] = $ fileContents ;
281
277
}
282
278
} else {
283
- $ this ->strings ['json ' ] = [
279
+ $ this ->strings [ 'json ' ] = [
284
280
285
281
$ language => $ fileContents ,
286
282
];
0 commit comments