Skip to content

Commit f6796cd

Browse files
committed
Fixed getting only one json file
1 parent 034f569 commit f6796cd

File tree

1 file changed

+82
-83
lines changed

1 file changed

+82
-83
lines changed

src/Classes/ExportLocalizations.php

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ExportLocalizations implements \JsonSerializable
3131
/**
3232
* @var string
3333
*/
34-
protected $excludePath = DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR;
34+
protected $excludePath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
3535

3636
/**
3737
* @var string
@@ -46,31 +46,31 @@ class ExportLocalizations implements \JsonSerializable
4646
public function export()
4747
{
4848
// 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' ) );
5151

5252
return $this;
5353
}
5454

5555
// Collect language files and build array with translations
56-
$files = $this->findLanguageFiles(resource_path('lang'));
56+
$files = $this->findLanguageFiles( resource_path( 'lang' ) );
5757

5858
// 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' ] );
6262

6363
// Trigger event for final translated array
64-
event(new LaravelLocalizationExported($this->strings));
64+
event( new LaravelLocalizationExported( $this->strings ) );
6565

6666
// 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'))
69-
->put(
70-
config('laravel-localization.caches.key', 'localization.array'),
71-
$this->strings,
72-
config('laravel-localization.caches.timeout', 60)
73-
);
67+
if ( config( 'laravel-localization.caches.timeout', 0 ) > 0 ) {
68+
Cache::store( config( 'laravel-localization.caches.driver', 'file' ) )
69+
->put(
70+
config( 'laravel-localization.caches.key', 'localization.array' ),
71+
$this->strings,
72+
config( 'laravel-localization.caches.timeout', 60 )
73+
);
7474
}
7575

7676
return $this;
@@ -83,58 +83,58 @@ public function export()
8383
*
8484
* @return array
8585
*/
86-
protected function findLanguageFiles($path)
86+
protected function findLanguageFiles( $path )
8787
{
8888
// 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 );
9191

9292
// Fetch only php files - skip others
9393
$phpFiles = array_values(
94-
array_map('current',
94+
array_map( 'current',
9595
iterator_to_array(
96-
new \RegexIterator($recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH)
96+
new \RegexIterator( $recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH )
9797
)
9898
)
9999
);
100100

101101
$jsonFiles = array_values(
102-
array_map('current',
102+
array_map( 'current',
103103
iterator_to_array(
104-
new \RegexIterator($recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH)
104+
new \RegexIterator( $recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH )
105105
)
106106
)
107107
);
108108

109-
$files = array_merge($phpFiles, $jsonFiles);
109+
$files = array_merge( $phpFiles, $jsonFiles );
110110

111111
// Sort array by filepath
112-
sort($files);
112+
sort( $files );
113113

114114
// 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+
} );
118118

119119
// 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+
} );
123123

124124
// 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+
} );
128128

129129
// 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+
} );
133133

134134
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 ),
138138
];
139139
}
140140

@@ -167,43 +167,43 @@ public function toArray()
167167
*
168168
* @return array
169169
*/
170-
public function toFlat($prefix = '.')
170+
public function toFlat( $prefix = '.' )
171171
{
172-
$results = [];
173-
$default_locale = config('laravel-localization.js.default_locale');
172+
$results = [];
173+
$default_locale = config( 'laravel-localization.js.default_locale' );
174174
$default_json_strings = null;
175175

176-
foreach ($this->strings as $lang => $strings) {
177-
if ($lang !== 'json') {
178-
foreach ($strings as $lang_array => $lang_messages) {
179-
$key = $lang.$prefix.$lang_array;
180-
$results[$key] = $lang_messages;
176+
foreach ( $this->strings as $lang => $strings ) {
177+
if ( $lang !== 'json' ) {
178+
foreach ( $strings as $lang_array => $lang_messages ) {
179+
$key = $lang . $prefix . $lang_array;
180+
$results[ $key ] = $lang_messages;
181181
}
182182
} else {
183-
foreach ($strings as $json_lang => $json_strings) {
184-
$key = $json_lang.$prefix.'__JSON__';
185-
if (array_key_exists($key, $results)) {
186-
$results[$key] = $json_strings;
183+
foreach ( $strings as $json_lang => $json_strings ) {
184+
$key = $json_lang . $prefix . '__JSON__';
185+
if ( array_key_exists( $key, $results ) ) {
186+
$results[ $key ] = $json_strings;
187187
} else {
188-
$results[$key] = $json_strings;
188+
$results[ $key ] = $json_strings;
189189
}
190190

191191
// Pick only the first $json_strings
192-
if (! $default_json_strings) {
192+
if ( !$default_json_strings ) {
193193
$default_json_strings = $json_strings;
194194
}
195195
}
196196
}
197197
}
198198

199199
// Create a JSON key value pair for the default language
200-
$default_key = $default_locale.$prefix.'__JSON__';
201-
if (! array_key_exists($default_key, $results)) {
200+
$default_key = $default_locale . $prefix . '__JSON__';
201+
if ( !array_key_exists( $default_key, $results ) ) {
202202
$buffer = array_keys(
203-
get_object_vars($default_json_strings)
203+
get_object_vars( $default_json_strings )
204204
);
205205

206-
$results[$default_key] = array_combine($buffer, $buffer);
206+
$results[ $default_key ] = array_combine( $buffer, $buffer );
207207
}
208208

209209
return $results;
@@ -216,29 +216,29 @@ public function toFlat($prefix = '.')
216216
*/
217217
public function toCollection()
218218
{
219-
return collect($this->strings);
219+
return collect( $this->strings );
220220
}
221221

222222
/**
223223
* Method to parse language files.
224224
*
225225
* @param string $file
226226
*/
227-
protected function parseLangFiles($file)
227+
protected function parseLangFiles( $file )
228228
{
229229
// Base package name without file ending
230-
$packageName = basename($file, '.php');
230+
$packageName = basename( $file, '.php' );
231231

232232
// Get package, language and file contents from language file
233233
// /<language_code>/(<package/)<filename>.php
234-
$language = explode(DIRECTORY_SEPARATOR, $file)[1];
235-
$fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file;
234+
$language = explode( DIRECTORY_SEPARATOR, $file )[ 1 ];
235+
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
236236

237237
// Check if language already exists in array
238-
if (array_key_exists($language, $this->strings)) {
239-
$this->strings[$language][$packageName] = $fileContents;
238+
if ( array_key_exists( $language, $this->strings ) ) {
239+
$this->strings[ $language ][ $packageName ] = $fileContents;
240240
} else {
241-
$this->strings[$language] = [
241+
$this->strings[ $language ] = [
242242
$packageName => $fileContents,
243243
];
244244
}
@@ -249,27 +249,27 @@ protected function parseLangFiles($file)
249249
*
250250
* @param string $file
251251
*/
252-
protected function parseVendorFiles($file)
252+
protected function parseVendorFiles( $file )
253253
{
254254
// Base package name without file ending
255-
$packageName = basename($file, '.php');
255+
$packageName = basename( $file, '.php' );
256256

257257
// Get package, language and file contents from language file
258258
// /vendor/<package>/<language_code>/<filename>.php
259-
$package = explode(DIRECTORY_SEPARATOR, $file)[2];
260-
$language = explode(DIRECTORY_SEPARATOR, $file)[3];
261-
$fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file;
259+
$package = explode( DIRECTORY_SEPARATOR, $file )[ 2 ];
260+
$language = explode( DIRECTORY_SEPARATOR, $file )[ 3 ];
261+
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
262262

263263
// Check if language already exists in array
264-
if (array_key_exists($language, $this->strings)) {
264+
if ( array_key_exists( $language, $this->strings ) ) {
265265
// Check if package already exists in language
266-
if (array_key_exists($package, $this->strings[$language])) {
267-
$this->strings[$language][$package][$packageName] = $fileContents;
266+
if ( array_key_exists( $package, $this->strings[ $language ] ) ) {
267+
$this->strings[ $language ][ $package ][ $packageName ] = $fileContents;
268268
} else {
269-
$this->strings[$language][$package] = [$packageName => $fileContents];
269+
$this->strings[ $language ][ $package ] = [ $packageName => $fileContents ];
270270
}
271271
} else {
272-
$this->strings[$language] = [
272+
$this->strings[ $language ] = [
273273

274274
$package => [
275275

@@ -279,22 +279,21 @@ protected function parseVendorFiles($file)
279279
}
280280
}
281281

282-
protected function parseJsonFiles($file)
282+
protected function parseJsonFiles( $file )
283283
{
284284
// Base package name without file ending
285-
$language = basename($file, '.json');
285+
$language = basename( $file, '.json' );
286286

287287
// Get package, language and file contents from language file
288288
// /<language_code>/(<package/)<filename>.php
289-
$fileContents = json_decode(file_get_contents(resource_path('lang').$file));
289+
$fileContents = json_decode( file_get_contents( resource_path( 'lang' ) . $file ) );
290290

291291
// Check if language already exists in array
292-
if (array_key_exists('json', $this->strings)) {
293-
if (array_key_exists($language, $this->strings['json'])) {
294-
$this->strings['json'][$language] = $fileContents;
295-
}
292+
if ( array_key_exists( 'json', $this->strings ) ) {
293+
294+
$this->strings[ 'json' ][ $language ] = $fileContents;
296295
} else {
297-
$this->strings['json'] = [
296+
$this->strings[ 'json' ] = [
298297

299298
$language => $fileContents,
300299
];

0 commit comments

Comments
 (0)