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.
This implementation includes:
Each language has its own translation file, ensuring clarity and scalability.
resources/lang/ ├── en ├── bn ├── ar └── fr
<?php
return [
'welcome' => 'Welcome',
'login' => 'Login',
'logout' => 'Logout',
'dashboard' => 'Dashboard',
];
<?php
return [
'welcome' => 'স্বাগতম',
'login' => 'লগইন',
'logout' => 'লগআউট',
'dashboard' => 'ড্যাশবোর্ড',
];
<?php
return [
'welcome' => 'مرحباً',
'login' => 'تسجيل الدخول',
'logout' => 'تسجيل الخروج',
'dashboard' => 'لوحة التحكم',
];
<?php
return [
'welcome' => 'Bienvenue',
'login' => 'Connexion',
'logout' => 'Déconnexion',
'dashboard' => 'Tableau de bord',
];
'locale' => 'en', 'fallback_locale' => 'en',
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();
});
<?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);
}
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SetLocale::class,
],
];
<h1>{{ __('messages.welcome') }}</h1>
<a href="#">{{ __('messages.login') }}</a>
<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>
<html lang="{{ app()->getLocale() }}"
dir="{{ app()->getLocale() == 'ar' ? 'rtl' : 'ltr' }}">
return response()->json([
'message' => __('messages.welcome')
]);