How to translate the validator message?
dgillier opened this issue · 10 comments
Hello,
I'm trying to translate the validator message.
I added a vat_number entry in the validation.php of my lang directory with no effect ...
Any idea what I'm missing here ?
thanks, Denis
Can you try a json translation? Create a en.json
file with:
{
":attribute is not a valid VAT ID number.": ":attribute is not a valid VAT ID number."
}
And then for every language you want a separate file with the translation of the value.
https://laravel.com/docs/8.x/localization#using-translation-strings-as-keys
Maybe I'm wrong but I think it doesn't currently work because the rule should return the translated string with the trans helper.
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be uppercase.';
}
To :
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('The :attribute must be uppercase.');
}
Of course, we can override rule but overkill.
@matthieumota it's not needed: https://laravel.com/docs/8.x/validation#using-rule-objects
It works for JSON translation files.
You've tested ? Because on documentation, it's reference that need to add trans helper. I don't see helper call in framework base code.
Because on documentation, it's reference that need to add trans helper.
That's only if you use a dedicated language file.
Not working on my side (but not using json) but the old translation system…
You need to use the JSON system for this.
I can't add both systems. If I use the trans
helper then I break everything for people using JSON files. It's one or the other. For this to work you are required to use the JSON translation files.
I use JSON and not work but if I add trans
helper, it works 😢
String for translations should be wrapped in __() function, I think it's equivalent to trans(). And you can translate using PHP and JSON, doesn't matter. If it's not returned as __('message'), then it should be translated on frontend when outputing the string, but without using any helper function (Laravel __() / trans() or Vue $t for example), I don't think translation will work.