Laravel 5 :: Switch Language

So I want to have a button that switches from one language to the other and consequently translates the website back and forth. Already added the secondary idiom in the lang folder and now I can't figure how to set the routes for it to happen. Was following a tutorial but it was for version 4 and there's no longer the filters.php file for it to work. The documentation is very vague and doesn't make it work:

			<select name="locale">
				<option value="/lang/en">English</option>
				<option value="/lang/pt">Portuguese</option>
			<input type="submit" value="Choose">
                             {{ Form::token() }}
			<p>{{ trans('homepage.home') }}</p>

I'm missing something but I'm not sure what exactly. Thanks.

marcod replied 4 years ago

You need to set the app's locale to the choosen language, e.g.: \App::setLocale($language);

In our applications all urls are prefixed with the language, for instance: /en/user/list And a custom middleware just sets the locale on every request using the first URL segment.

Perseus0 replied 4 years ago

Thanks for the reply. Please break it down for me because I'm not a Laravel or PHP expert.

I want to have two idioms: EN and PT.

Have set the middleware in App/Html/MiddlewareApp.php:

<?php namespace App\Http\Middleware;

use Closure;

class App {

 * Handle an incoming request.
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
public function handle($request, Closure $next)
       \Session::put('locale', \Config::get('app.locale'));


    return $next($request);


In kernel.php:



<?php namespace App\Http\Controllers;

use App\Http\Requests;

use App\Http\Controllers\Controller; use Auth; use Session; use Input; use Redirect; use Illuminate\Http\Request;

class LanguageController extends Controller {

public function index()
	Session::set('locale', Input::get('locale'));
	return Redirect::back();



Route::post('/language', array(

'before' => 'csrf',
'as'	=>	'language-choose',
'uses'	=>	'[email protected]'


In Config/App.php I have:

'locale' => 'en',

Do I need to add 'pt' ?

Thank you.

jartaud replied 4 years ago

You are triying to reinvent the wheel dude. Check out this package:

Perseus0 replied 4 years ago

jartaud said:

You are triying to reinvent the wheel dude. Check out this package:

Yes, have worked on that package since yesterday. It's not fully working but I'm getting there.

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us