mcamara / laravel-localization

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

Unable to transate nested dynamics route #889

Closed StefanoTesla closed 7 months ago

StefanoTesla commented 7 months ago

Describe the bug In my case I have the translated slug in the database, using a package to translate the models. After some search I deep into the solution given here https://github.com/mcamara/laravel-localization#translatable-route-parameters

My model is this:

class Test extends Model implements \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable
{

    public function getLocalizedRouteKey($locale)
    {
        dd("stop!");
        return $this->getTranslation('slug',$locale);
    }

}

But getLocalizedRouteKey in never fired.

The fron end is this:

<a rel="alternate" href="{{ LaravelLocalization::getLocalizedURL('en') }}">EN</a> <a rel="alternate" href="{{ LaravelLocalization::getLocalizedURL('fr') }}">FR</a>

Continuing with the documentation I tried to implement the resolveRouteBinding

class Test extends Model implements \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable
{

    public function getLocalizedRouteKey($locale)
    {
        dd("stop!");
        return $this->getTranslation('slug',$locale);
    }

    public function resolveRouteBinding($slug, $field = null)
    {
         dd("I'm here");
        return static::findByLocalizedSlug($slug)->first() ?? abort(404);
    }
}

But is never fired that olso.

The resoult is the model slug,using LaravelLocalization::getLocalizedURL('en') is equal for any locale

route.php EN

<?php

return [
    "pages"    =>  "houses",
    "page"    =>  "houses/{slug?}",
];

route.php FR

<?php

return [
    "pages"    =>  "maisons",
    "page"    =>  "maisons/{slug?}",
];

Example for slug beutiful-house in EN

LaravelLocalization::getLocalizedURL('en') return http://site.y/maisons/beutiful-house LaravelLocalization::getLocalizedURL('fr') return http://site.y/maisons/beutiful-house

I wnt to override the beutiful-house in french!

Laravel v10.33.0 (PHP v8.2.13) latest mcamara