From ed1c065b1ba15f1fc78e0e02a0f9cb49051ca152 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Mon, 21 Sep 2015 15:41:50 +0100 Subject: [PATCH] Refactoring RouteTranslator --- src/Utilities/RouteTranslator.php | 45 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index 4d9cf5d..094fde1 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -214,9 +214,13 @@ 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); } @@ -224,10 +228,6 @@ public function getUrlFromRouteName($locale, $defaultLocale, $transKey, $attribu return $route; } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ - */ /** * Get the translation for a given key. * @@ -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; } /* ------------------------------------------------------------------------------------------------ @@ -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 + } }