简体   繁体   中英

Laravel localize url helper

I need to localize my website. I am using this package: https://github.com/mcamara/laravel-localization My route group is:

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController@garage']);
//OTHER ROUTES
});

And i want to localize link to this route. If i am using

href="{{ route('garage') }}

everything is ok, link looks like "www.example.com/locale/garage". But if i am using

href="{{ url('/garage') }}

my localization doesn't work, link looks like "www.example.com/garage". Is there some way to localize links made with URL helper?

LaravelLocalization package includes a middleware object to redirect all non-localized routes to the corresponding "localized".

So, if a user navigates to http://www.example.com/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect him automatically to http://www.example.com/en/test .

To active this functionality go to app/Http/Kernel.php and add these classes in protected $routeMiddleware = [] at the beginning and then in your routes file bring these changes:

Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController@garage']);
//OTHER ROUTES
});

And your problem will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM