/** * Returns an URL adapted to $locale or current locale. * * @todo: Refactor this beast * * @param string|null $locale * @param string|null $url * @param array $attributes * * @return string|false */ public function getLocalizedURL($locale = null, $url = null, $attributes = []) { if (is_null($locale)) { $locale = $this->getCurrentLocale(); } $this->isLocaleSupportedOrFail($locale); if (empty($attributes)) { $attributes = Url::extractAttributes($url); } if (empty($url)) { if ($this->routeTranslator->hasCurrentRoute()) { if (empty($attributes)) { $attributes = $this->request()->route()->parameters(); } return $this->getUrlFromRouteName($locale, $this->routeTranslator->getCurrentRoute(), $attributes); } $url = $this->request()->fullUrl(); } if ($locale && ($translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->getCurrentLocale()))) { return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); } $baseUrl = $this->request()->getBaseUrl(); $parsedUrl = parse_url($url); $translatedRoute = $this->routeTranslator->getTranslatedRoute($baseUrl, $parsedUrl, $this->getDefaultLocale(), $this->getSupportedLocales()); if ($translatedRoute !== false) { return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); } if (!empty($locale) && ($locale !== $this->getDefaultLocale() || !$this->isDefaultLocaleHiddenInUrl())) { $parsedUrl['path'] = $locale . '/' . ltrim($parsedUrl['path'], '/'); } $parsedUrl['path'] = ltrim(ltrim($baseUrl, '/') . '/' . $parsedUrl['path'], '/'); $parsedUrl['path'] = rtrim($parsedUrl['path'], '/'); $url = Url::unparse($parsedUrl); if (filter_var($url, FILTER_VALIDATE_URL)) { return $url; } if (empty($url)) { $url = $parsedUrl['path']; } return $this->createUrlFromUri($url); }