From 08b47fdb4bab6495647ed993d892f3f75f3c997a Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 27 Apr 2017 14:45:59 +0100 Subject: [PATCH] Adding the ability to force show on hidden default locale in URL --- _docs/3-Usage.md | 27 ++---- src/Contracts/Localization.php | 12 +-- src/Contracts/RouteTranslator.php | 3 +- src/Localization.php | 35 ++++---- src/Utilities/RouteTranslator.php | 5 +- tests/LocalizationTest.php | 132 ++++++++++++++++++++++-------- 6 files changed, 139 insertions(+), 75 deletions(-) diff --git a/_docs/3-Usage.md b/_docs/3-Usage.md index dc5bbd6..fc1c1fb 100644 --- a/_docs/3-Usage.md +++ b/_docs/3-Usage.md @@ -104,17 +104,14 @@ If you want to hide the default locale but always show other locales in the url, /** * Returns an URL adapted to $locale or current locale. * - * - * @param string|bool $locale - * @param string|false $url - * @param array $attributes + * @param string|null $locale + * @param string|null $url + * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false - * - * @throws UndefinedSupportedLocalesException - * @throws UnsupportedLocaleException */ -public function getLocalizedURL($locale = null, $url = null, $attributes = []) +public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false) // OR @@ -124,8 +121,6 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) * @param string $url * @param string|null $locale * - * @throws UnsupportedLocaleException - * * @return string */ public function localizeURL($url = null, $locale = null); @@ -157,13 +152,11 @@ It returns a clean URL of any localization. * @param string|bool $locale * @param string $transKey * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false - * - * @throws UndefinedSupportedLocalesException - * @throws UnsupportedLocaleException */ -public function getUrlFromRouteName($locale, $transKey, $attributes = []) +public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false) ``` It returns a route, localized to the desired locale using the locale passed. @@ -192,8 +185,6 @@ function localized_route($transRoute, array $attributes = [], $locale = null) * Return an array of all supported Locales. * * @return \Arcanedev\Localization\Entities\LocaleCollection - * - * @throws UndefinedSupportedLocalesException */ public function getSupportedLocales() ``` @@ -207,8 +198,6 @@ It returns all locales as a `Arcanedev\Localization\Entities\LocaleCollection` C * Get supported locales keys. * * @return array - * - * @throws UndefinedSupportedLocalesException */ public function getSupportedLocalesKeys() ``` @@ -370,8 +359,6 @@ public function setBaseUrl($url) * * @param string|bool $locale * - * @throws UndefinedSupportedLocalesException - * * @return bool */ public function isLocaleSupported($locale) diff --git a/src/Contracts/Localization.php b/src/Contracts/Localization.php index ae199c3..1f707e1 100644 --- a/src/Contracts/Localization.php +++ b/src/Contracts/Localization.php @@ -160,13 +160,14 @@ public function getNonLocalizedURL($url = null); /** * Returns an URL adapted to $locale. * - * @param string|bool $locale - * @param string|false $url - * @param array $attributes + * @param string|null $locale + * @param string|null $url + * @param array $attributes + * @param bool|bool $showHiddenLocale * * @return string|false */ - public function getLocalizedURL($locale = null, $url = null, $attributes = []); + public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false); /** * Create an url from the uri. @@ -187,10 +188,11 @@ public function createUrlFromUri($uri); * @param string|bool $locale * @param string $transKey * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getUrlFromRouteName($locale, $transKey, $attributes = []); + public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false); /* ----------------------------------------------------------------- | Check Methods diff --git a/src/Contracts/RouteTranslator.php b/src/Contracts/RouteTranslator.php index d2fe32e..f981b34 100644 --- a/src/Contracts/RouteTranslator.php +++ b/src/Contracts/RouteTranslator.php @@ -95,11 +95,12 @@ public function findTranslatedRouteByPath($path, $locale); * @param string $transKey * @param array $attributes * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ public function getUrlFromRouteName( - $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false + $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false, $showHiddenLocale = false ); /* ----------------------------------------------------------------- diff --git a/src/Localization.php b/src/Localization.php index f0b3224..3d54114 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -293,10 +293,11 @@ public function getNonLocalizedURL($url = null) * @param string|null $locale * @param string|null $url * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getLocalizedURL($locale = null, $url = null, $attributes = []) + public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false) { if (is_null($locale)) $locale = $this->getCurrentLocale(); @@ -314,7 +315,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) return $this->getUrlFromRouteName( $locale, $this->routeTranslator->getCurrentRoute(), - $attributes + $attributes, + $showHiddenLocale ); } @@ -325,7 +327,7 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) $locale && $translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->getCurrentLocale()) ) { - return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); + return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); } $baseUrl = $this->request()->getBaseUrl(); @@ -336,13 +338,12 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) ); if ($translatedRoute !== false) - return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); + return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); - if ( - ! empty($locale) && - ($locale !== $this->getDefaultLocale() || ! $this->isDefaultLocaleHiddenInUrl()) - ) { - $parsedUrl['path'] = $locale . '/' . ltrim($parsedUrl['path'], '/'); + if ( ! empty($locale)) { + if ($locale !== $this->getDefaultLocale() || ! $this->isDefaultLocaleHiddenInUrl() || $showHiddenLocale) { + $parsedUrl['path'] = $locale.'/'.ltrim($parsedUrl['path'], '/'); + } } $parsedUrl['path'] = ltrim(ltrim($baseUrl, '/') . '/' . $parsedUrl['path'], '/'); @@ -368,7 +369,9 @@ public function createUrlFromUri($uri) { $uri = ltrim($uri, '/'); - return empty($this->baseUrl) ? $this->app['url']->to($uri) : $this->baseUrl.$uri; + return empty($this->baseUrl) + ? $this->app[\Illuminate\Contracts\Routing\UrlGenerator::class]->to($uri) + : $this->baseUrl.$uri; } /** @@ -417,13 +420,14 @@ private function findTranslatedRouteByUrl($url, $attributes, $locale) /** * Returns an URL adapted to the route name and the locale given. * - * @param string $locale - * @param string $transKey - * @param array $attributes + * @param string|bool $locale + * @param string $transKey + * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getUrlFromRouteName($locale, $transKey, $attributes = []) + public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false) { $this->isLocaleSupportedOrFail($locale); @@ -432,7 +436,8 @@ public function getUrlFromRouteName($locale, $transKey, $attributes = []) $this->getDefaultLocale(), $transKey, $attributes, - $this->isDefaultLocaleHiddenInUrl() + $this->isDefaultLocaleHiddenInUrl(), + $showHiddenLocale ); // This locale does not have any key for this route name diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index 3747f17..941f7e9 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -201,18 +201,19 @@ public function findTranslatedRouteByPath($path, $locale) * @param string $transKey * @param array $attributes * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ public function getUrlFromRouteName( - $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false + $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false, $showHiddenLocale = false ) { if ( ! is_string($locale)) $locale = $defaultLocale; $url = ''; - if ( ! ($locale === $defaultLocale && $defaultHidden)) + if ( ! ($locale === $defaultLocale && $defaultHidden) || $showHiddenLocale) $url = '/'.$locale; if ($this->hasTranslation($transKey, $locale)) diff --git a/tests/LocalizationTest.php b/tests/LocalizationTest.php index 8693337..5c0d4b8 100644 --- a/tests/LocalizationTest.php +++ b/tests/LocalizationTest.php @@ -195,26 +195,6 @@ public function it_can_localize_url() /** @test */ public function it_can_get_localized_url() { - $this->assertSame( - $this->testUrlOne.'es/acerca', - localization()->getLocalizedURL('es', $this->testUrlOne.'en/about') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1/proyecto', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1/project') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1/proyecto/1', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1/project/1') - ); - $this->assertSame( - $this->testUrlOne.'en/about', - localization()->getLocalizedURL('en', $this->testUrlOne.'about') - ); $this->assertSame( $this->testUrlOne.localization()->getCurrentLocale(), localization()->getLocalizedURL() @@ -223,18 +203,7 @@ public function it_can_get_localized_url() app('config')->set('localization.hide-default-in-url', true); // testing default language hidden - $this->assertSame( - $this->testUrlOne.'es/acerca', - localization()->getLocalizedURL('es', $this->testUrlOne.'about') - ); - $this->assertSame( - $this->testUrlOne.'about', - localization()->getLocalizedURL('en', $this->testUrlOne.'about') - ); - $this->assertSame( - $this->testUrlOne, - localization()->getLocalizedURL() - ); + $this->assertNotEquals( $this->testUrlOne.localization()->getDefaultLocale(), localization()->getLocalizedURL() @@ -292,6 +261,27 @@ public function it_can_get_localized_url() ); } + /** + * @test + * + * @param bool $hideDefault + * @param bool $forceDefault + * @param string $locale + * @param string $url + * @param string $expected + * + * @dataProvider getLocalizedURLDataProvider + */ + public function it_can_get_localized_url_with_specific_format($hideDefault, $forceDefault, $locale, $url, $expected) + { + $this->app['config']->set('localization.hide-default-in-url', $hideDefault); + + $this->assertEquals( + $expected, + \localization()->getLocalizedURL($locale, $url, [], $forceDefault) + ); + } + /** @test */ public function it_can_get_url_from_route_name_translated() { @@ -554,4 +544,82 @@ public function makeCall($uri, array $server = []) { return $this->call('GET', $uri, [], [], [], $server); } + + /* ----------------------------------------------------------------- + | Providers + | ----------------------------------------------------------------- + */ + /** + * Provide data for `it_can_get_localized_url_with_specific_format`. + * + * @return array + */ + public function getLocalizedURLDataProvider() + { + $url = 'http://localhost/'; + + // [$hideDefault, $forceDefault, $locale, $url, $expected] + + return [ + // Do not hide default with [es] locale + [false, false, 'es', $url, $url.'es'], + [false, false, 'es', $url.'es', $url.'es'], + [false, false, 'es', $url.'en/about', $url.'es/acerca'], + [false, false, 'es', $url.'ver/1', $url.'es/ver/1'], + [false, false, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [false, false, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default with [en] locale + [false, false, 'en', $url.'en', $url.'en'], + [false, false, 'en', $url.'about', $url.'en/about'], + [false, false, 'en', $url.'ver/1', $url.'en/ver/1'], + [false, false, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [false, false, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + + // Hide default with [es] locale + [true, false, 'es', $url, $url.'es'], + [true, false, 'es', $url.'es', $url.'es'], + [true, false, 'es', $url.'en/about', $url.'es/acerca'], + [true, false, 'es', $url.'ver/1', $url.'es/ver/1'], + [true, false, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [true, false, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Hide default with [en] locale + [true, false, 'en', $url.'en', $url.''], + [true, false, 'en', $url.'about', $url.'about'], + [true, false, 'en', $url.'ver/1', $url.'ver/1'], + [true, false, 'en', $url.'view/1/project', $url.'view/1/project'], + [true, false, 'en', $url.'view/1/project/1', $url.'view/1/project/1'], + + // Do not hide default + forcing the show with [es] locale + [false, true, 'es', $url, $url.'es'], + [false, true, 'es', $url.'es', $url.'es'], + [false, true, 'es', $url.'en/about', $url.'es/acerca'], + [false, true, 'es', $url.'ver/1', $url.'es/ver/1'], + [false, true, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [false, true, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default + forcing the show with [en] locale + [false, true, 'en', $url.'en', $url.'en'], + [false, true, 'en', $url.'about', $url.'en/about'], + [false, true, 'en', $url.'ver/1', $url.'en/ver/1'], + [false, true, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [false, true, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + + // Do not hide default + forcing the show with [es] locale + [true, true, 'es', $url, $url.'es'], + [true, true, 'es', $url.'es', $url.'es'], + [true, true, 'es', $url.'en/about', $url.'es/acerca'], + [true, true, 'es', $url.'ver/1', $url.'es/ver/1'], + [true, true, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [true, true, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default + forcing the show with [en] locale + [true, true, 'en', $url.'en', $url.'en'], + [true, true, 'en', $url.'about', $url.'en/about'], + [true, true, 'en', $url.'ver/1', $url.'en/ver/1'], + [true, true, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [true, true, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + ]; + } }