Home - Scripts - Website Development
Laravel’s multi-language system allows for seamless translation, enhancing SEO, user experience, and engagement for businesses targeting diverse international audiences through custom websites.
Key Points
Creating a multi-language website is crucial for businesses aiming to reach diverse audiences, improve user engagement, and boost international SEO. Laravel offers robust localization features that make this easy to implement.
In this guide, we’ll walk through step-by-step how to build a multilingual Laravel app that supports dynamic language switching and displays translations seamlessly. This is highly valuable for Laravel developers or businesses looking to expand globally.
Laravel organizes translations inside the resources/lang folder. Each supported language has its subfolder containing PHP files for translation keys.
Your folder structure should look like this:
resources/
└── lang/
├── en/ # English Translations
│ ├── messages.php # General UI messages
│
├── hi/ # Hindi Translations
│ ├── messages.php
│
├── fr/ # French Translations
│ ├── messages.php
│
└── (more locales as needed...)
Example resources/lang/en/messages.php:
These files allow Laravel to fetch the correct translations based on the active locale. This is foundational for any custom website development targeting multiple languages.
Define routes for changing the language and for testing language display. In routes/web.php:
Visiting /lang/fr would switch the website to French.
Create a middleware to read the chosen locale from the session and tell Laravel to use that language globally.
Generate a middleware:
php artisan make:middleware SetLocale
Then edit app/Http/Middleware/SetLocale.php:
This is a crucial step for ensuring that every request uses the correct language.
Register the middleware so it runs automatically for web requests.
Open app/Http/Kernel.php and add it to the web middleware group:
This ensures Laravel uses the selected language across all routes and pages.
Create a controller to handle language switching logic.
Generate the controller:
php artisan make:controller LanguageController
Edit app/Http/Controllers/LanguageController.php:
switchLang: Saves the chosen language to the session.
language: Loads the translation and sends it to a view.
This pattern is common in website development services where dynamic language switching is required.
Create a Blade template to display the language selector and translated content.
Example Blade View: resources/views/language.blade.php
This allows users to click a link to change the site’s language. Once the language changes, all __('...') calls automatically pull from the correct language file.
Laravel stores all translations under resources/lang/.
Each language has its folder like en, hi, fr.
Inside each folder, you create PHP files like messages.php containing translation arrays.
Use the __() helper or @lang() directive to display translated text.
Example:
{{ __('messages.welcome') }}
Visiting /lang/{locale} stores the selected language in the session.
E.g., /lang/hi saves hi to the session.
Once the locale is set, any __('...') call loads translations from the chosen language folder.
Example: __('messages.cart') returns:
English: Cart is empty
Hindi: कार्ट खाली है
French: Le panier est vide
This makes it easy to localize any part of your website.
By following these steps, you’ve created a fully functional multilingual system in Laravel. Whether you’re building custom web applications or working with clients who need international support, mastering Laravel’s localization tools is crucial. From routing and middleware to translation files and Blade views, Laravel makes it simple to build scalable, multilingual applications that elevate your brand globally.
©2025Digittrix Infotech Private Limited , All rights reserved.