What is localization?
Laravel’s localization system lets you store translation strings for each language your application supports and retrieve them through a single helper function. When you call__('messages.welcome'), Laravel looks up the string in the active locale’s file and returns the translated version.
There are two ways to organize translation strings:
Publishing language files
New Laravel apps don’t include thelang directory by default. Run this command to create it and publish Laravel’s built-in translations:
Configuring the locale
Default locale
Set the application’s default language inconfig/app.php, typically via .env:
Changing locale at runtime
Use theApp facade to switch locale for the current request — useful in middleware:
Checking the current locale
Defining translation strings
PHP file format (short keys)
Create one file per feature group inside a locale directory:JSON file format (translation strings as keys)
Use the English text as the key. Each other language gets a JSON file at thelang/ root:
Retrieving translation strings
The __() helper
In Blade templates
Placeholders
Define dynamic parts with a:name prefix:
Pluralization
Different languages handle pluralization differently. Laravel’strans_choice() function selects the right form based on a count.
Simple singular / plural
Separate the two forms with|:
Range-based forms
Use{n} for exact values and [min,max] for ranges (* means infinity):
Placeholders in plural strings
:count is always available as the passed count:
Setting up locale switching
Here’s a complete pattern for letting users switch language and persist it in the session:1
Create a SetLocale middleware
2
Implement the middleware
3
Register the middleware
In
bootstrap/app.php:4
Add a locale-switching route
5
Add switcher buttons in Blade
Overriding package translations
When a third-party package ships its own language files, override individual strings by placing a file inlang/vendor/{package}/{locale}/: