From 3429f47c08d7eacdd55c92b0c2a3bd2c409da7cf Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Sun, 28 Jan 2018 14:35:28 +0100 Subject: [PATCH 1/8] fire locale.changed event so argon applies the current locale --- classes/Translator.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/Translator.php b/classes/Translator.php index f395b5f0..da64460e 100644 --- a/classes/Translator.php +++ b/classes/Translator.php @@ -1,6 +1,7 @@ setSessionLocale($locale); } + // this is a workaround to force Argon to switch to the current locale + Event::fire('locale.changed', $locale); + return true; } From 001e6ea1c473639aae65e8c2fa08c772185c0ad7 Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Sun, 28 Jan 2018 14:47:02 +0100 Subject: [PATCH 2/8] Revert "Make the cache timeout configurable (#335)" This reverts commit 8a21ec92d3333c23948dbe1812f3ceae0bc89963. --- config/config.php | 19 +++---------------- models/Message.php | 3 +-- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/config/config.php b/config/config.php index 7b8714bf..0a73dd6f 100644 --- a/config/config.php +++ b/config/config.php @@ -3,27 +3,14 @@ return [ /* |-------------------------------------------------------------------------- - | Prefix the default Locale + | prefix the default Locale |-------------------------------------------------------------------------- | | Should the default locale be prefixed by the plugin? | - | ( For now only used in the alternate-hreflang component ) + | ( For now only used in the alternate-hreflang cmponent ) | */ - 'prefixDefaultLocale' => true, - - /* - |-------------------------------------------------------------------------- - | Cache timeout in minutes - |-------------------------------------------------------------------------- - | - | By default all translations are cached for 24 hours (1440 min). - | This setting allows to change that period with given amount of minutes. - | - | ( For example 43200 for 30 days or 525600 for one year ) - | - */ - 'cacheTimeout' => 1440, + 'prefixDefaultLocale' => true ]; diff --git a/models/Message.php b/models/Message.php index e80ec20f..4c4c6b25 100644 --- a/models/Message.php +++ b/models/Message.php @@ -4,7 +4,6 @@ use Lang; use Model; use Cache; -use Config; /** * Message Model @@ -239,7 +238,7 @@ public static function saveToCache() return; } - Cache::put(self::makeCacheKey(), self::$cache, Config::get('rainlab.translate::cacheTimeout', 1440)); + Cache::put(self::makeCacheKey(), self::$cache, 1440); } /** From 26be46717e1110a2f2881dbd7a32059c24e63d7d Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Mon, 29 Jan 2018 12:03:55 +0100 Subject: [PATCH 3/8] "Make the cache timeout configurable (#335)" This reverts commit 001e6ea1c473639aae65e8c2fa08c772185c0ad7. --- classes/Translator.php | 4 ---- config/config.php | 19 ++++++++++++++++--- models/Message.php | 3 ++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/classes/Translator.php b/classes/Translator.php index da64460e..f395b5f0 100644 --- a/classes/Translator.php +++ b/classes/Translator.php @@ -1,7 +1,6 @@ setSessionLocale($locale); } - // this is a workaround to force Argon to switch to the current locale - Event::fire('locale.changed', $locale); - return true; } diff --git a/config/config.php b/config/config.php index 0a73dd6f..7b8714bf 100644 --- a/config/config.php +++ b/config/config.php @@ -3,14 +3,27 @@ return [ /* |-------------------------------------------------------------------------- - | prefix the default Locale + | Prefix the default Locale |-------------------------------------------------------------------------- | | Should the default locale be prefixed by the plugin? | - | ( For now only used in the alternate-hreflang cmponent ) + | ( For now only used in the alternate-hreflang component ) | */ - 'prefixDefaultLocale' => true + 'prefixDefaultLocale' => true, + + /* + |-------------------------------------------------------------------------- + | Cache timeout in minutes + |-------------------------------------------------------------------------- + | + | By default all translations are cached for 24 hours (1440 min). + | This setting allows to change that period with given amount of minutes. + | + | ( For example 43200 for 30 days or 525600 for one year ) + | + */ + 'cacheTimeout' => 1440, ]; diff --git a/models/Message.php b/models/Message.php index 4c4c6b25..e80ec20f 100644 --- a/models/Message.php +++ b/models/Message.php @@ -4,6 +4,7 @@ use Lang; use Model; use Cache; +use Config; /** * Message Model @@ -238,7 +239,7 @@ public static function saveToCache() return; } - Cache::put(self::makeCacheKey(), self::$cache, 1440); + Cache::put(self::makeCacheKey(), self::$cache, Config::get('rainlab.translate::cacheTimeout', 1440)); } /** From 59c22ce4964c80811cdf6fc3106cfcf820abeb2a Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Thu, 20 Dec 2018 05:24:02 +0100 Subject: [PATCH 4/8] added default locale 'x' to export and import. --- models/MessageExport.php | 19 ++++++++++++------- models/MessageImport.php | 10 ++++------ tests/unit/models/ExportMessageTest.php | 16 ++++++++++++++++ tests/unit/models/ImportMessageTest.php | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/models/MessageExport.php b/models/MessageExport.php index cf2b8cb6..bccbf66c 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -5,14 +5,15 @@ class MessageExport extends ExportModel { const CODE_COLUMN_NAME = 'code'; + const DEFAULT_LOCALE_COLUMN_NAME = 'default'; /** * exports the message data with each locale in a separate column. * - * code | en | de | fr - * ------------------------------- - * title | Title | Titel | Titre - * name | Name | Name | Prénom + * code | default | en | de | fr + * ---------------------------------------------- + * title | title | Title | Titel | Titre + * item.name | Item Name | Name | Name | Prénom * ... * * @param $columns @@ -21,6 +22,7 @@ class MessageExport extends ExportModel */ public function exportData($columns, $sessionKey = null) { + return Message::all()->map(function($message) use($columns) { $data = $message->message_data; // add code to data to simplify algorithm @@ -36,8 +38,11 @@ public function exportData($columns, $sessionKey = null) public static function getColumns() { - // code column + all existing locales - return array_merge([self::CODE_COLUMN_NAME => self::CODE_COLUMN_NAME], - Locale::lists(self::CODE_COLUMN_NAME, self::CODE_COLUMN_NAME)); + // code, default column + all existing locales + return array_merge([ + self::CODE_COLUMN_NAME => self::CODE_COLUMN_NAME, + Message::DEFAULT_LOCALE => Message::DEFAULT_LOCALE, + ], Locale::lists(self::CODE_COLUMN_NAME, self::CODE_COLUMN_NAME)); } + } diff --git a/models/MessageImport.php b/models/MessageImport.php index 9895b8a6..6176007e 100644 --- a/models/MessageImport.php +++ b/models/MessageImport.php @@ -38,14 +38,11 @@ public function importData($results, $sessionKey = null) $message = Message::firstOrNew(['code' => $code]); - // create empty array, if $message is new - $message->message_data = $message->message_data ?: []; - - if(!isset($message->message_data[Message::DEFAULT_LOCALE])) { + // write default if not existing + if(!isset($result[Message::DEFAULT_LOCALE])) { $result[Message::DEFAULT_LOCALE] = $code; } - - $message->message_data = array_merge($message->message_data, $result); + $message->message_data = array_merge($message->message_data ?: [], $result); if ($message->exists) { $this->logUpdated(); @@ -57,6 +54,7 @@ public function importData($results, $sessionKey = null) $this->logSkipped($index, 'no code provided'); } } catch (\Exception $exception) { + echo $exception->getMessage(); $this->logError($index, $exception->getMessage()); } } diff --git a/tests/unit/models/ExportMessageTest.php b/tests/unit/models/ExportMessageTest.php index 41c88d99..6330063d 100644 --- a/tests/unit/models/ExportMessageTest.php +++ b/tests/unit/models/ExportMessageTest.php @@ -1,6 +1,7 @@ assertEquals($expected, $result); } + public function testGetColumns() + { + Locale::unguard(); + Locale::create(['code' => 'de', 'name' => 'German', 'is_enabled' => true]); + + $columns = MessageExport::getColumns(); + + $this->assertEquals([ + MessageExport::CODE_COLUMN_NAME => MessageExport::CODE_COLUMN_NAME, + Message::DEFAULT_LOCALE => Message::DEFAULT_LOCALE, + 'en' => 'en', + 'de' => 'de', + ], $columns); + } + private function createMessages() { Message::create([ diff --git a/tests/unit/models/ImportMessageTest.php b/tests/unit/models/ImportMessageTest.php index 491eb5d7..a6932c4a 100644 --- a/tests/unit/models/ImportMessageTest.php +++ b/tests/unit/models/ImportMessageTest.php @@ -71,4 +71,25 @@ public function testMissingCodeIsSkipped() $this->assertEquals(true, $stats->hasMessages); $this->assertEquals(Message::count(), 0); } + + public function testDefaultLocaleIsImported() + { + $messageImport = new MessageImport(); + $data = [ + ['code' => 'test.me', 'x' => 'foo bar', 'de' => 'Neu 2', 'en' => 'new 2'] + ]; + + $messageImport->importData($data); + + $stats = $messageImport->getResultStats(); + $this->assertEquals(1, $stats->created); + $this->assertEquals(0, $stats->updated); + $this->assertEquals(0, $stats->skippedCount); + $this->assertEquals(false, $stats->hasMessages); + $this->assertEquals(Message::count(), 1); + + $message = Message::where('code', 'test.me')->first(); + + $this->assertEquals('foo bar', $message->message_data['x']); + } } \ No newline at end of file From 73a1c2337031b2c73d11f49c9b4c5d8c84a88ce4 Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Thu, 20 Dec 2018 05:24:46 +0100 Subject: [PATCH 5/8] adopted documentation --- models/MessageExport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/MessageExport.php b/models/MessageExport.php index bccbf66c..1a86391f 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -10,7 +10,7 @@ class MessageExport extends ExportModel /** * exports the message data with each locale in a separate column. * - * code | default | en | de | fr + * code | x | en | de | fr * ---------------------------------------------- * title | title | Title | Titel | Titre * item.name | Item Name | Name | Name | Prénom From 1ed505ac42dbb1b60429da79a1da0cf4eca9b55e Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Thu, 20 Dec 2018 05:28:21 +0100 Subject: [PATCH 6/8] increment version --- updates/version.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/updates/version.yaml b/updates/version.yaml index 66813e8f..29aead63 100755 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -51,3 +51,4 @@ 1.4.0: Add importer and exporter for messages 1.4.1: Updated Hungarian translation. Added Arabic translation. Fixed issue where default texts are overwritten by import. Fixed issue where the language switcher for repeater fields would overlap with the first repeater row. 1.4.2: Add multilingual MediaFinder +1.4.3: Export and import messages default locale. \ No newline at end of file From 3048a563c0027d77d4b6b873c7d409558c8ca11d Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Thu, 20 Dec 2018 05:28:41 +0100 Subject: [PATCH 7/8] increment version --- updates/version.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updates/version.yaml b/updates/version.yaml index 29aead63..887b9f52 100755 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -51,4 +51,4 @@ 1.4.0: Add importer and exporter for messages 1.4.1: Updated Hungarian translation. Added Arabic translation. Fixed issue where default texts are overwritten by import. Fixed issue where the language switcher for repeater fields would overlap with the first repeater row. 1.4.2: Add multilingual MediaFinder -1.4.3: Export and import messages default locale. \ No newline at end of file +1.4.3: Export and import messages default locale. From 3d6b9c667e0921444c039ce87fb1e327794e5804 Mon Sep 17 00:00:00 2001 From: Sascha Aeppli Date: Thu, 20 Dec 2018 11:35:30 +0100 Subject: [PATCH 8/8] removed unused constant --- models/MessageExport.php | 1 - 1 file changed, 1 deletion(-) diff --git a/models/MessageExport.php b/models/MessageExport.php index 1a86391f..65bd22c0 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -5,7 +5,6 @@ class MessageExport extends ExportModel { const CODE_COLUMN_NAME = 'code'; - const DEFAULT_LOCALE_COLUMN_NAME = 'default'; /** * exports the message data with each locale in a separate column.