Laravel Localization for English, Bangla & More

Laravel Localization for English, Bangla & More

Laravel uses a clean file-based system where each language has its own folder. These folders store translation strings that can be reused across views, controllers, and APIs.

This approach keeps your code organized and your translations easy to manage.

 Supported Languages in This Setup

This implementation includes:

  • English (en) – Default language
  • Bangla (bn) – For Bengali users
  • Arabic (ar) – With RTL support
  • French (fr) – For European and global users

Each language has its own translation file, ensuring clarity and scalability.

 Create Language Directories

resources/lang/
├── en
├── bn
├── ar
└── fr

resources/lang/en/messages.php

<?php

return [
    'welcome' => 'Welcome',
    'login' => 'Login',
    'logout' => 'Logout',
    'dashboard' => 'Dashboard',
];

resources/lang/bn/messages.php

<?php

return [
    'welcome' => 'স্বাগতম',
    'login' => 'লগইন',
    'logout' => 'লগআউট',
    'dashboard' => 'ড্যাশবোর্ড',
];

resources/lang/ar/messages.php

<?php

return [
    'welcome' => 'مرحباً',
    'login' => 'تسجيل الدخول',
    'logout' => 'تسجيل الخروج',
    'dashboard' => 'لوحة التحكم',
];

resources/lang/fr/messages.php

<?php

return [
    'welcome' => 'Bienvenue',
    'login' => 'Connexion',
    'logout' => 'Déconnexion',
    'dashboard' => 'Tableau de bord',
];

config/app.php

'locale' => 'en',
'fallback_locale' => 'en',

routes/web.php

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;

Route::get('lang/{locale}', function ($locale) {

    if (! in_array($locale, ['en', 'bn', 'ar', 'fr'])) {
        abort(400);
    }

    Session::put('locale', $locale);
    App::setLocale($locale);

    return redirect()->back();
});

app/Http/Middleware/SetLocale.php

<?php

namespace App\Http\Middleware;

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

class SetLocale
{
    public function handle($request, Closure $next)
    {
        if (Session::has('locale')) {
            App::setLocale(Session::get('locale'));
        }

        return $next($request);
    }
}

app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\SetLocale::class,
    ],
];

Use Translation in Blade

<h1>{{ __('messages.welcome') }}</h1>
<a href="#">{{ __('messages.login') }}</a>

Language Switcher (Blade UI)

<ul>
    <li><a href="{{ url('lang/en') }}">English</a></li>
    <li><a href="{{ url('lang/bn') }}">বাংলা</a></li>
    <li><a href="{{ url('lang/ar') }}">العربية</a></li>
    <li><a href="{{ url('lang/fr') }}">Français</a></li>
</ul>

Blade Layout Example

<html lang="{{ app()->getLocale() }}"
      dir="{{ app()->getLocale() == 'ar' ? 'rtl' : 'ltr' }}">

Use Localization in Controller

return response()->json([
    'message' => __('messages.welcome')
]);