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:
1<?php
2
3return [
4
5 // ...
6
7 'accepted' => 'The :attribute must be accepted.',
8 'active_url' => 'The :attribute is not a valid URL.',
9
10 // ...
11
12];
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.
#Signature
The signature of the __
method is:
1function __(
2 $key,
3 $replace = [],
4 $locale = null
5);
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:
1<?php
2
3return [
4 // ...
5 'accepted' => 'The :attribute must be accepted.',
6 // ...
7];
We can retrieve the accepted
line like so:
1$translatedText = __('validation.accepted');
If the above code was executed, the $translatedText
variable would contain the following value:
1The :attribute must be accepted.
However, the returned text contains the :attribute
placeholder, we can supply arguments to the __
function using the $replace
array parameter:
1$translatedText = __('validation.accepted', [
2 'attribute' => 'Terms of Service'
3 ]);
Now, with the newly supplied $replace
argument, the $translatedText
variable would now contain the following value:
1The 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:
1{
2 "validation.accepted": "Los :attribute deben ser aceptados."
3}
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:
1$translatedText = __('validation.accepted', [
2 'attribute' => 'términos de servicio'
3 ], 'es');
After the above example has executed, the $translatedText
variable would contain the following value:
1Los 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:
1{{-- Using the __ helper function --}}
2 <p>
3 {{ __('validation.accepted',
4 ['attribute' => 'Terms of Service'])
5 }}
6 </p>
7
8{{-- Using the @lang directive --}}
9@lang('validation.accepted', ['attribute' => 'Terms of Service'])
∎