Skip to content

All topics  /  Laravel

How to Create Multilingual Website in Laravel 8

Laravel ·

Teams applying “How to Create Multilingual Website in Laravel 8” in a production project can use practical i form to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi Dev,

In this article I will share with you how to create a multilingual website in laravel 8 application, I will show an example of laravel 8 multiple languages.

Here, I will give you a full example of how to laravel 8 multilanguage with language dropdown or laravel 8 multiple language websites as below so follow my all steps.

So, let's follow few step to create multi language support in your laravel application.

Step 1 : Install Laravel


First step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Database Configuration

In second step, we will make database Configuration for example database name, username, password etc. So lets open .env file and fill all deatils like as bellow:

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_8
DB_USERNAME=root
DB_PASSWORD=

Step 3 : Create Lang File

In this third step i will create three files in following folder for english,french,russian in lang folder.

resources/lang/en/messages.php

<?php 
return [
    'title' => 'This is English Language Title.'
];

resources/lang/en/messages.php

<?php 
return [
    'title' => 'Ceci est le titre fr langue anglaise.'
];

resources/lang/ru/messages.php

<?php
return [
    'title' => '??? ???????? ?? ?????????? ?????.'
];

Step 4 : Create Route

In this step i will create two route one for display dashboard and second one is change language.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LanguageController;
Route::get('/', [LanguageController::class, 'index'])->name('home');
Route::get('lan-change', [LanguageController::class, 'langChange'])->name('lan.change');

Step 5 : Create LanguageController

In this step,we will create a controller. Use the below command for generate controller

php artisan make:controller LanguageController

app/Http/Controllers/LanguageController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class LanguageController extends Controller
{
    /**
     * Write Your Code.
     *
     * @return string
    */
    public function index()
    {
        return view('lang');
    }
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function langChange(Request $request)
    {
        App::setLocale($request->lang);
        session()->put('locale',$request->lang);
        return redirect()->back();
    }
}

Step 6 : Create a blade view

In this step, we will create a blade file name lang.blade.php bellow following path.

resources/views/lang.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Create Multilingual Website in Laravel 8?</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body class="bg-dark">
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-8 offset-2">
                <div class="card">
                    <div class="card-header">
                        <h5>How to Create Multilingual Website in Laravel 8 - <span class="text-primary">NiceSnippets.com</span></h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3 col-md-offset-6 text-right">
                                <strong>Select Language: </strong>
                            </div>
                            <div class="col-md-9">
                                <select class="form-control changeLang">
                                    <option value="en" {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English</option>
                                    <option value="fr" {{ session()->get('locale') == 'fr' ? 'selected' : '' }}>French</option>
                                    <option value="ru" {{ session()->get('locale') == 'ru' ? 'selected' : '' }}>Russian</option>
                                </select>
                            </div>
                        </div>
                        <h2 class="mt-4 text-center">{{ __('messages.title') }}</h2>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    var url = "{{ route('lan.change') }}";

    
    $('.changeLang').change(function(){
        window.location.href = url + "?lang=" + $(this).val();
    });
</script>
</html>

Step 7 : Create Middleware

In this step i will create one middleware for dynamically change language let's created below artisan command.

php artisan make:middleware LanguageManager

app/Http/Middleware/LanguageManager.php

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

So,now we need to register in kernel.php file so let's define.

<?php

  
namespace App\Http;

  
use Illuminate\Foundation\Http\Kernel as HttpKernel;

  
class Kernel extends HttpKernel
{
    /**
     * 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,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\LanguageManager::class,
        ],

  
        'api' => [
            'throttle:60,1',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

Now, we will use the php artisan serve command.

php artisan serve

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/

Output:

It will help you...