mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.38k stars 515 forks source link

Translatable route parameters #720

Open AlePix opened 4 years ago

AlePix commented 4 years ago

Switching language parameters fail to show in url. I'm trying to do a simple editing pages with translatable routes,it seems that everything works, the parameter is intercepted, the user updates his data but when I switch to the language from Iocale to another i receive this url as a response : set 'it' as local language in app.php http://booking:8888/en/EditProfile/%7Bid%7D ->with language set to 'en' from menu unlike this that i receive with 'it' setting as local in app.php: http://booking:8888/it/ModificaProfilo/21 ->with language set to 'it' from menu Viceversa if i set 'en' as local language in app.php i'm getting: http://booking:8888/it/ModificaProfilo/%7Bid%7D ->with language set to 'it' from menu and http://booking:8888/en/EditProfile/21 -> with language set to 'en' from menu

Web.php

Route::group([
    'prefix' => LaravelLocalization::setLocale(), 
    'where' => ['locale' => '[a-zA-Z]{2}'],
    'middleware' => [ 'localeSessionRedirect','localizationRedirect', 'localeViewPath','localize' ]], function() {
//Frontend
//Route Home Page
Route::get(LaravelLocalization::transRoute('/'), function () {
    return view('frontendViews.home');
})->name('home');
//Ruote Login Page
Route::get(LaravelLocalization::transRoute('routes.login'), function () {
    return view('auth.login');
})->name('login');
//Ruote Register Page
Route::get(LaravelLocalization::transRoute('routes.register'), function () {
    return view('auth.register');
})->name('register');
//Ruote List boats Page
Route::get(LaravelLocalization::transRoute('routes.listBoats'),('Boats'), function () {
    //return view('frontendViews.listBoats');
})->name('listBoats');
//Ruote Contacts Page
Route::get(LaravelLocalization::transRoute('routes.contact'), function () {
    return view('frontendViews.contact');
})->name('contact');
//Ruote Details Boat Page
Route::get(LaravelLocalization::transRoute('routes.detailedBoat'),('DetailedBoat'), function (\App\Boat $id) {
    return $id;
})->name('detailedBoat');

Auth::routes();

//Backend
**Route::get(LaravelLocalization::transRoute('routes.user'),('Users@index'));
Route::get(LaravelLocalization::transRoute('routes.edituser'),('Users@edit'), function (\App\User $id) {
    return $id;**
});
**Route::patch(LaravelLocalization::transRoute('routes.updateuser'),('Users@update'), function (\App\User $id) {
    return $id;**
});
});

Ruotes.php files for relative language:

<?php
// resources/lang/en/routes.php
return [
    "login"    =>  "Login",
    "register"  =>  "Register",
    "listBoats" => "Boats",
    "contact" => "Contact",
    "detailedBoat" => "DetailBoat/{id}",
    "user" => "Dashboard",
    "edituser" => "EditProfile/{id}",
    "updateuser" => "UpdateProfile/{id}",

];
<?php
// resources/lang/it/routes.php
return [
    "login"    =>  "Entra",
    "register"  =>  "Registrati",
    "listBoats" => "Barche",
    "contact" => "Contatti",
    "detailedBoat" => "DettaglioBarca/{id}",
    "user" => "Profilo",
    "edituser" => "ModificaProfilo/{id}",
    "updateuser" => "AggiornaProfilo/{id}",

];

Example of action used in different views:

action="{{ LaravelLocalization::getLocalizedURL(null, trans('routes.updateuser'), ['id' => $user->id]) }}

I really don't understand what's the problem, also because everything is working except that string that should be attached to the url.

RomkaLTU commented 3 years ago

I don't know, but I'm using this approach:

Route::get(LaravelLocalization::transRoute('routes.doctor'), ['App\Http\Controllers\DoctorController', 'show'])->name('doctor');

Ant inside blade template:

<a href="{{ route('doctor', $doctor) }}" ...