Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create multi language website in Laravel

How to create multi language website in Laravel 5 / 6 / 7 with example?

Aman Jain, May 10, 2022May 10, 2022

Laravel provides its own localization library to handle the multi language feature in application. Laravel stores each languages translations in resource/lang . If you are going to build a full featured multi language website then you are at right place to end your search. In this article i will show you to translate the website in different languages using the Laravel localization library. I will pick 3 languages to change English, French and Spanish. You will be able to switch the language by selecting three of one.

So let’s begin the tutorial step by step

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2: Create a Languges files

Next step is to create the each language files in resources/lang folder. In this example we are taking example of three languages so we need to create 3 sub directory in resources/lang folder. below is the example

/resources
    /lang
        /en
            messages.php
        /fr
            messages.php
        /es
            messages.php

Therefore, as above creating 3 files .

English

<?php
return [
   "title"=>"Hello Guest, This is readerstacks. how are you today"
];

Spanish

<?php
return [
   "title"=>"Hola invitado, estas son las pilas de lectores. cómo estás hoy"
];

French

<?php
return [
   "title"=>"Bonjour Invité, Ceci est readerstacks. Comment vas-tu aujourd'hui"
];

Step 3: Create routes

Create route to show the html and change language route

<?php


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
$this->group([   'middleware' => 'Language'], function () {
	Route::get('/home',"\App\Http\Controllers\HomeController@index");
	Route::get('/change-language/{lang}',"\App\Http\Controllers\HomeController@changeLang");
});

Here, we created two routes one for view and other for change language and we used a Middleware Language which is used to set the selected language on each request. so let’s create the middleware

Step 4 : Create a language middleware

We are creating middleware to set the selected language on every request . so create the middleware by running the artisan command

php artisan make:middleware Language

and the change the content as below

<?php

namespace App\Http\Middleware;

use Closure;
use App;
class Language
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(session()->has("lang_code")){
            App::setLocale(session()->get("lang_code"));
        }
        return $next($request);
    }
}

Register our new middleware in app\Http\kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    .....
 protected $routeMiddleware = [
  ....
    "Language"=>\App\Http\Middleware\Language::class
  ];
}

Step 5 : Create a controller

Create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller HomeController

And put the below content

<?php

 
namespace App\Http\Controllers;

use App\Http\Controllers\Controller; 
use App;
class HomeController extends Controller
{
   function index(){
    
      return view("home");
   }
   
   function changeLang($langcode){
    
      App::setLocale($langcode);
      session()->put("lang_code",$langcode);
      return redirect()->back();
  }  
}

here, we created two methods one for render the view and second for change the language. We also set session so we can use the same language in other request. To handle the other requests we have already created middleware. App::setLocale($langcode); is used to set the language file for current request but we want it in our all request so i store the language code in session as well.

Step 5 : Create view

Now, create a view where we can show the translation and can change the language using select box

  <body>
     <h1>Laravel multi language web with example -readerstacks.com </h1>
    <div>
        Langauge : <select onchange="changeLanguage(this.value)" >
            <option {{session()->has('lang_code')?(session()->get('lang_code')=='en'?'selected':''):''}} value="en">English</option>
            <option {{session()->has('lang_code')?(session()->get('lang_code')=='fr'?'selected':''):''}} value="fr">French</option>
            <option {{session()->has('lang_code')?(session()->get('lang_code')=='es'?'selected':''):''}} value="es">Spanish</option>
        </select>
    </div>

    <h2>{{__("messages.title")}}</h2>

    </body>
   <script>
    
    function changeLanguage(lang){
        window.location='{{url("change-language")}}/'+lang;
    }
    </script>

here, we have created a select box to choose the language and we have used __() function to render the respective selected language .

Screenshot of implementation:

Screenshot 2021 11 25 at 4.23.55 PM 1
Screenshot 2021 11 25 at 4.25.18 PM
Laravel multi language example

Related

Php Laravel laravelMulti Language

Post navigation

Previous post
Next post

Related Posts

Laravel How to Run a Background Queue Job or Request in Laravel

How to Run a Background Queue Job or Request in Laravel ?

May 17, 2022August 20, 2022

In any website running a queue can help to increase the runtime performance of any application by running a background queue job or request in laravel. Queues are much helpful while our application performs bulk actions like mailing to thousand of users or running a heavy tasks. In laravel we…

Read More
Laravel Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want…

Read More
Laravel 9 Collection Count and Check Empty Eloquent Collection in Laravel

Collection Count and Check Empty Eloquent Collection in Laravel

October 26, 2022March 16, 2024

In this article we will learn collection count and detect or check empty eloquent collection in laravel. Laravel collection is used to simplify the operations of array. Illuminate\Support\Collection library is used to handle the collection and arrays in laravel. Laravel heavily used collections in eloquent query builder to result the…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version