driesvints / vat-calculator

Handle all the hard stuff related to EU MOSS tax/vat regulations, the way it should be.
MIT License
1.2k stars 88 forks source link

How to translate the validator message? #146

Closed dgillier closed 2 years ago

dgillier commented 2 years ago

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

driesvints commented 2 years ago

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

matthieumota commented 6 months ago

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.

driesvints commented 6 months ago

@matthieumota it's not needed: https://laravel.com/docs/8.x/validation#using-rule-objects

It works for JSON translation files.

matthieumota commented 6 months ago

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.

dgillier commented 6 months ago

Not working on my side (but not using json) but the old translation system…

On 5 Mar 2024, at 09:57, Matthieu Mota @.***> wrote:

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 https://github.com/laravel/framework/blob/8.x/src/Illuminate/Validation/Validator.php#L817.

— Reply to this email directly, view it on GitHub https://github.com/driesvints/vat-calculator/issues/146#issuecomment-1978254248, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJBTUDASYY7DGLARD2L5O3DYWWCGJAVCNFSM54IJJZHKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJXHAZDKNBSGQ4A. You are receiving this because you authored the thread.

driesvints commented 6 months ago

Because on documentation, it's reference that need to add trans helper.

That's only if you use a dedicated language file.

driesvints commented 6 months ago

Not working on my side (but not using json) but the old translation system…

You need to use the JSON system for this.

driesvints commented 6 months ago

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.

matthieumota commented 6 months ago

I use JSON and not work but if I add trans helper, it works :cry:

kikky7 commented 2 months ago

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.