Building a Multilingual Website with Laravel Localization

Last modified on December 17, 2019 4 min read

In this tutorial, I’m going to show how to build a multilingual website with Laravel. There are many ways of using localization in Laravel. I’ll write two methods here. The output of the project will look like this:

Building a Multilingual Website with Laravel Localization
Building a Multilingual Website with Laravel Localization

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Set Default Language
  3. Translate Using Short Keys
  4. Translation Strings As Keys
  5. Create Controller and Middleware
  6. Make Blade (View) Files
  7. Register Web Routes

Step 1 : Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

Step 2 : Set Default Language

After installation, open this file config/app.php to set the default language.

'locale' => 'en',

By default local is set to en (English). If you check resources/lang folder, you will find a folder named en.

Step 3 : Translate Using Short Keys

We are going to add two more languages. These are Bengali and Hindi. Go to resources/lang directory and create two folders named bn and in. We are going to add a short key named title. The title will be translated into 3 languages.

Let’s create a file called lang.php in all 3 folders and add title key like this:

For English:

return [
    'title' => 'Welcome to MyNotePaper'

For Bengali:

return [
    'title' => 'MyNotePaper এ আপনাকে স্বাগতম'

For Hindi:

return [
    'title' => 'MyNotePaper में आपका स्वागत है'

We’ve added a title key. We will implement in blade file after a few steps.

Step 4 : Translation Strings As Keys

In this step, we are going to add some strings as keys for translation. Go to resources/lang and create two JSON file including two strings like this:

For Bengali:

    "Hello!" : "হ্যালো!",
    "Stay with us and keep learning" : "আমাদের সাথে থাকুন এবং শিখতে থাকুন"

For Hindi:

    "Hello!" : "नमस्ते!",
    "Stay with us and keep learning" : "हमारे साथ रहें और सीखते रहें"

We’ve added some strings as keys. Like short key, we will implement in blade file after a few steps.

Step 5 : Create Controller and Middleware

We need a controller to change language. Create the controller using this command:

php artisan make:controller LocalizationController

Now open the controller from app\Http\Controllers and paste the below code. I’ve written a function to store selected language in session.


namespace App\Http\Controllers;

use App;
use Illuminate\Http\Request;

class LocalizationController extends Controller
    public function index($locale)
        // store the locale in session so that the middleware can register it
        session()->put('locale', $locale);
        return redirect()->back();

Now we need a Middleware to register the local. Create the middleware using this command:

php artisan make:middleware Localization

Open the Localization Middleware from app\Http\Middleware and replace the code with this:


namespace App\Http\Middleware;

use App;
use Closure;

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')) {
        return $next($request);

We need to register the Localization Middleware. Open app/Http/Kernel.php file add \App\Http\Middleware\Localization::class, in the $middlewareGroups array’s web key.

protected $middlewareGroups = [
    'web' => [
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \App\Http\Middleware\Localization::class, // Localization Middleware

    'api' => [

We’ve created and set-up controller & middleware.

Step 6 : Make Blade (View) Files

We are going to create two view files. Create the two files named layout.blade.php and home.blade.php.

Open layout.blade.php and paste the below code. Here’s I’ve added a language selection dropdown menu using Boostrap 4.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('', 'Laravel') }}</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="">
<div id="app">
    <div style="background-color: #f8f9fa;">
        <nav class="navbar navbar-expand-lg navbar-light bg-light container">
            <a href="" target="_blank"><img src=""
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02"
                    aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>

            <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
                <!-- Right Side Of Navbar -->
                <ul class="navbar-nav ml-auto">
                    <!-- Authentication Links -->
                    @php $locale = session()->get('locale'); @endphp
                    <li class="nav-item dropdown">
                        <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                <img src="{{asset('img/us.png')}}"> English
                                <img src="{{asset('img/bn.png')}}"> Bengali
                                <img src="{{asset('img/in.png')}}"> Hindi
                                <img src="{{asset('img/us.png')}}"> English
                            <span class="caret"></span>
                        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="lang/en"><img src="{{asset('img/us.png')}}"> English</a>
                            <a class="dropdown-item" href="lang/bn"><img src="{{asset('img/bn.png')}}"> Bengali</a>
                            <a class="dropdown-item" href="lang/in"><img src="{{asset('img/in.png')}}"> Hindi</a>

    <main class="py-4">

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src=""></script>
<script src=""></script>
<script src=""></script>

Now open home.blade.php file. In this file, we will display our short key and strings as keys translation.

We can retrieve translation using trans()@lang and __() methods. Let’s retrieve them in this view:

    <div class="container">
        <h1 class="display-4 text-center" style="font-size: 2.5rem">@lang("Hello!")</h1>
        <h3 class="display-4 text-center" style="font-size: 1.5rem">{{ trans('lang.title') }}</h3>
        <h4 class="display-4 text-center" style="font-size: 1.5rem">{{ __("Stay with us and keep learning")}}</h4>

Step 7 : Register Web Routes

We are at the end. This is the last step. We have to define a route to display home view and another to change the language. Let’s do that:

Route::get('/', function () {
    return view('home');
Route::get('lang/{locale}', '[email protected]');

Now run the project and see the output.

The tutorial is over. You can download this project from GitHub. Thank you. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.