mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.32k stars 507 forks source link

Nested slugs not translatable in language switcher #870

Open Elkex opened 1 year ago

Elkex commented 1 year ago

Hello,

This package seems to work perfectly to translate slugs on the first level of the URL, but seems to fail for the second level of a nested URL.

For instance: /en/teachers translates perfectly in the language switcher to /fr/professeurs

However, if I have a second level page: /en/teachers/apply does not translate to /fr/professeurs/appliquer, it shows /fr/teachers/apply

I implemented the package accordingly with the Twill CMS, but once the slug is nested, it does not seem to translate correctly.

vgaldikas commented 1 year ago

I have same issue. I did some digging into the source code, and it appears that the issue arises because for some reason, when you add the second dynamic segment the getRouteNameFromAPath stops resolving the $routeName in Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes

vgaldikas commented 1 year ago

Hey @Elkex I got it working as expected.

initially I was defining route as such:

'categories.index' => 'c/{category}/{manufacturer:slug?}'

After some trial and error i ended up doing the following:

define route as such:

'categories.index' => 'c/{category}/{manufacturer?}'

Then add these 2 methods to my Manufacturer model:

    public function getRouteKeyName(): string
    {
        return 'slug';
    }

    public function getLocalizedRouteKey($locale)
    {
        return $this->slug;
    }

And you got to implement the `Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable' on your model too.

So far didn't found any problems

vgaldikas commented 1 year ago

Actually @mcamara maybe could you weigh in, is this i a potential bug, or is this expected behavior? I could take a look at either creating pull request for documentation detailing how to make this work, or if it's a bug, I would maybe debug further to get it to work both ways?

mcamara commented 1 year ago

Thanks for asking @vgaldikas , feel free to create a PR and @iwasherefirst2 or I will review it.

StefanoTesla commented 7 months ago

Actually @mcamara maybe could you weigh in, is this i a potential bug, or is this expected behavior? I could take a look at either creating pull request for documentation detailing how to make this work, or if it's a bug, I would maybe debug further to get it to work both ways?

Did you understand the problem? I don't really understand how to solve the same problem..