laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

routes.translation event

Open lukkoro opened this issue 5 years ago • 0 comments

Changed where the event routes.translation is firing in order to fix the issue of attributes not being translated even though the event was firing.

public function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = [], $forceDefaultLocation = false)
    {
        if (!$this->checkLocaleInSupportedLocales($locale)) {
            throw new UnsupportedLocaleException('Locale \''.$locale.'\' is not in the list of supported locales.');
        }

        if (!\is_string($locale)) {
            $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())) {
            $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), '/');
    }

lukkoro avatar Jun 23 '19 10:06 lukkoro