How can I get the user to switch languages ββin Laravel 5?
Option 1:
- Store custom language in database, I have mine in users table. This is to avoid asking the user every time they log into your application. You can set 'en' as default. However, if the user is a guest, we keep the locale in the session.
So your migration might look like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('locale', 5)->default('en');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
- When a user or guest clicks on a specific language link, update the user's locale in the database, or save the guest's selection in the session
Example: For an authenticated user or guest in a controller
public function setLocale($locale){
if(Auth::check()){
$user = User::find(Auth::user()->id);
$user->update(['locale'=>$locale]);
}else{
Session::put('locale',$locale);
}
}
- We need to find a way to set the language for each request because Laravel does not store a set of locales with
App::setLocale()
, so we will use a setLocale middleware for each request.
To understand how Laravel handles App::setLocale()
, here is a method in Illuminate \ Foundation \ Application.php that handles the locale setting
public function setLocale($locale)
{
$this['config']->set('app.locale', $locale);
$this['translator']->setLocale($locale);
$this['events']->fire('locale.changed', array($locale));
}
This method calls another method in Translator.php below:
/**
* Set the default locale.
*
* @param string $locale
* @return void
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
As you can see, caching or session is used to remember the language, so we have to set it for every request. So let's create a Middleware for it. I'll call it LocaleMiddleware.
<?php namespace App\Http\Middleware;
use Closure, Session, Auth;
class LocaleMiddleware {
public function handle($request, Closure $next)
{
if(Auth::user()){
app()->setLocale(Auth::user()->locale);
}elseif($locale = Session::has('locale')){
app()->setLocale($locale);
}
return $next($request);
}
}
- Allows you to install middleware for each request by adding it to the middleware stack in app \ Http \ Kernel.php
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
'App\Http\Middleware\LocaleMiddleware'
];
source to share
You can achieve the above by referencing the Laravel documentation, this is the Localization section.
source to share