Laravel Localization: Managing Language Files, Setting Locale, and Using Translation Strings
This guide explains Laravel's localization features, covering folder structure for language files, configuring default and fallback locales, defining translation strings with short keys or full text, retrieving them via helper functions, handling placeholders, and managing pluralization with trans_choice.
Introduction
Laravel provides a simple folder‑based approach for localization, allowing applications to support multiple languages. Language files are stored in the resources/lang directory, with each language having its own sub‑folder (e.g., en , es ).
/resources
/lang
/en
messages.php
/es
messages.phpEach language file returns an associative array of key‑value pairs, for example:
'Welcome to our application'
];
?>Locale Settings
The default locale is defined in config/app.php . You can change the current locale at runtime using the App facade's setLocale method:
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});A fallback locale can also be configured in config/app.php :
'fallback_locale' => 'en',Determine Current Locale
You can retrieve the current locale with App::getLocale() and check it with App::isLocale('en') :
$locale = App::getLocale();
if (App::isLocale('en')) {
//
}Defining Translation Strings
Using Short Keys
Translation files under resources/lang contain arrays of short keys:
/resources
/lang
/en
messages.php
/es
messages.php 'Welcome to our application'
];
?>Using Translation Strings as Keys
For large projects you can store full text keys in JSON files, e.g., resources/lang/es.json :
{
"I love programming.": "Me encanta programar."
}Retrieving Translation Strings
The __ helper fetches translations. It accepts the file and key as the first argument:
echo __('messages.welcome');
echo __('I love programming.');In Blade templates you can use {{ __() }} or the @lang directive:
{{ __('messages.welcome') }}
@lang('messages.welcome')If a translation key does not exist, __ simply returns the key name.
Parameter Replacement in Translation Strings
Placeholders prefixed with : can be defined in translation strings and replaced by passing an associative array to __ :
'welcome' => 'Welcome, :name', echo __('messages.welcome', ['name' => 'dayle']);Upper‑case placeholders preserve case in the output.
Pluralization
Plural forms are defined using the pipe | separator or more complex range rules:
'apples' => 'There is one apple|There are many apples', 'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',Use trans_choice to retrieve the appropriate string based on a quantity, optionally replacing placeholders:
echo trans_choice('messages.apples', 10); 'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
echo trans_choice('time.minutes_ago', 5, ['value' => 5]);Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.