Laravel 5: The Translator's File Loader

Laravel 5: The Translator's File Loader

Laravel 5

The FileLoader is the class that is responsible for loading the translation files from the file system. The language files are loaded from the /resources/lang/ directory by default. The lang/ directory contains the en (English from the ISO 639-1 standard) sub-directory by default. Each sub-directory within the lang/ directory corresponds to a difference locale; the locale should share the same name as the sub-directory.

PHP Based Language Files

Each locale directory may contain various PHP files returning arrays. These arrays contain a key/value pair, indicating the key of the translation and the value of the language translation. For example, the following is the contents of the /resources/lang/en/passwords.php file:


return [

    // ...

    'password' =>
        'Passwords must be at least six characters and match the
    'user' => "We can't find a user with that e-mail address.",
    'token' => 'This password reset token is invalid.',
    'sent' => 'We have e-mailed your password reset link!',
    'reset' => 'Your password has been reset!',


The individual PHP files containing the translation lines are referred to as "groups". For a fresh Laravel installation, the following groups exist:

Group Name Path
Authentication resources/lang/en/auth.php
Pagination resources/lang/en/pagination.php
Passwords resources/lang/en/passwords.php
Validation resources/lang/en/validation.php

JSON Based Language Files

It is also possible to create language files using a JSON object. When creating language files with JSON, we do not create a sub-directory for each locale; rather, we create a JSON file with the locale as it's name according to the format <LOCALE_NAME>.json. If we wanted to create a JSON based locale file for Spanish, we could create a es.json language file within the root language file directory (/resources/lang).

When creating these JSON files, each language key/value is stored as a property within the JSON object; translation keys cannot be nested. Because we cannot nest the key names in the JSON language files, if your application makes use of both PHP based language files and JSON language files, you may occasionally run into issues where language keys are not resolved from the JSON files when using dot notation.

However, we can emulate the dot notation syntax that is used to retrieve nested translation strings from PHP-based files by providing the dot notation as part of the JSON key (notice the validation.accepted key):

    "validation.accepted": "The :attribute must be accepted.",
    "active_url":          "The :attribute is not a valid URL."

Start the Discussion

Leave a comment

Subscribe to our newsletter