Skip to content

Commit

Permalink
Adding the ability to force show on hidden default locale in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Apr 27, 2017
1 parent 2b8d8ee commit 08b47fd
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 75 deletions.
27 changes: 7 additions & 20 deletions _docs/3-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
```
Expand All @@ -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()
```
Expand Down Expand Up @@ -370,8 +359,6 @@ public function setBaseUrl($url)
*
* @param string|bool $locale
*
* @throws UndefinedSupportedLocalesException
*
* @return bool
*/
public function isLocaleSupported($locale)
Expand Down
12 changes: 7 additions & 5 deletions src/Contracts/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

/* -----------------------------------------------------------------
Expand Down
35 changes: 20 additions & 15 deletions src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -314,7 +315,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [])
return $this->getUrlFromRouteName(
$locale,
$this->routeTranslator->getCurrentRoute(),
$attributes
$attributes,
$showHiddenLocale
);
}

Expand All @@ -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();
Expand All @@ -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'], '/');
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Utilities/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
132 changes: 100 additions & 32 deletions tests/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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'],
];
}
}

0 comments on commit 08b47fd

Please sign in to comment.