Skip to content

Commit

Permalink
Merge pull request #27 from ridaamirini/master
Browse files Browse the repository at this point in the history
Improved toFlat method (#26)
  • Loading branch information
kg-bot committed Dec 20, 2018
2 parents 82316dc + d8e6e97 commit 034f569
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/Classes/ExportLocalizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function export()
// If timeout > 0 save array to cache
if (config('laravel-localization.caches.timeout', 0) > 0) {
Cache::store(config('laravel-localization.caches.driver', 'file'))
->put(
config('laravel-localization.caches.key', 'localization.array'),
$this->strings,
config('laravel-localization.caches.timeout', 60)
);
->put(
config('laravel-localization.caches.key', 'localization.array'),
$this->strings,
config('laravel-localization.caches.timeout', 60)
);
}

return $this;
Expand Down Expand Up @@ -170,6 +170,9 @@ public function toArray()
public function toFlat($prefix = '.')
{
$results = [];
$default_locale = config('laravel-localization.js.default_locale');
$default_json_strings = null;

foreach ($this->strings as $lang => $strings) {
if ($lang !== 'json') {
foreach ($strings as $lang_array => $lang_messages) {
Expand All @@ -184,10 +187,25 @@ public function toFlat($prefix = '.')
} else {
$results[$key] = $json_strings;
}

// Pick only the first $json_strings
if (! $default_json_strings) {
$default_json_strings = $json_strings;
}
}
}
}

// Create a JSON key value pair for the default language
$default_key = $default_locale.$prefix.'__JSON__';
if (! array_key_exists($default_key, $results)) {
$buffer = array_keys(
get_object_vars($default_json_strings)
);

$results[$default_key] = array_combine($buffer, $buffer);
}

return $results;
}

Expand Down
6 changes: 5 additions & 1 deletion src/config/laravel-localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@
*/
'timeout' => 60,
],
'js' => [
'js' => [
/*
* Default locale for export
*/
'default_locale' => 'en',

/*
* root location to where JavaScript file will be exported
Expand Down

0 comments on commit 034f569

Please sign in to comment.