From fc2188a82611507f85f2219664b0d789f3d38212 Mon Sep 17 00:00:00 2001 From: Christian Ahidjo Date: Tue, 23 Jan 2018 10:10:21 -0500 Subject: [PATCH] Exclude current locale from the language selector (#521) * allow exclusion of current locale * refactor exclusion of current locale * update README.md * add param in docblock --- README.md | 17 ++++++++++++++++ .../LaravelLocalization.php | 20 +++++++++++-------- tests/LocalizerTests.php | 17 ++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c03e61e..f823960 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,9 @@ public function getSupportedLocales() //Should be called like this: {{ LaravelLocalization::getSupportedLocales() }} + +// To exclude current locale from returned array +{{ LaravelLocalization::getSupportedLocales(true) }} ``` This function will return all supported locales and their properties as an array. @@ -384,6 +387,20 @@ If you're supporting multiple locales in your project you will probably want to @endforeach ``` + +If you want to exclude current locale from the list, in the language selector. +``` + +``` + Here default language will be forced in getLocalizedURL() to be present in the URL even `hideDefaultLocaleInURL = true`. ## Translated Routes diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index c67a053..a8ef365 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -368,25 +368,29 @@ public function getDefaultLocale() /** * Return an array of all supported Locales. * + * @param boolean $excludeCurrent * @throws SupportedLocalesNotDefined * * @return array */ - public function getSupportedLocales() + public function getSupportedLocales($excludeCurrent = false) { - if (!empty($this->supportedLocales)) { - return $this->supportedLocales; + if (empty($this->supportedLocales)) { + $this->supportedLocales = $this->configRepository->get('laravellocalization.supportedLocales'); } - $locales = $this->configRepository->get('laravellocalization.supportedLocales'); - - if (empty($locales) || !is_array($locales)) { + if (empty($this->supportedLocales) || !is_array($this->supportedLocales)) { throw new SupportedLocalesNotDefined(); } - $this->supportedLocales = $locales; + if ($excludeCurrent) { + $locales = $this->supportedLocales; + unset($locales[$this->currentLocale]); + + return $locales; + } - return $locales; + return $this->supportedLocales; } /** diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 53528eb..eb8c00a 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -1,6 +1,7 @@ supportedLocales); + $currentLocale = $this->defaultLocale; + + $this->assertThat( + LaravelLocalization::getSupportedLocales(true), + $this->logicalNot( + $this->arrayHasKey($currentLocale) + ) + ); + $this->assertCount($allLocalesCount - 1, LaravelLocalization::getSupportedLocales(true)); + + $this->assertCount($allLocalesCount, LaravelLocalization::getSupportedLocales(false)); + } + public function testGetCurrentLocaleName() { $this->assertEquals(