diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 9e643db..d96df0c 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -261,6 +261,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $ $urlQuery = parse_url($url, PHP_URL_QUERY); $urlQuery = $urlQuery ? '?'.$urlQuery : ''; + + if (empty($url)) { if (!empty($this->routeName)) { return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes, $forceDefaultLocation); @@ -272,10 +274,14 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $ $url = preg_replace('/'. preg_quote($urlQuery, '/') . '$/', '', $url); } + + if ($locale && $translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->currentLocale)) { return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation).$urlQuery; } + + $base_path = $this->request->getBaseUrl(); $parsed_url = parse_url($url); $url_locale = $this->getDefaultLocale(); @@ -303,6 +309,7 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $ $parsed_url['path'] = ltrim($parsed_url['path'], '/'); if ($translatedRoute = $this->findTranslatedRouteByPath($parsed_url['path'], $url_locale)) { + return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation).$urlQuery; } @@ -352,6 +359,16 @@ public function getURLFromRouteNameTranslated($locale, $transKeyName, $attribute $locale = $this->getDefaultLocale(); } + $response = event('routes.translation', [$locale, $attributes]); + + if (!empty($response)) { + $response = array_shift($response); + } + + if (\is_array($response)) { + $attributes = array_merge($attributes, $response); + } + $route = ''; if ($forceDefaultLocation || !($locale === $this->defaultLocale && $this->hideDefaultLocaleInURL())) { @@ -907,15 +924,7 @@ protected function extractAttributes($url = false, $locale = '') } $attributes = $this->normalizeAttributes($this->router->current()->parameters()); - $response = event('routes.translation', [$locale, $attributes]); - - if (!empty($response)) { - $response = array_shift($response); - } - - if (\is_array($response)) { - $attributes = array_merge($attributes, $response); - } + } return $attributes;