Skip to content
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

getLocalizedURL does not translate slug #718

Open
nlucia opened this issue Apr 15, 2020 · 4 comments
Open

getLocalizedURL does not translate slug #718

nlucia opened this issue Apr 15, 2020 · 4 comments
Labels

Comments

@nlucia
Copy link

nlucia commented Apr 15, 2020

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.

@iwasherefirst2
Copy link
Collaborator

Did you follow all steps mentioned in the video tutorial https://www.youtube.com/watch?v=B1AUqCdizgc&feature=youtu.be ?

@nlucia
Copy link
Author

nlucia commented Apr 24, 2020

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

if ($value instanceOf Interfaces\LocalizedUrlRoutable) {
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.

@rsmondejar
Copy link

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) {...}

@cdanielzt
Copy link

I have the same issue, did you fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants