-
-
Notifications
You must be signed in to change notification settings - Fork 509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
routes.translation event listen doesn't work #322
Comments
+1 |
To print a language switcher you just need to add this code to your blade template.
Why are you using events for this? |
I am using the event because I have to translate the parameters present in the URL, as well as the routes themselves. We are in /gasoline/cars and through the /resources/lang/en/routes.php return [
'cars' => '{fuel}/cars'
]; /resources/lang/it/routes.php return [
'cars' => '{fuel}/macchine'
]; The strange behaviors are two.
Morover, the routes.translation event is fired with only one parameter, the attributes, so I really don't understand how the listener should know which locale use to translate the attributes (now I forced to italian) |
Any news here? I debugged the code but I some help to fix this issue |
+1 please, I need to solve the problem and I can pay for an help! |
@fede91it the problem is in the file LaravelLocalization.php # L855
The event is triggered only with an argument when the documentation says they are two $ locale, $ attributes @mcamara hopefully can fix the bug please! |
I have the exact same problem as @fede91it.
In my routes translation file I have the following line: return [
'program' => 'programmation/{category?}',
]; I'm displaying the language switcher using this: @foreach( LaravelLocalization::getSupportedLocales() as $localeCode => $properties )
<li>
<a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode) }}"{!! LaravelLocalization::getCurrentLocale() === $localeCode ? 'class="active"' : '' !!}>{{ $localeCode }}</a>
</li>
@endforeach And finally my event listener in EventServiceProvider.php Event::listen('routes.translation', function($locale, $attributes) {
if( array_key_exists('category', $attributes) ) {
$category = Category::whereTranslation('slug', $attributes['category'])->first();
if( $category && $category->hasTranslation($locale) ) {
$attributes['category'] = $category->translate($locale)->slug;
}
}
return $attributes;
}); I know the event is firing because I can dump my Category model. However it doesn't change anything in the localized URL.
But I get these results with the event listener whatever I write in it:
I know this issue has been closed but it looks like it still hasn't been solved. Or maybe I'm doing something wrong? UPDATE Unfortunately I didn't manage to solve this issue but I found the origin of the problem. $routeName = $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes);
if ($this->getNonLocalizedURL($routeName) == $this->getNonLocalizedURL($url)) {
return $translatedRoute;
} In my case, $url is equal to /programmation/spectacles but $routeName is equal to /programmation/shows because the attributes have been replaced by its english translation. I guess my only solution here is to overwrite this function so it can look for the attributes translations. |
Did you manage to solve this issue? I have the same problem where I'm trying to pull localized slug from the database and I know the event is firing but still doesn't translate.. EDIT |
I have a language switch bar that has to generate the URL for the current page in all supported languages.
To do that I have to subscribe to the routes.translation event, but isn't so simple.
When I execute this
with this
I get this error
Why? I followed your docs but doesn't work as expected.
I see that the first parameter is the attributes array, but should be the second, right?
See entire logs if needed.
The text was updated successfully, but these errors were encountered: