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.
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:
Example : For authenticated user or guest in your controller
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 localeThis method calls another method in Translator.php show below:
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.
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' ];
You can achieve the above by referring to the Laravel documentation, it is the Localization section.