1
votes

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?

1
href="{{ url(LaravelLocalization::getCurrentLocale().'/garage') }}"Calin Blaga
@Calin yes, this will work, thx, but i have few hundreds link in code :( Is there smth more elegant than this way?user3529607
As far as I know, not without using named routes.Calin Blaga

1 Answers

1
votes

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.