From 824acf119189badd984599628b8fcbd0a31a6037 Mon Sep 17 00:00:00 2001 From: Ben Cavens Date: Tue, 24 Apr 2018 16:57:36 +0200 Subject: [PATCH] Ft: convert locale to application specific locales --- src/Detect.php | 26 +++++++++- src/Values/Locale.php | 11 ++++ src/config/locale.php | 39 +++++++------- tests/ConvertLocale/ConvertLocaleTest.php | 63 +++++++++++++++++++++++ tests/Values/LocaleTest.php | 8 +++ 5 files changed, 125 insertions(+), 22 deletions(-) create mode 100644 tests/ConvertLocale/ConvertLocaleTest.php diff --git a/src/Detect.php b/src/Detect.php index 96379ac..3b0bdf9 100644 --- a/src/Detect.php +++ b/src/Detect.php @@ -67,7 +67,7 @@ public function detectLocale(): self $this->locale = $locale; - app()->setLocale($locale->get()); + $this->setApplicationLocale(); return $this; } @@ -111,4 +111,28 @@ private function detectScope() { $this->scope = ScopeCollection::fromConfig($this->config)->findByRoot($this->request->root()); } + + private function setApplicationLocale() + { + $locale = $this->locale; + + $conversions = $this->config->get('convert_locales_to', []); + + if('auto' === $this->config->get('convert_locales')) + { + if(isset($conversions[$locale->get()])){ + $locale = Locale::from($conversions[$locale->get()]); + }else{ + $locale = $locale->withoutRegion(); + } + } + else if(true === $this->config->get('convert_locales')) + { + if(isset($conversions[$locale->get()])){ + $locale = Locale::from($conversions[$locale->get()]); + } + } + + app()->setLocale($locale->get()); + } } diff --git a/src/Values/Locale.php b/src/Values/Locale.php index 26bab4e..0b032cf 100644 --- a/src/Values/Locale.php +++ b/src/Values/Locale.php @@ -19,6 +19,17 @@ public static function from(string $value) return new static($value); } + public function withoutRegion() + { + if(false !== strpos($this->value, '-')){ + $value = substr($this->value,0,strpos($this->value, '-')); + }else if(false !== strpos($this->value, '_')){ + $value = substr($this->value,0,strpos($this->value, '_')); + } + + return new static($value); + } + public function get(): string { return $this->value; diff --git a/src/config/locale.php b/src/config/locale.php index 4eb586f..b39b05e 100644 --- a/src/config/locale.php +++ b/src/config/locale.php @@ -12,29 +12,26 @@ '*' => 'en', ], -// 'locales' => [ -// 'fr.example.com' => 'fr', -// 'example.com' => [ -// '/fr' => 'fr', -// '/' => 'en', -// ], -// 'mijnkindishetallerliefste.be' => 'nl', -// 'monenfantestleplusadorable.be' => [ -// '/nl' => 'nl', -// '/' => 'fr-BE', -// ], -// 'monenfantestleplusadorable.fr' => 'fr', -// -// '*' => [ -// 'en' => 'en-GB', -// 'us' => 'en-US', -// 'fr' => 'fr', -// '/' => 'nl', -// ], -// ], + /** + * In case your locales contains both language as region references and this locale differs + * from the application's locale, you can opt to point this locale to the one as expected + * by the application. + * + * The value can be either: auto, true or false. + * - false default value. Keeps the locale values as set as above. + * - true locales will be converted to the ones defined below in the 'convert_locales_to' + * - auto if the format is in the RFC 4646 format with hyphen (en-US) or underscore (en_US), we will automatically convert the locale + * to the language portion. e.g. en-US + */ + 'convert_locales' => false, + + 'convert_locales_to' => [ + // e.g. 'nl-BE' => 'nl', + ], /* - * Define your canonical domains here. + * Define specific canonical domains here. + * * Specify unique domains for each locale to avoid crawler duplicate content warnings. * By default the current scope is used to determine the canonical domain. Here * you can specify a domain for a certain locale if the same locale is present in multiple domains. diff --git a/tests/ConvertLocale/ConvertLocaleTest.php b/tests/ConvertLocale/ConvertLocaleTest.php new file mode 100644 index 0000000..515064b --- /dev/null +++ b/tests/ConvertLocale/ConvertLocaleTest.php @@ -0,0 +1,63 @@ +detectLocaleAfterVisiting('http://convert.example.com/', [ + 'locales' => [ + 'convert.example.com' => [ + '/' => 'locale-ten', + ], + ], + 'convert_locales' => true, + 'convert_locales_to' => [ + 'locale-ten' => 'converted-ten' + ], + ]); + + $this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get()); + $this->assertEquals('converted-ten', app()->getLocale()); + } + + /** @test */ + function it_can_automatically_convert_locale_to_application_one() + { + $this->detectLocaleAfterVisiting('http://convert.example.com/', [ + 'locales' => [ + 'convert.example.com' => [ + '/' => 'locale-ten', + ], + ], + 'convert_locales' => 'auto', + ]); + + $this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get()); + $this->assertEquals('locale', app()->getLocale()); + } + + /** @test */ + public function explicit_conversion_has_priority_over_automatic() + { + $this->detectLocaleAfterVisiting('http://convert.example.com/', [ + 'locales' => [ + 'convert.example.com' => [ + '/' => 'locale-ten', + ], + ], + 'convert_locales' => 'auto', + 'convert_locales_to' => [ + 'locale-ten' => 'converted-ten' + ], + ]); + + $this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get()); + $this->assertEquals('converted-ten', app()->getLocale()); + } +} diff --git a/tests/Values/LocaleTest.php b/tests/Values/LocaleTest.php index 7a95daf..0d6debb 100644 --- a/tests/Values/LocaleTest.php +++ b/tests/Values/LocaleTest.php @@ -19,6 +19,14 @@ public function it_can_compare() $this->assertTrue(Locale::from('nl')->equals(Locale::from('nl'))); $this->assertFalse(Locale::from('nl')->equals(Locale::from('fr'))); } + + /** @test */ + function it_can_auto_convert_to_language_locale_without_region() + { + $this->assertEquals(Locale::from('nl'), Locale::from('nl-BE')->withoutRegion()); + $this->assertEquals(Locale::from('nl'), Locale::from('nl_BE')->withoutRegion()); + $this->assertEquals(Locale::from('NL'), Locale::from('NL-NL')->withoutRegion()); + } /** @test */ public function it_prints_out_as_string()