laravel-localization
laravel-localization copied to clipboard
getLocalizedURL does not translate slug
When i try to get the localized URL of a model with translated route and slug, it returns the URL with the same slug (non localized), example:
LaravelLocalization::getLocalizedURL('en', 'aparato/caldera', [], true);
returns
http://site.test/en/device_type/caldera
instead of
http://site.test/en/device_type/boiler
I have the translated routes as follows:
in resources/lang/en/routes.php:
"device_type" => "device_type/{deviceType}",
in resources/lang/es/routes.php:
"device_type" => "aparato/{deviceType}",
and my routes are defined like this:
Route::prefix(LaravelLocalization::setLocale())
->middleware('localize', 'localeSessionRedirect', 'localizationRedirect')
->group( function ()
{
...
Route::get(LaravelLocalization::transRoute('routes.device_type'), 'HomeController@deviceType')->name('device_type');
...
}
I use route model binding and implement LocalizedUrlRoutable
, and have implemented
resolveRouteBinding
and getLocalizedRouteKey
in my model so that they return what i think they should, tested in tinker:
>>> $dt->resolveRouteBinding('boiler')->getLocalizedRouteKey('es')
=> "caldera"
>>> $dt->resolveRouteBinding('boiler')->getLocalizedRouteKey('en')
=> "boiler"
>>> $dt->resolveRouteBinding('caldera')->getLocalizedRouteKey('es')
=> "caldera"
>>> $dt->resolveRouteBinding('caldera')->getLocalizedRouteKey('en')
=> "boiler"
so when i try to get the localized url, i get the following:
>>> LaravelLocalization::getLocalizedURL('en', 'aparato/caldera', [], true);
=> "http://site.test/en/device_type/caldera"
>>> LaravelLocalization::getLocalizedURL('en', '/es/aparato/caldera', [], true);
=> "http://site.test/en/device_type/caldera"
when I'd expect to get "http://site.test/en/device_type/boiler"
I'm using laravel v. 7.6.1 and laravel-localization v. 1.5.0.
Using middleware localize
, localeSessionRedirect
and localizationRedirect
in my routes.
Config options:
'supportedLocales' => [ 'es' => .... , 'en' => ... ],
'useAcceptLanguageHeader' => true,
'hideDefaultLocaleInURL' => true,
'localesOrder' => [],
'localesMapping' => [],
Is this the expected behaviour? Any idea why it does not translate the slug? Thank you.
Did you follow all steps mentioned in the video tutorial https://www.youtube.com/watch?v=B1AUqCdizgc&feature=youtu.be ?
Yes, I think so. In the video it shows only getting an object by any slug, and that works ok. The problem I have is related to URL translation. I noticed that it works ok if I call 'getLocalizedURL' from a view, but shows the problem described in the issue when called from tinker or from a controller. While debugging i noticed that (when called from a controller) the route parameter is a string ('caldera' to follow my example) instead of the model instance, so when it goes to https://github.com/mcamara/laravel-localization/blob/13f418e481ed06f482e4fca87ec5ff67c2949373/src/Mcamara/LaravelLocalization/LaravelLocalization.php#L641 that expression returns false and the translated slug can not be retrieved. When called from a view the parameter is a model instance, so 'getLocalizedRouteKey' is called on the model, returning the correct slug. I don't know enough of the inner workings of Laravelor or how the route parameters work, maybe it can only work after a response is sent ,but not on the request. I did not see anything related to this in the documentation, so I just expected it to work anywhere. Thank you for your time.
If you are not using it yet, try Route Model Binding. Route Model Binding
I had the same issue and it was resolved when I changed my routes.php
and my controller method.
If you use the routes you mentioned, it could work if you change your controller method, for example:
function xxxx(DeviceType $deviceType) {...}
I have the same issue, did you fix it?