Skip to content

Commit 10d7ed5

Browse files
committed
Improved toFlat method (#26)
1 parent c3ee8f3 commit 10d7ed5

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/Classes/ExportLocalizations.php

+22-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,24 @@ 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;
187194
}
188195
}
189196
}
190197

198+
// Create a JSON key value pair for the default language
199+
$default_key = $default_locale . $prefix . '__JSON__';
200+
if (!array_key_exists($default_key, $results)) {
201+
$buffer = array_keys(
202+
get_object_vars($default_json_strings)
203+
);
204+
205+
$results[$default_key] = array_combine($buffer, $buffer);
206+
}
207+
191208
return $results;
192209
}
193210

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)