Skip to content

Commit

Permalink
More refactoring and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 21, 2015
1 parent 62e873d commit ce3c715
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
13 changes: 13 additions & 0 deletions src/Contracts/RouteTranslatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public function getRouteNameFromPath($uri, $locale);
*/
public function findTranslatedRouteByPath($path, $locale);

/**
* Get URL from route name.
*
* @param string $locale
* @param string $defaultLocale
* @param string $transKey
* @param array $attributes
* @param bool|false $defaultHidden
*
* @return string
*/
public function getUrlFromRouteName($locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false);

/* ------------------------------------------------------------------------------------------------
| Check Functions
| ------------------------------------------------------------------------------------------------
Expand Down
30 changes: 9 additions & 21 deletions src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ private function findTranslatedRouteByUrl($url, $attributes, $locale)

// check if this url is a translated url
foreach ($this->routeTranslator->getTranslatedRoutes() as $translatedRoute) {
$routeName = $this->getUrlFromRouteName($locale, $translatedRoute, $attributes);
$translatedUrl = $this->getUrlFromRouteName($locale, $translatedRoute, $attributes);

if ($this->getNonLocalizedURL($routeName) == $this->getNonLocalizedURL($url)) {
if ($this->getNonLocalizedURL($translatedUrl) === $this->getNonLocalizedURL($url)) {
return $translatedRoute;
}
}
Expand All @@ -444,25 +444,13 @@ public function getUrlFromRouteName($locale, $transKey, $attributes = [])
{
$this->isLocaleSupportedOrFail($locale);

if ( ! is_string($locale)) {
$locale = $this->getDefaultLocale();
}

$route = '';

if (
! ($locale === $this->getDefaultLocale() && $this->isDefaultLocaleHiddenInUrl())
) {
$route = '/' . $locale;
}

if (
is_string($locale) &&
$this->routeTranslator->hasTranslation($transKey, $locale)
) {
$translation = $this->routeTranslator->trans($transKey, $locale);
$route = Url::substituteAttributes($attributes, $route . '/' . $translation);
}
$route = $this->routeTranslator->getUrlFromRouteName(
$locale,
$this->getDefaultLocale(),
$transKey,
$attributes,
$this->isDefaultLocaleHiddenInUrl()
);

// This locale does not have any key for this route name
if (empty($route)) return false;
Expand Down
31 changes: 30 additions & 1 deletion src/Utilities/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ public function findTranslatedRouteByPath($path, $locale)
return false;
}

/**
* Get URL from route name.
*
* @param string $locale
* @param string $defaultLocale
* @param string $transKey
* @param array $attributes
* @param bool|false $defaultHidden
*
* @return string
*/
public function getUrlFromRouteName($locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false)
{
if ( ! is_string($locale)) {
$locale = $defaultLocale;
}

$route = ($locale === $defaultLocale && $defaultHidden) ? '' : '/' . $locale;

if ( ! empty($locale) && $this->hasTranslation($transKey, $locale)) {
$translation = $this->trans($transKey, $locale);
$route = Url::substituteAttributes($attributes, $route . '/' . $translation);
}

return $route;
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
Expand All @@ -219,11 +246,13 @@ private function translate($key, $locale = null)

$translated = $this->translator->trans($key, [], '', $locale);

// @codeCoverageIgnoreStart
if ( ! is_string($translated)) {
throw new InvalidTranslationException(
"The translation key [$key] for locale [$locale] has returned an array instead of string."
"The translation key [$key] for locale [$locale] should return a string value."
);
}
// @codeCoverageIgnoreEnd

return $translated;
}
Expand Down

0 comments on commit ce3c715

Please sign in to comment.