Skip to content

Commit

Permalink
Refactoring RouteTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 21, 2015
1 parent ce3c715 commit ed1c065
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/Utilities/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,20 @@ public function getUrlFromRouteName($locale, $defaultLocale, $transKey, $attribu
$locale = $defaultLocale;
}

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

if ( ! empty($locale) && $this->hasTranslation($transKey, $locale)) {
if ( ! ($locale === $defaultLocale && $defaultHidden)) {
$route = '/' . $locale;
}

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

return $route;
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Get the translation for a given key.
*
Expand All @@ -244,17 +244,10 @@ private function translate($key, $locale = null)
$locale = $this->translator->getLocale();
}

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

// @codeCoverageIgnoreStart
if ( ! is_string($translated)) {
throw new InvalidTranslationException(
"The translation key [$key] for locale [$locale] should return a string value."
);
}
// @codeCoverageIgnoreEnd
$translation = $this->translator->trans($key, [], '', $locale);
$this->checkTranslation($key, $locale, $translation);

return $translated;
return $translation;
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -283,4 +276,24 @@ public function hasTranslation($key, $locale = null)
{
return $this->translator->has($key, $locale);
}

/**
* Check the translation.
*
* @param string $key
* @param string $locale
* @param mixed $translation
*
* @throws InvalidTranslationException
*/
private function checkTranslation($key, $locale, $translation)
{
// @codeCoverageIgnoreStart
if ( ! is_string($translation)) {
throw new InvalidTranslationException(
"The translation key [$key] for locale [$locale] should return a string value."
);
}
// @codeCoverageIgnoreEnd
}
}

0 comments on commit ed1c065

Please sign in to comment.