I need localisation in my app. The way I prefer is that the first segment in the URL contains the current language. For example: /en/blog, /nl/blog, etc.
Laravel supports localisation and I'm using this to translate my static strings. However, I can't get a proper setup working for my routes.
I found https://github.com/mcamara/laravel-localization but that immediately a huge package (and still makes things complicated). All I want are some simple routes that do the following:
- Add a localised version of every page. Eg:
/en/blog, /nl/blog
- redirect routes without locale to the default locale (
/blog to /en/blog)
What I currently have:
// routes.php
Route::group(['middleware' => ['web']], function () {
Route::group(['prefix' => '{locale}'], function($locale) {
Route::get('', function () {
return view('welcome');
});
Route::auth();
Route::get('home', 'HomeController@index');
Route::resource('blog', 'BlogController', ['only' => ['index', 'show']]);
});
});
This however produces routes with incorrect names: {locale}.blog.index. This means generating routes with route('blog.index') doesn't work. Also, this approach forces me to add the $locale parameter to every controller method.
Can someone share some thoughts how to get this working nice and easy? Thank you!