Mcamara\LaravelLocalization\LaravelLocalization::getURLFromRouteNameTranslated PHP Méthode

getURLFromRouteNameTranslated() public méthode

Returns an URL adapted to the route name and the locale given
public getURLFromRouteNameTranslated ( string | boolean $locale, string $transKeyName, array $attributes = [] ) : string | false
$locale string | boolean Locale to adapt
$transKeyName string Translation key name of the url to adapt
$attributes array Attributes for the route (only needed if transKeyName needs them)
Résultat string | false URL translated
    public function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = array())
    {
        if (!$this->checkLocaleInSupportedLocales($locale)) {
            throw new UnsupportedLocaleException('Locale \'' . $locale . '\' is not in the list of supported locales.');
        }
        if (!is_string($locale)) {
            $locale = $this->getDefaultLocale();
        }
        $route = "";
        if (!($locale === $this->defaultLocale && $this->hideDefaultLocaleInURL())) {
            $route = '/' . $locale;
        }
        if (is_string($locale) && $this->translator->has($transKeyName, $locale)) {
            $translation = $this->translator->trans($transKeyName, [], "", $locale);
            $route .= "/" . $translation;
            $route = $this->substituteAttributesInRoute($attributes, $route);
        }
        if (empty($route)) {
            // This locale does not have any key for this route name
            return false;
        }
        return rtrim($this->createUrlFromUri($route));
    }

Usage Example

Exemple #1
0
 /**
  * Returns an URL adapted to the route name and the locale given
  *
  * @throws SupportedLocalesNotDefined
  * @throws UnsupportedLocaleException
  * @param string|boolean $locale Locale to adapt
  * @param string $transKeyName Translation key name of the url to adapt
  * @param array $attributes Attributes for the route (only needed if transKeyName needs them)
  * @return string|false URL translated
  * @static 
  */
 public static function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = array())
 {
     return \Mcamara\LaravelLocalization\LaravelLocalization::getURLFromRouteNameTranslated($locale, $transKeyName, $attributes);
 }