How to Make Laravel 9 Website Multilingual

2 years ago admin Laravel

Today's tutorial will show you how to make your Laravel 9 website Multilingual, we will see how to create the database and add routes, and toggle the languages.


Create the database

First, let's create the database and add columns like below.

                                                    
                                                                                                                
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title_fr');
            $table->string('title_en');
            $table->string('slug');
            $table->text('body_fr');
            $table->text('body_en');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};


Add changeLang function

Inside your HomeController add the changeLang function.

                                                        
                                                                                                                        
public function changeLang($lang){
    session()->forget('lang');
    session()->put('lang', $lang);
    return redirect()->back();
}

Create the route

Inside the web.php file, we create the route to toggle the languages.

                                                        
                                                                                                                        
Route::get('change/lang/{lang}', [HomeController::class, 'changeLang'])
->name('change.lang');

Add the menu item

Inside the navbar, we add the menu item to change the language

                                                        
                                                                                                                        
<li class="nav-item dropdown">
   <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
      <i class="fas fa-language"></i> Languages
   </a>
   <ul class="dropdown-menu">
     <li><a class="dropdown-item" href="{{route('change.lang','en')}}">En</a></li>
     <li><a class="dropdown-item" href="{{route('change.lang','fr')}}">Fr</a></li>   
   </ul>
</li>

Display content based on the selected language

So when the user changes the language we can display the content based on the selected language like below.

                                                        
                                                                                                                        
@extends('layouts.main')

@section('title')
    Home
@endsection

@section('content')
    <div class="row my-5">
        <div class="col-md-12">
            <div class="card p-4">
                <div class="row">
                    @foreach ($posts as $post)
                        <div class="col-md-4 mb-2">
                            <div class="card h-100">
                                <div class="card-body">
                                    <div class="card-title fw-bold">
                                        @if(session()->get('lang') === 'fr')
                                            {{$post->title_fr}}
                                        @else
                                            {{$post->title_en}}
                                        @endif
                                    </div>
                                    <p class="card-text">
                                        @if(session()->get('lang') === 'fr')
                                            {{ Str::limit($post->body_fr, 100) }}
                                        @else
                                            {{ Str::limit($post->body_en, 100) }}
                                        @endif
                                    </p>
                                    <a href="{{route('posts.show', $post)}}" class="btn btn-primary">
                                        <i class="fas fa-eye"></i>
                                        @if(session()->get('lang') === 'fr')
                                            Voir
                                        @else
                                            View
                                        @endif
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
                <div class="card-footer bg-white">
                    <div class="d-flex justify-content-center">
                        {{$posts->links()}}
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

Related Tuorials

How to Prevent the Loop Incrementing Operator from Resetting Back to 1 in the Next Pagination Pages in Laravel

In this lesson, we will see how to prevent the loop incrementing operator from resetting back to 1 i...


How to Logout a User from the Other Devices in Laravel 11

In this lesson, we will see how to logout a user from the other devices in Laravel 11, sometimes you...


How to Logout a User from the Current Device in Laravel 11

In this lesson, we will see how to logout a user from the current device in Laravel 11, sometimes yo...


How to Import Multiple Classes from a Single Namespace in Laravel

In this lesson, we will see how to import multiple classes from a single namespace in Laravel by add...


Laravel 11 Livewire CRUD Application Tutorial Part 2

In the second part of this tutorial, we will display all the tasks on the home page and later we wil...


Laravel 11 Livewire CRUD Application Tutorial Part 1

This tutorial will show us how to create a Laravel 11 Livewire CRUD application. The user can c...


How to Conditionally Include a Blade Template in Laravel

In this lesson, we will see how to conditionally include a blade template in Laravel.Sometimes,...


How to Include a Blade Template Only if it Exists in Laravel

In this lesson, we will see how to include a blade template only if it exists in Laravel.Sometimes,&...


How to Pass a Variable to Include in Laravel

In this lesson, we will see how to pass a variable to include in Laravel. Sometimes, we want to pass...


How to the Get the Previous and Next Posts in Laravel

In this lesson, we will see how to get the previous and next posts in Laravel, sometimes when you ge...