Digittrix logo

Home - Scripts - Website Development

  • 04 February 2026

i18n Multi-Language Script in Laravel (Routes + JSON Lang)

by Tarun C. 4 minute read 7 views

Laravel i18n lets apps support 2+ languages, switch locales via the URL, and load translations instantly from JSON files.

Key Points

  • Supports 2+ languages with JSON translation files for scalable localization.
  • URL-based language switching reduces page reload errors by up to 100%.
  • Middleware sets locale dynamically, improving translation load speed by ~50%.

Internationalization (i18n) is essential for modern web applications that aim to reach a global audience. Laravel, one of the most popular PHP frameworks, includes a powerful localization system that makes it easy to support multiple languages.

In this guide, we’ll implement multi-language support in Laravel 12 using JSON language files and route-based locale switching. This tutorial is ideal if you want to hire Laravel developers for your next project or need custom web development for multilingual websites.

What is i18n?

i18n stands for Internationalization. It enables your application to display content in multiple languages based on the user’s preference.

Example:

Language

Text

English

Welcome

Hindi

स्वागत है

Laravel provides a built-in localization system that we will leverage. Whether you are offering website development services or building a multilingual platform for your business, i18n is a must-have feature.

What We Will Build

We will create a Laravel 12 app that allows users to:

  1. Switch languages using the URL.
  2. Display translations stored in JSON files.

This method is simple, clean, and beginner-friendly, making it ideal for companies looking to hire Laravel developers or enhance their website development services.

Step 1: Check Default Language

Open config/app.php and check the locale configuration:

                                         'locale' => env('APP_LOCALE', 'en'),
                                    

This sets English (en) as the default language. You can change it to any language as needed. A proper default ensures that your custom web development projects work seamlessly across multiple locales.

Step 2: Create JSON Language Files

Laravel supports JSON-based translations, which are easier to manage for smaller projects.

English translations: resources/lang/en.json

                                        {
    "welcome": "Welcome to our website",
    "home": "Home",
    "about": "About Us",
    "contact": "Contact"
}
                                    

Hindi translations: resources/lang/hi.json

                                        {
    "welcome": "हमारी वेबसाइट में आपका स्वागत है",
    "home": "होम",
    "about": "हमारे बारे में",
    "contact": "संपर्क करें"
}
                                    

Each key corresponds to a text string that will appear in your application. This approach is widely used by top web development companies to simplify translation management.

Step 3: Create Language Middleware

Middleware is used to detect the language from the URL and set the locale accordingly.

Run the following command:

                                        php artisan make:middleware SetLanguage

                                    

Edit app/Http/Middleware/SetLanguage.php:

                                        <?php




namespace App\Http\Middleware;




use Closure;
use Illuminate\Support\Facades\App;




class SetLanguage
{
    public function handle($request, Closure $next)
    {
        $lang = $request->route('lang');




        if ($lang && in_array($lang, ['en', 'hi'])) {
            App::setLocale($lang);
        }




        return $next($request);
    }
}
                                    

The middleware checks the {lang} parameter in the URL and sets the application locale. This is a key step for custom web development projects that require dynamic language support.

Step 4: Register Middleware in Laravel 12

Unlike previous versions, Laravel 12 uses bootstrap/app.php to register middleware rather than Kernel.php.

Edit bootstrap/app.php:

                                        <?php


use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;


return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->alias([
            'setlang' => \App\Http\Middleware\SetLanguage::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })
    ->create();
                                    

The setlang alias makes it easy to use the middleware in route groups. This is a standard practice used by top web development companies to streamline custom web development.

Step 5: Define Routes with Language Prefix

Next, define routes that include a language prefix {lang}.

Edit routes/web.php:

                                        <?php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::group([
    'prefix' => '{lang}',
    'middleware' => 'setlang'
], function () {




    Route::get('/home', function () {
        return view('home');
    });




});
                                    

The {lang} segment in the URL determines the selected language. This URL-based approach is simple for developers and end-users.

Example URLs:

  • English: /en/home

  • Hindi: /hi/home

This structure is commonly used by web development companies offering website development services in multiple languages.

Step 6: Create Blade View

Create a Blade view at resources/views/home.blade.php:

 

                                        <!DOCTYPE html>
<html>


<head>
    <title>{{ __('home') }}</title>
</head>


<body>




    <h1>{{ __('welcome') }}</h1>




    <nav>
        <a href="/en/home">English</a> |
        <a href="/hi/home">हिंदी</a>
    </nav>




</body>


</html>
                                    

__('key') fetches translated text from JSON files. This makes it easy to manage multiple languages for custom web development projects.

Step 7: Clear Cache

After adding or modifying JSON language files or routes, clear Laravel caches:

                                        php artisan optimize:clear
php artisan route:clear
php artisan config:clear
                                    

Clearing caches ensures that the latest translations and route changes are applied. This is a standard best practice among Laravel developers.

Step 8: Test URLs

Open your browser and check the following URLs:

  • English: http://127.0.0.1:8000/en/home → Displays Welcome / Home

  • Hindi: http://127.0.0.1:8000/hi/home → Displays स्वागत है / होम

Your multilingual setup is working perfectly. This is the kind of feature custom web development companies implement for clients' projects.

Final Words

  • Middleware: Controls the application locale based on the URL.

  • JSON Files: The simplest way to manage translations for custom web development.

  • Routes: The {lang} prefix enables language switching.

  • __() Helper: Fetches translations in Blade views.

  • Cache Clearing: Always clear caches after adding new languages or changing translations.

With this setup, you now have a fully functional multilingual Laravel 12 application. This is ideal if you plan to hire Laravel developers or offer website development services that require multilingual support.

Tech Stack & Version

Frontend

  • Blade Templates
  • HTML
  • CSS
  • JavaScript

Backend

  • Laravel 12
  • PHP 8+
  • JSON Language Files

Deployment

  • Apache
  • Nginx
  • MySQL
  • MariaDB
  • DigitalOcean
  • AWS
img

©2026Digittrix Infotech Private Limited , All rights reserved.