skip to Main Content

I would like to translate my error messages with Laravel 11.

I spotted this very reputable repository.

I installed it and added the "fr" translations.

When I trigger an error with the "en" locale, then I get a very clean, understandable error message. Example:

"last_name": [
        "The last name field must not be greater than 50 characters."
    ]

When I trigger the same error with the "fr" locale, then I have a message translated into French, but the attribute is also translated. Example:

"last_name": [
            "Le texte de nom ne peut pas contenir plus de 50 caractères."
   ]

Which no longer means anything. I was expecting a translation like this

Le champ last name ne peut pas contenir plus de 50 caractères.

My question: how to prohibit this translation of the attribute?

It’s not easy to explain this translation issue in English with examples in French, I hope my explanations are clear.

2

Answers


  1. Chosen as BEST ANSWER

    I found what happens.

    After the installation of the "fr" locale (php artisan lang:add fr), the /lang/fr/validation.php file has been installed in the project.

    In this file, there is an 'attributes' array which contains a lot of translations of common word (for example "area" => "zone").

    I have commented this attributes array, and now I have the error messages like I want, without attributes translation.

    More detail also in the documentation here.


  2. Have you tried translating the attribute when using the trans/__ helper? For example:

    __('validation.required', ['attribute' => __('last_name')])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search