How to make multilanguage in Laravel correctly, so that the language would be in the route, such as pl/team/1 ?

    2 answers 2

    The easiest way is to use special middleware: laravel-localization

    We need to wrap the group of routes that you want to prefix into the function:

     // app/Http/routes.php // тут обрабатываются URL с префиксами. Route::group(['prefix' => LaravelLocalization::setLocale()], function() { /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/ Route::get('/', function() { return View::make('hello'); }); Route::get('test',function(){ return View::make('test'); }); }); /** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/ 

    For more information, see the documentation.

      Use prefix with middleware - something like this code will be in routes.php

       Route::get('/signout', ['as' => 'auth.signout', 'uses' => 'Auth\AuthController@signout']); Route::get('/login', ['as' => 'auth.login', 'uses' => 'Auth\AuthController@login']); Route::post('/create', ['as' => 'auth.create', 'uses' => 'Auth\AuthController@create']); Route::post('/signin', ['as' => 'auth.signin', 'uses' => 'Auth\AuthController@signin']); Route::get('/', function(){ return Redirect::to('/ro'); }); Route::group(['middleware' => 'isAdmin:admin', 'namespace' => 'Admin'], function(){ //... }); Route::group(['middleware' => 'Localization'], function() { Route::get('/{lang}/offers/view/{offer_id}', ['as' => 'offers.view', 'uses' => 'OffersController@view']); Route::get('/{lang}/news/view/{offer_id}', ['as' => 'news.view', 'uses' => 'NewsController@view']); }); Route::group(['prefix' => '{lang}', 'middleware' => 'Localization'], function() { Route::get('/', ['as' => 'site.index', 'uses' => 'SiteController@index']); Route::get('/contacts', ['as' => 'site.contacts', 'uses' => 'SiteController@contacts']); Route::get('/offers', ['as' => 'offers.index', 'uses' => 'OffersController@index']); Route::get('/news', ['as' => 'news.index', 'uses' => 'NewsController@index']); Route::get('/info', ['as' => 'site.info', 'uses' => 'SiteController@info']); Route::get('/info2', ['as' => 'site.info2', 'uses' => 'SiteController@info2']); Route::get('/info3', ['as' => 'site.info3', 'uses' => 'SiteController@info3']); Route::get('/info4', ['as' => 'site.info4', 'uses' => 'SiteController@info4']); Route::get('/payments', ['as' => 'site.payments', 'uses' => 'SiteController@payments']); }); 
      • Stanislav I did something like this, but I thought there might be some kind of standard feature in Laravel for that. Now, in all methods where route parameters are used, for example id, I had to describe the language as the first argument because it does not distinguish between parameters - Jonny Manowar
      • @JonnyManowar well, this is probably not a bug, but a feature - after all, in these methods you may need to know which language - you just assume that for all languages ​​there will be identical behavior and content, as you most likely now have, while in fact, everything could be completely different - Stanislav Belichenko
      • Not enough imagination to come up with such a situation. - Jonny Manowar
      • @JonnyManowar trying hard :) The first thing that comes to mind is different payment systems in different countries. Different details of accounts, different addresses of offices, in general, you can continue for a long time - Stanislav Belichenko