I have created a bilingual laravel 5 application that contains two locales, en and ar.
What I want is for the site visitor to be able to change the language of the website by clicking on a link labeled with the language name.
Option 1:
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');
    }
}
Example : For authenticated user or guest in your controller
public function setLocale($locale){
 if(Auth::check()){
     $user = User::find(Auth::user()->id);
     $user->update(['locale'=>$locale]);
  }else{
    Session::put('locale',$locale);
  }
}
App::setLocale() hence we are going to use a Middleware to setLocale on each request.To understand how Laravel handles App::setLocale() here is the method in Illuminate\Foundation\Application.php that handles setting of locale
    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 show below:
/**
     * Set the default locale.
     *
     * @param  string  $locale
     * @return void
     */
    public function setLocale($locale)
    {
        $this->locale = $locale;
    }
As you can see nothing like caching or session is used to remember locale so we must set it on each request. So lets create a Middleware for it. I will 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);
    }
}
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'
        ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With