Translations in Laravel

Author
By Darío Rivera
Posted On in Laravel

Translations in Laravel allow you to display your application in different languages easily and quickly. This allows your application to have a much greater reach depending on the languages you have enabled. Each language will generally be located in the resources/lang folder. By default Laravel is configured in English and will bring the resources/lang/en folder. If you want your application to display text in both English and Spanish, you must create the ../es folder. Within each language folder, there can be different files depending on the context, for example, Laravel comes with the passwords.php file by default, which contains the following content.

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Password Reset Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are the default lines which match reasons
    | that are given by the password broker for a password update attempt
    | has failed, such as for an invalid token or invalid new password.
    |
    */

    'reset' => 'Your password has been reset!',
    'sent' => 'We have e-mailed your password reset link!',
    'token' => 'This password reset token is invalid.',
    'user' => "We can't find a user with that e-mail address.",
    'throttled' => 'Please wait before retrying.',

];

Each file must return an array of keys and values where each key will contain a translation identifier and each value the translation itself.

Create a translation

To create a translation we are going to assume that you have a laravel project previously created.If not, you can visit our post Laravel Installation to create a new project quickly. After this, you are going to create the resources/lang/es folder. Next, we are going to create the greetings.php file in both en/es translation folders. We present below the content that each file should have.

/en/greetings.php

<?php

return [
    'hello' => 'Hello World',
];

/es/greetings.php

<?php

return [
    'hello' => 'Hola Mundo',
];

Using a translation

We are going to modify the file that comes by default with the Laravel installation called welcome.blade.php in the resoureces/views folder. To use the text of the translation, simply replace the string Laravel with the value of the translation taken from the lang helper.

@lang('greetings.hello')

Or,

{{ '\{\{' }} __('greetings.hello') {{ '\}\}' }}

Translations with parameters

Often it is necessary to display text where a word or section is dynamic. For this, simply indicate in the translation definition an identifier in the following way:

return [
    'hello' => 'Hello :name',
];

To indicate the value that said identifier takes, simply pass it as the second parameter when displaying the message.

echo __('greetings.hello', ['name' => 'Steave']);

You can also indicate if the parameter should be shown in all uppercase by placing the parameter as :NAME or if you only want to show the first letter in uppercase, you should indicate the parameter as :Name.

Pluralization

In cases where it is necessary to translate a word in both its singular and plural form, we could create two entries in our translations array. However, Laravel provides a much more elegant way of dealing with this problem through the trans_choice helper.

Suppose we want to show the string result and results depending on whether a search returned one or multiple results. The correct entry in our array would be as follows:

return [
    'search' => 'result|results',
];

If we want to display the first singular value, we can use the helper as follows:

echo trans_choice('forms.search', 1);

If we want to display the plural value, we should pass the number 2 as the second parameter of the helper.

echo trans_choice('forms.search', 2);

Until next time!.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.