From 5b9ac4d2dd5a71ca3c6147dda9eaf3939343493e Mon Sep 17 00:00:00 2001 From: Fredrik Jungstedt Date: Fri, 22 Apr 2022 15:17:44 +0200 Subject: [PATCH 1/4] Allow setting of default locale. --- .../LaravelLocalization.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 1f9cbe8..34d1733 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -138,12 +138,7 @@ public function __construct() $this->url = $this->app['url']; // set default locale - $this->defaultLocale = $this->configRepository->get('app.locale'); - $supportedLocales = $this->getSupportedLocales(); - - if (empty($supportedLocales[$this->defaultLocale])) { - throw new UnsupportedLocaleException('Laravel default locale is not in the supportedLocales array.'); - } + $this->setDefaultLocale($this->configRepository->get('app.locale')); } /** @@ -414,6 +409,20 @@ public function getDefaultLocale() return $this->defaultLocale; } + /** + * Sets the default locale. + * + * @param $defaultLocale + * @throws UnsupportedLocaleException + */ + public function setDefaultLocale($defaultLocale) + { + if (empty($supportedLocales[$defaultLocale])) { + throw new UnsupportedLocaleException('Laravel default locale is not in the supportedLocales array.'); + } + $this->defaultLocale = $defaultLocale; + } + /** * Return locales mapping. * From e43a94b756581c93a347473725c7c11a7c59465b Mon Sep 17 00:00:00 2001 From: Fredrik Jungstedt Date: Fri, 22 Apr 2022 15:22:53 +0200 Subject: [PATCH 2/4] Fixed reference. --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 34d1733..a7e8131 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -417,6 +417,7 @@ public function getDefaultLocale() */ public function setDefaultLocale($defaultLocale) { + $supportedLocales = $this->getSupportedLocales(); if (empty($supportedLocales[$defaultLocale])) { throw new UnsupportedLocaleException('Laravel default locale is not in the supportedLocales array.'); } From 954ec09e034d7694e5b8ac91d56e47de205720db Mon Sep 17 00:00:00 2001 From: Fredrik Jungstedt Date: Fri, 22 Apr 2022 15:45:14 +0200 Subject: [PATCH 3/4] Set default locale tests. --- tests/LocalizerTests.php | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 80f83f6..30f1b69 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -880,4 +880,46 @@ public function testSetLocaleWithMapping() $this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom')); $this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en')); } + + public function testSetDefaultLocale() + { + $this->assertEquals( + app('laravellocalization')->getDefaultLocale(), $this->defaultLocale + ); + + app('laravellocalization')->setDefaultLocale('es'); + + $this->assertEquals( + app('laravellocalization')->getDefaultLocale(), 'es' + ); + } + + public function testSetInvalidDefaultLocale() + { + $this->expectException(\Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException::class); + app('laravellocalization')->setDefaultLocale('sv'); + } + + public function testSetDefaultLocaleHiddenInUrl() + { + app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); + + $this->assertEquals( + $this->test_url, + app('laravellocalization')->localizeURL('', $this->defaultLocale) + ); + + app('laravellocalization')->setDefaultLocale('es'); + + $this->assertNotEquals( + $this->test_url, + app('laravellocalization')->localizeURL('', $this->defaultLocale) + ); + + $this->assertEquals( + $this->test_url, + app('laravellocalization')->localizeURL('', 'es') + ); + + } } From 5b28f2690a57788111e70a1406193fc41cb5f932 Mon Sep 17 00:00:00 2001 From: Fredrik Jungstedt Date: Fri, 22 Apr 2022 15:50:10 +0200 Subject: [PATCH 4/4] Added additional @throws. --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index a7e8131..ce0e400 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -414,6 +414,7 @@ public function getDefaultLocale() * * @param $defaultLocale * @throws UnsupportedLocaleException + * @throws SupportedLocalesNotDefined */ public function setDefaultLocale($defaultLocale) {