How to Create Multilingual Website in Laravel 8

How to Create Multilingual Website in Laravel 8

Hello friends, Today we will show How to create multilingual website in Laravel 8. You will learn  laravel 8 multilingual website example. we will provide simple example of laravel 8 multi language with language dropdown.

In this example, We will give you a full example of how to create laravel 8 multilanguage with language dropdown or laravel 8 multiple language websites as follow so follow my all steps.

Here, we will help you how to add multiple language in laravel 8. we will use laravel trans to create Multilingual language website. So, let’s follow few step to create multi language support in laravel 8 application.

Read also : Laravel 8 Automatic Database Encryption Decryption

Laravel 8 Create Multilingual Website Example

Step 1 : Install Laravel

Now, we required to get fresh laravel 8 version application. So let’s open terminal and run following command to install fresh laravel project.

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

Step 2 : Database Configuration

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.

Read Also :  How Generate PDF From HTML In Laravel dompdf

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

<?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

Now, we will create a controller. Use the following 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

Now, 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">web-tuts.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 following 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 required 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 following command to quick run.

http://localhost:8000/

I hope you understand of multilingual website in Laravel 8 and it can help you…

Read Also :  Drag and Drop File Upload using Dropzone in Laravel