Laravel 5 Translation Using the Double Underscore (__) Helper Function

Laravel 5 Translation Using the Double Underscore (__) Helper Function

Laravel 5

The __ helper function can be used to retrieve lines of text from language files. You can retrieve lines of text from either key-based translation files (represented as PHP arrays) or from literal, string-based translation files (represented as a JSON object).

Replacements are passed as a key/value pair. The replacement keys will be matched to any placeholders within the translation string. Placeholders begin with a single colon (:) followed by a name and a space. For example, in the validation.php language file, the following lines can be found:


return [

    // ...

    'accepted'             => 'The :attribute must be accepted.',
    'active_url'           => 'The :attribute is not a valid URL.',

    // ...


In the above translation lines, :attribute is a placeholder that can be replaced using the $parameters parameter. The following code examples will demonstrate the results of the $parameters parameter.


The signature of the __ method is:

function __(
    $replace = [],
    $locale = null

The default value of $locale is null, which will indicate to the translation service the applications default locale should be used.

Example Use

If we look in the resources/lang/en/validation.php resource file, we will see something similar to the following defined at the top of the file:


return [
    // ...
    'accepted'             => 'The :attribute must be accepted.',
    // ...

We can retrieve the accepted line like so:

$translatedText = __('validation.accepted');

If the above code was executed, the $translatedText variable would contain the following value:

The :attribute must be accepted.

However, the returned text contains the :attribute placeholder, we can supply arguments to the __ function using the $replace array parameter:

$translatedText = __('validation.accepted', [
        'attribute' => 'Terms of Service'

Now, with the newly supplied $replace argument, the $translatedText variable would now contain the following value:

The Terms of Service must be accepted.

Supplying a Locale

For the next example, we will create a new file at resources/lang/es.json to both showcase literal based language files as well as how the $locale parameter can be used.

Note: with JSON based language files, there is only one file per locale (meaning, when using JSON language files we would not have a different file for validation messages, error messages, etc).

The contents of this new validation.json file should be:

  "validation.accepted": "Los :attribute deben ser aceptados."

When using JSON based language files, we cannot nest the translation keys; for this example we are providing compatibility with dot notation by specifying our key in dot notation.

We can resolve the Spanish translation for the accepted validation message like so:

$translatedText = __('validation.accepted', [
        'attribute' => 'términos de servicio'
    ], 'es');

After the above example has executed, the $translatedText variable would contain the following value:

Los términos de servicio deben ser aceptados.

Example Within Blade Templates

The following examples demonstrate how we can retrieve language translations from within Blade templates:

{{-- Using the __ helper function --}}
        {{ __('validation.accepted',
            ['attribute' => 'Terms of Service']) 

{{-- Using the @lang directive --}}
@lang('validation.accepted', ['attribute' => 'Terms of Service'])

Start the Discussion

Leave a comment

Subscribe to our newsletter