Author: ARCANEDEV (arcanedev.maroc@gmail.com)
Inheritance: implements Arcanedev\Localization\Contracts\UrlInterface
Example #1
0
 /**
  * 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 = '';
     if (!($locale === $defaultLocale && $defaultHidden)) {
         $route = '/' . $locale;
     }
     if ($this->hasTranslation($transKey, $locale)) {
         $translation = $this->trans($transKey, $locale);
         $route = Url::substituteAttributes($attributes, $route . '/' . $translation);
     }
     return $route;
 }
Example #2
0
 /**
  * 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);
 }