Adding multi-language functionality in a website developed in laravel.
Tags: laravel, multilanguage, localization, middleware
Published: 2019-01-05 16:58:38

---
title: Adding multi-language functionality in a website developed in laravel.
published: true
cover_image : https://thepracticaldev.s3.amazonaws.com/i/3glyrh45vwj8chjhrpf3.jpg
tags: laravel, multilanguage, localization, middleware
---

Hello Everyone,
Today I will share with you how do add multi language to a website that was developed in laravel, my version is **5.6** but it will work on **5.7** as well.

## So...here we go!


First of all, I assume that you already have a working version of laravel. Navigate to that particular folder and open your terminal or command prompt.

Run the following command in order to create the required middleware.

```composer
$ php artisan make:middleware Localization

```
Then open it. The file should be in "***project_name/app/Http/Middleware/Localization.php***"

Update the code that is present with the one below : 

```php
public function handle($request, Closure $next)
{
   if(\Session::has('locale'))
   {
       \App::setlocale(\Session::get('locale'));
   }
   return $next($request);
}

```

####Lets say you want your website to be in ***English*** and ***French***.

1. Go to "***project_name/resources/lang/en/***"

2. Create a file name "***messages.php***"

3. Create folder "***fr***" in "***project_name/resources/lang/***"

4. Create a file name "***messages.php***" in "***project_name/resources/lang/fr/***"

Open "***messages.php***" in both folder and insert the following codes.

####"***messages.php***" in '**en**' folder.

```php

return [
    'welcome'       => 'Welcome to our application'
];
```

####"***messages.php***" in '**fr**' folder.

```php

return [
    'welcome'       => 'Bienvenue sur notre application'
];
```

##Most important...

Add the below codes in "***project_name/app/Http/Kernel.php***" in "*protected $middlewareGroups* section".

```php
\App\Http\Middleware\Localization::class,
```
So your codes should be like this :

```php
protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\Localization::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];
```

Now all this code in the route, "***project_name/routes/web.php***" on top of other defined routes.

```php
Route::get('locale/{locale}', function ($locale){
    Session::put('locale', $locale);
    return redirect()->back();
});
```

So in order to know the language that is being use, you can simply use :

```php

<html lang="{{ app()->getLocale() }}">
```

## Changing language en/fr


To change the language, just create an anchor tag in html like the following : 

```php

<li><a href="{{ url('locale/en') }}" ><i class="fa fa-language"></i> EN</a></li>
                            
<li><a href="{{ url('locale/fr') }}" ><i class="fa fa-language"></i> FR</a></li>

```

To call the desired text :

```php

<h1>{{ __('messages.welcome') }}</h1>

```