From 10d7ed562c2d7171e8dfefae410b3b5d54a4020a Mon Sep 17 00:00:00 2001 From: Rida Amirini Date: Thu, 20 Dec 2018 00:48:23 +0100 Subject: [PATCH 1/3] Improved toFlat method (#26) --- src/Classes/ExportLocalizations.php | 27 ++++++++++++++++++++++----- src/config/laravel-localization.php | 6 +++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Classes/ExportLocalizations.php b/src/Classes/ExportLocalizations.php index 43389d3..a9b67e6 100644 --- a/src/Classes/ExportLocalizations.php +++ b/src/Classes/ExportLocalizations.php @@ -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; @@ -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) { @@ -184,10 +187,24 @@ 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; } diff --git a/src/config/laravel-localization.php b/src/config/laravel-localization.php index 462d40f..b7dc499 100644 --- a/src/config/laravel-localization.php +++ b/src/config/laravel-localization.php @@ -63,7 +63,11 @@ */ 'timeout' => 60, ], - 'js' => [ + 'js' => [ + /* + * Default locale for export + */ + 'default_locale' => 'en', /* * root location to where JavaScript file will be exported From bd4402274a8eadadcf5592f0697e97a0c2ac049a Mon Sep 17 00:00:00 2001 From: Rida Amirini Date: Thu, 20 Dec 2018 00:53:23 +0100 Subject: [PATCH 2/3] Update ExportLocalizations.php --- src/Classes/ExportLocalizations.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Classes/ExportLocalizations.php b/src/Classes/ExportLocalizations.php index a9b67e6..dbecd0c 100644 --- a/src/Classes/ExportLocalizations.php +++ b/src/Classes/ExportLocalizations.php @@ -189,15 +189,15 @@ public function toFlat($prefix = '.') } // Pick only the first $json_strings - if (!$default_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)) { + $default_key = $default_locale.$prefix. '__JSON__'; + if (! array_key_exists($default_key, $results)) { $buffer = array_keys( get_object_vars($default_json_strings) ); From d8e6e97350feb0a0836e2840c8c50730e51f3dc7 Mon Sep 17 00:00:00 2001 From: Rida Amirini Date: Thu, 20 Dec 2018 00:55:19 +0100 Subject: [PATCH 3/3] Update ExportLocalizations.php --- src/Classes/ExportLocalizations.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Classes/ExportLocalizations.php b/src/Classes/ExportLocalizations.php index dbecd0c..e964f39 100644 --- a/src/Classes/ExportLocalizations.php +++ b/src/Classes/ExportLocalizations.php @@ -189,14 +189,15 @@ public function toFlat($prefix = '.') } // Pick only the first $json_strings - if (! $default_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__'; + $default_key = $default_locale.$prefix.'__JSON__'; if (! array_key_exists($default_key, $results)) { $buffer = array_keys( get_object_vars($default_json_strings)