Adding Localization and Translations
Localization lets a single Laravel application serve content in multiple languages, rather than maintaining separate codebases per language.
How Laravel Organizes Translations
Translation strings live in language files under the lang directory (or resources/lang in older versions), organized by locale.
lang/
├── en/
│ └── messages.php
├── es/
│ └── messages.php
Setting the Application Locale
App::setLocale('es');
The default locale is set in config/app.php, and can be changed per-request based on user preference, URL segment, or browser headers.
A Simple Translation File
// lang/en/messages.php
return [
'welcome' => 'Welcome back!',
];
With the structure in place, the next step is actually pulling these translated strings into views and application logic.
Showing Translated Text in Views and Code
Laravel reads a translation using the file name and the key, joined by a dot. The __() helper works in controllers, and Blade offers the same helper inside templates.
// controller or any PHP code
$message = __('messages.welcome');
{{-- Blade view --}}
<h1>{{ __('messages.welcome') }}</h1>
<p>@lang('messages.welcome')</p>
If a key is missing for the active locale, Laravel tries the fallback locale set by fallback_locale. If it is missing there too, the key itself is shown, which makes gaps easy to spot during testing.
On Laravel 11 and later, a fresh project has no lang folder. Run php artisan lang:publish to create it with the default validation and auth messages.
Placeholders and Plurals
Hard-coding names or counts into sentences breaks word order in other languages. Use placeholders that start with a colon and pass values at runtime.
// lang/en/messages.php
return [
'greeting' => 'Hello, :name!',
'cart' => '{0} Your cart is empty|{1} You have one item|[2,*] You have :count items',
];
// lang/es/messages.php
return [
'greeting' => '¡Hola, :name!',
'cart' => '{0} Tu carrito está vacío|{1} Tienes un artículo|[2,*] Tienes :count artículos',
];
// usage
__('messages.greeting', ['name' => $user->name]);
trans_choice('messages.cart', $count);
Writing :NAME in capitals makes Laravel insert the value in upper case, and :Name capitalises the first letter.
JSON Translation Files
For large apps with many short strings, JSON files can be easier. The English sentence itself becomes the key, so templates stay readable.
// lang/hi.json
{
"Save changes": "बदलाव सहेजें",
"Log out": "लॉग आउट"
}
{{ __('Save changes') }}
Switching Locale for Each Request
A small middleware can read the language from the URL, a session value or the user's profile, then set it before controllers run.
public function handle(Request $request, Closure $next)
{
$supported = ['en', 'es', 'hi'];
$locale = $request->segment(1);
if (in_array($locale, $supported, true)) {
App::setLocale($locale);
}
return $next($request);
}
Always check against a list of supported locales, so a random URL segment cannot select a language folder that does not exist.
Register the middleware on the web route group, and store the chosen language in the session or on the user's profile so it sticks across pages. When the locale comes from the browser instead, $request->getPreferredLanguage($supported) picks the best match from the Accept-Language header.
Habits That Keep Translations Manageable
- Group keys by feature, such as
checkout.phporprofile.php, instead of one huge file. - Never build sentences by joining translated fragments; translate the whole sentence with placeholders.
- Format dates and money for the locale, for example with Carbon's
translatedFormat()and PHP'sNumberFormatter. - Set the HTML
langattribute fromapp()->getLocale()so browsers and screen readers know the language. - Check for missing keys before each release by switching through every locale.
- Translate validation messages too; the published
validation.phpfile in each locale folder controls them.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.