From 713a5e06582d26d1bf35164370d69ad711664513 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 19 Jul 2021 13:03:44 -0400 Subject: [PATCH 1/9] - get fallback locale from app's config - fallback to parent locale for client localization strings overrides --- modules/system/console/WinterUtil.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index 1bbe6d9857..6cb8d80e05 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -190,10 +190,21 @@ protected function utilCompileLang() /* * Generate messages */ - $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; + $fallbackLocale = Config::get('app.fallback_locale', 'en'); + $fallbackPath = base_path() . '/modules/system/lang/'.$fallbackLocale.'/client.php'; + $srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php'; + + if (!File::isFile($fallbackPath) && str_contains($fallbackLocale, '-')) { + $this->warn('Warning: fallback path not found for country-specific locale: reverting to parent locale'); + list ($parentFallbackLocale, $country) = explode('-', $fallbackLocale); + $fallbackPath = base_path() . '/modules/system/lang/'.$parentFallbackLocale.'/client.php'; + } else { + $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; + } $messages = require $fallbackPath; + if (File::isFile($srcPath) && $fallbackPath != $srcPath) { $messages = array_replace_recursive($messages, require $srcPath); } @@ -204,6 +215,12 @@ protected function utilCompileLang() $overridePath = base_path() . '/lang/'.$locale.'/system/client.php'; if (File::isFile($overridePath)) { $messages = array_replace_recursive($messages, require $overridePath); + } else if (str_contains($locale, '-')) { + list ($parentLocale, $country) = explode('-', $locale); + $overridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php'; + if (File::isFile($overridePath)) { + $messages = array_replace_recursive($messages, require $overridePath); + } } /* From 79a13beab3cd490f03d13753bc62e45766b77ffb Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 20 Jul 2021 00:02:42 -0400 Subject: [PATCH 2/9] revert using app.fallback_locale --- modules/system/console/WinterUtil.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index 6cb8d80e05..6986125954 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -190,19 +190,11 @@ protected function utilCompileLang() /* * Generate messages */ - $fallbackLocale = Config::get('app.fallback_locale', 'en'); - $fallbackPath = base_path() . '/modules/system/lang/'.$fallbackLocale.'/client.php'; + $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; $srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php'; - if (!File::isFile($fallbackPath) && str_contains($fallbackLocale, '-')) { - $this->warn('Warning: fallback path not found for country-specific locale: reverting to parent locale'); - list ($parentFallbackLocale, $country) = explode('-', $fallbackLocale); - $fallbackPath = base_path() . '/modules/system/lang/'.$parentFallbackLocale.'/client.php'; - } else { - $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; - } $messages = require $fallbackPath; if (File::isFile($srcPath) && $fallbackPath != $srcPath) { From f247e391ad27f18c809b9500087b13f6e89540f0 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 20 Jul 2021 00:08:05 -0400 Subject: [PATCH 3/9] remove empty lines --- modules/system/console/WinterUtil.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index 6986125954..a389ee8821 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -191,12 +191,9 @@ protected function utilCompileLang() * Generate messages */ $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; - $srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php'; - $messages = require $fallbackPath; - if (File::isFile($srcPath) && $fallbackPath != $srcPath) { $messages = array_replace_recursive($messages, require $srcPath); } From dd943d21e68501f46b70342764ebd7258a200bfb Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Jul 2021 01:24:23 -0600 Subject: [PATCH 4/9] Update modules/system/console/WinterUtil.php --- modules/system/console/WinterUtil.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index a389ee8821..f9987a8e70 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -204,7 +204,7 @@ protected function utilCompileLang() $overridePath = base_path() . '/lang/'.$locale.'/system/client.php'; if (File::isFile($overridePath)) { $messages = array_replace_recursive($messages, require $overridePath); - } else if (str_contains($locale, '-')) { + } elseif (str_contains($locale, '-')) { list ($parentLocale, $country) = explode('-', $locale); $overridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php'; if (File::isFile($overridePath)) { From 75f10d317be56546efbdc13bc48f59a18c57280a Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Jul 2021 01:24:46 -0600 Subject: [PATCH 5/9] Update modules/system/console/WinterUtil.php --- modules/system/console/WinterUtil.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index f9987a8e70..398e8f1deb 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -205,7 +205,7 @@ protected function utilCompileLang() if (File::isFile($overridePath)) { $messages = array_replace_recursive($messages, require $overridePath); } elseif (str_contains($locale, '-')) { - list ($parentLocale, $country) = explode('-', $locale); + list($parentLocale, $country) = explode('-', $locale); $overridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php'; if (File::isFile($overridePath)) { $messages = array_replace_recursive($messages, require $overridePath); From a3fa3928c3a4258e89537516336872c246394b6a Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 20 Jul 2021 11:16:23 -0400 Subject: [PATCH 6/9] include all override levels when merging overrides --- modules/system/console/WinterUtil.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index 398e8f1deb..a7797c99ca 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -191,9 +191,11 @@ protected function utilCompileLang() * Generate messages */ $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; + $srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php'; $messages = require $fallbackPath; + if (File::isFile($srcPath) && $fallbackPath != $srcPath) { $messages = array_replace_recursive($messages, require $srcPath); } @@ -201,17 +203,25 @@ protected function utilCompileLang() /* * Load possible replacements from /lang */ + $overrides = []; + $parentOverrides = []; + $overridePath = base_path() . '/lang/'.$locale.'/system/client.php'; if (File::isFile($overridePath)) { - $messages = array_replace_recursive($messages, require $overridePath); - } elseif (str_contains($locale, '-')) { + $overrides = require $overridePath; + } + + if (str_contains($locale, '-')) { list($parentLocale, $country) = explode('-', $locale); - $overridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php'; - if (File::isFile($overridePath)) { - $messages = array_replace_recursive($messages, require $overridePath); + + $parentOverridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php'; + if (File::isFile($parentOverridePath)) { + $parentOverrides = require $parentOverridePath; } } + $messages = array_replace_recursive($messages, $parentOverrides, $overrides); + /* * Compile from stub and save file */ From 3f4e346fa4f0de11dbba256f47ebafdf33f9ee00 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 20 Jul 2021 11:24:22 -0400 Subject: [PATCH 7/9] remove empty line --- modules/system/console/WinterUtil.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/system/console/WinterUtil.php b/modules/system/console/WinterUtil.php index a7797c99ca..a3f609588a 100644 --- a/modules/system/console/WinterUtil.php +++ b/modules/system/console/WinterUtil.php @@ -191,7 +191,6 @@ protected function utilCompileLang() * Generate messages */ $fallbackPath = base_path() . '/modules/system/lang/en/client.php'; - $srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php'; $messages = require $fallbackPath; From 0b675e84e2571402e9f7c4787974d0d41d620713 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Tue, 28 Sep 2021 12:47:23 +0100 Subject: [PATCH 8/9] Added WinterUtilTest for compile lang testing --- tests/unit/system/console/WinterUtilTest.php | 82 ++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/unit/system/console/WinterUtilTest.php diff --git a/tests/unit/system/console/WinterUtilTest.php b/tests/unit/system/console/WinterUtilTest.php new file mode 100644 index 0000000000..636097ecbf --- /dev/null +++ b/tests/unit/system/console/WinterUtilTest.php @@ -0,0 +1,82 @@ + \'is coming\'];'); + // execute compile + $this->artisan('winter:util compile lang')->execute(); + // validate default lang handling + $lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js')); + $this->assertStringContainsString('winter', $lang); + $this->assertStringContainsString('is coming', $lang); + + // simulate override + $created = []; + + foreach (['lang/en/system', 'lang/en-gb/system'] as $slug) { + $path = rtrim(base_path(), '/'); + foreach (explode('/', $slug) as $dir) { + $path = $path . '/' . $dir; + if (!is_dir($path)) { + mkdir($path, 0755); + $created[] = $path; + } + } + } + + // handle existing file + $langClient = base_path('lang/en/system/client.php'); + if (file_exists($langClient)) { + rename($langClient, $langClient . '.backup'); + } + + file_put_contents($langClient, ' \'is epic\'];'); + + // handle existing file + $langCountryClient = base_path('lang/en-gb/system/client.php'); + if (file_exists($langCountryClient)) { + rename($langCountryClient, $langCountryClient . '.backup'); + } + + file_put_contents($langCountryClient, ' \'winter\'];'); + + // execute compile + $this->artisan('winter:util compile lang')->execute(); + // validate override handling + $lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js')); + $this->assertStringContainsString('winter', $lang); + $this->assertStringContainsString('is epic', $lang); + + // check that lang subset has included parent overrides + $lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en-gb.js')); + $this->assertStringContainsString('winter', $lang); + $this->assertStringContainsString('is epic', $lang); + $this->assertStringContainsString('whats_epic', $lang); + $this->assertStringContainsString('winter', $lang); + + // restore + unlink($target); + rename($target . '.backup', $target); + + foreach ([$langClient, $langCountryClient] as $client) { + unlink($client); + if (file_exists($client . '.backup')) { + rename($client . '.backup', $client); + } + } + + foreach (array_reverse($created) as $dir) { + rmdir($dir); + } + + // regenerate compiled lang + $this->artisan('winter:util compile lang')->execute(); + } +} From 638f74f5760a1909a8678be2e12fbfa9b61fe258 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Tue, 28 Sep 2021 13:05:52 +0100 Subject: [PATCH 9/9] Tidied up test and improved comments --- tests/unit/system/console/WinterUtilTest.php | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/unit/system/console/WinterUtilTest.php b/tests/unit/system/console/WinterUtilTest.php index 636097ecbf..9ea1c88dc8 100644 --- a/tests/unit/system/console/WinterUtilTest.php +++ b/tests/unit/system/console/WinterUtilTest.php @@ -1,17 +1,19 @@ \'is coming\'];'); + rename($defaultClient, $defaultClient . '.backup'); + + file_put_contents($defaultClient, ' \'is coming\'];'); + // execute compile $this->artisan('winter:util compile lang')->execute(); + // validate default lang handling $lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js')); $this->assertStringContainsString('winter', $lang); @@ -31,16 +33,16 @@ public function testCompileLang() } } - // handle existing file $langClient = base_path('lang/en/system/client.php'); + // handle existing file if (file_exists($langClient)) { rename($langClient, $langClient . '.backup'); } file_put_contents($langClient, ' \'is epic\'];'); - // handle existing file $langCountryClient = base_path('lang/en-gb/system/client.php'); + // handle existing file if (file_exists($langCountryClient)) { rename($langCountryClient, $langCountryClient . '.backup'); } @@ -49,6 +51,7 @@ public function testCompileLang() // execute compile $this->artisan('winter:util compile lang')->execute(); + // validate override handling $lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js')); $this->assertStringContainsString('winter', $lang); @@ -62,8 +65,8 @@ public function testCompileLang() $this->assertStringContainsString('winter', $lang); // restore - unlink($target); - rename($target . '.backup', $target); + unlink($defaultClient); + rename($defaultClient . '.backup', $defaultClient); foreach ([$langClient, $langCountryClient] as $client) { unlink($client); @@ -76,7 +79,7 @@ public function testCompileLang() rmdir($dir); } - // regenerate compiled lang + // regenerate original compiled lang $this->artisan('winter:util compile lang')->execute(); } }