How To Implement Localization in Laravel 5.8

This laravel 5.8 tutorial help to create multilingual web application.We will create different languages messages and display to user based on selected locale information.We will create application that help to serve fr(France) and en(English) languages message.The default laravel language is english(en), Although not everyone speaks or understands English and many countries have more than one official language.

There are many third party packages available to create multilingual website using laravel, But laravel provides inbuilt localization support to create multilingual website.

Create Multilingual Website Using Laravel Localization

The laravel provides localization features for multilingual application.The language string message are stored into in the files resources/lang directory. Within that directory, there should be a subdirectory for each language supported by the application.

We will gone through following features into this tutorial-

  • We will create different languages folder into resources/lang like fr/en.
  • Create middleware to inject locale information into each request.
  • Set user selected locale option into the laravel session.
  • Display selected language message into blade view file.

We will create sample laravel application using below command –
 laravel new laravel_localization_example

Now we will cd laravel_localization_example folder.By default, The Laravel application’s local language is English or en type.You can find it on the config/app.php file.

'locale' => 'en',

You can change this default as per your locale or personalize language.

Create Languages folder into Laravel Application

There is en type subdirectory already available into resources/lang folder.The resources/lang/en folder have some files like auth.php, pagination.php, passwords.php, validation.php.

All the files returns key:value pair array data.

we will add French(fr) language support for laravel application, Now create a fr folder into resources/lang folder.Also create constant.php file into en/ and fr/ folder, please make sure the key name will be same into across the different languages folder file.We just change the value of constant.

//resources/lang/en/constant.php return [‘welcome’ :’Welcome Laravel Multilingual Application’];

//resources/lang/en/constant.php

return [‘welcome’ :’Welcome Laravel Multilingual Application’];

Create same constant into France language –

//resources/lang/fr/constant.php return [‘welcome’ :’Bienvenue application multilingue Laravel’];

//resources/lang/fr/constant.php

return [‘welcome’ :’Bienvenue application multilingue Laravel’];

How to set and get Locale Into Laravel Application

The laravel have in-built helper method to get and set locale information in application.

How to set Locale Language Into Laravel

I can use set locale language using below method –

$locale =’fr’; App::setLocale($locale);

How to get Locale Language Into Laravel

I can use get locale language using below method –
App::getLocale($locale);

We can put Local information into session using below controller method –

public function setLang($locale) { App::setLocale($locale); session()->put(‘locale’, $locale); return redirect()->back(); }

public function setLang($locale)

session()->put(‘locale’, $locale);

return redirect()->back();

Create Middleware to set Localization in Laravel For Request

We can use middleware to set localeization for laravel application.Created a new middleware using below command –
 php artisan make:middleware Localization

Above command will create a Localization.php file into app/Http/Middleware folder.

Create a new method inside app/Http/Middleware/Localization.php file to set Locale lang of application, First we will check locale is set if not then set into session otherwise skip.

<?php // Localization.php namespace App\Http\Middleware; use Closure; use App; class Localization { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (session()->has(‘locale’)) { App::setLocale(session()->get(‘locale’)); } return $next($request); } }

namespace App\Http\Middleware;

* Handle an incoming request.

* @param \Illuminate\Http\Request $request

public function handle($request, Closure $next)

if (session()->has(‘locale’)) {

App::setLocale(session()->get(‘locale’));

How To Register Middleware in Laravel application

After creation of middleware into laravel application, I need to register middleware with kernel as like below, I will make one entry of Localization into app/Http/Kernel.php file.

/** * The application’s route middleware groups. * * @var array */ protected $middlewareGroups = [ ‘web’ => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, ….. …. \App\Http\Middleware\Localization::class, ],

* The application’s route middleware groups.

protected $middlewareGroups = [

\App\Http\Middleware\EncryptCookies::class,

\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

\Illuminate\Session\Middleware\StartSession::class,

\App\Http\Middleware\Localization::class,

How To Display Locale Message In Laravel

We will create a new /resources/views/hello.blade.php file to test message in different language,

<div class=”title m-b-md”> <p>{{ trans(‘constant.welcome’)}}</p> </div>

<div class=”title m-b-md”>

<p>{{ trans(‘constant.welcome’)}}</p>

Here, I have used the trans() helper string function which will take one argument combination of filename and keyname, the params as like below – filename.array_key_name

I hope, You enjoyed Multilingual functionality integration using Laravel Localization.


Originally published at www.phpflow.com on April 13, 2019.