Skip to content

Commit 034f569

Browse files
authored
Merge pull request #27 from ridaamirini/master
Improved toFlat method (#26)
2 parents 82316dc + d8e6e97 commit 034f569

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Classes/ExportLocalizations.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public function export()
6666
// If timeout > 0 save array to cache
6767
if (config('laravel-localization.caches.timeout', 0) > 0) {
6868
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-
);
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;
@@ -170,6 +170,9 @@ public function toArray()
170170
public function toFlat($prefix = '.')
171171
{
172172
$results = [];
173+
$default_locale = config('laravel-localization.js.default_locale');
174+
$default_json_strings = null;
175+
173176
foreach ($this->strings as $lang => $strings) {
174177
if ($lang !== 'json') {
175178
foreach ($strings as $lang_array => $lang_messages) {
@@ -184,10 +187,25 @@ public function toFlat($prefix = '.')
184187
} else {
185188
$results[$key] = $json_strings;
186189
}
190+
191+
// Pick only the first $json_strings
192+
if (! $default_json_strings) {
193+
$default_json_strings = $json_strings;
194+
}
187195
}
188196
}
189197
}
190198

199+
// 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)) {
202+
$buffer = array_keys(
203+
get_object_vars($default_json_strings)
204+
);
205+
206+
$results[$default_key] = array_combine($buffer, $buffer);
207+
}
208+
191209
return $results;
192210
}
193211

src/config/laravel-localization.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@
6363
*/
6464
'timeout' => 60,
6565
],
66-
'js' => [
66+
'js' => [
67+
/*
68+
* Default locale for export
69+
*/
70+
'default_locale' => 'en',
6771

6872
/*
6973
* root location to where JavaScript file will be exported

0 commit comments

Comments
 (0)