Skip to content

Internationalization (i18n) ​

Language files ​

lang/
├── en/
│   ├── auth.php
│   ├── validation.php
│   └── flights.php
└── es/
    ├── auth.php
    └── flights.php
php
// lang/en/flights.php
return [
    'welcome'       => 'Welcome, :name',
    'found'         => ':count flight found|:count flights found',
    'no_results'    => 'No flights available for this route.',
    'book'          => 'Book now',
];

Using translations ​

php
// Simple
__('flights.welcome', ['name' => 'Raúl']);
// → "Welcome, Raúl"

trans('flights.no_results');
// → "No flights available for this route."

// Pluralization
trans_choice('flights.found', 1);   // → "1 flight found"
trans_choice('flights.found', 5);   // → "5 flights found"

In Twig templates ​

twig
{{ __('flights.welcome', {name: user.name}) }}
{{ trans_choice('flights.found', count) }}

{# Shorthand #}
{{ 'flights.book' | trans }}

Setting the locale ​

php
// In a middleware or at boot
app()->setLocale('es');

// Current locale
app()->getLocale();   // 'es'

Configuration ​

ini
APP_LOCALE=en
APP_FALLBACK_LOCALE=en
php
// config/app.php
return [
    'locale'          => env('APP_LOCALE', 'en'),
    'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
];

Validation messages ​

Validation messages are English by default and follow the current locale. The framework ships lang/en/validation.php and lang/es/validation.php; switch to Spanish with APP_LOCALE=es or Translator::setLocale('es').

To change a message, or to add a language, create lang/{locale}/validation.php in your app. You only need the keys you want to override: the rest come from the framework's file.

php
// lang/en/validation.php
return [
    'required' => 'Please fill in :attribute.',
    'min'      => ':attribute needs at least :min.',
];

Available placeholders: :attribute on every rule, plus :min, :max, :size, :digits, :values, :other, :value, :date and :format on the rules that use them.

The lookup order is: the message you pass to validate() (or messages() in a FormRequest), then lang/{locale}/validation.php, then lang/{fallback}/validation.php, then the built-in English text.

Released under the MIT License.