Home > Back-end >  How to translate Laravel "hardcoded" string "(and 1 more error)", using dynamic
How to translate Laravel "hardcoded" string "(and 1 more error)", using dynamic

Time:09-30

I have some validations in place, and I am validating it like this:

validator make

and I noticed when there is more than 1 error, laravel will add to the message "and 1(or 2 or 4..) more error(s)" ..

Take a look at the images and you will see what I mean:

validator message response

I found out where this is happening inside laravel framework / which function:

Validation Exception summarize function

it calls the Translater get method with a dynamic key! How would I translate dynamic keys??

translator interface

I tried a few things:

  • Adding the key with the place holder in both English/Portuguese "validation" translation file
  • Add the key with the number itself (replaced placeholder)

none took effect, see what I mean below:

dynamic translation key with placeholder translation key with replaced placeholder

I've noticed there are other people with the same issue when searching in google but so far I have not found a solution, I would like not to have to do a "string replace" and use a more elegant solution... Any one could help?

other people with the same issue: remove and 1 more error from laravel validation exception summarize

CodePudding user response:

The (and :count more error) and (and :count more errors) need to be added to your json translation files as this is not a "short key" but a "translation string": https://laravel.com/docs/9.x/localization#using-translation-strings-as-keys

  • Related