skwasjer / IbanNet

C# .NET IBAN validator, parser, builder and generator
Apache License 2.0
119 stars 31 forks source link

How to have translated validation error messages? #94

Closed jeroenheijmans closed 1 year ago

jeroenheijmans commented 1 year ago

I see the error messages come from a .resx file. When using the library in my own project, is there a (recommended) way to add error messages translated in my own language? For example by adding a nl-NL resx file as an embedded resource or something similar?

I checked the wiki but found mostly information on how to use this library.

Thanks a bunch for a nice bit of open source, either way! <3

skwasjer commented 1 year ago

Indeed a <Filename>.<locale>.resx would need to be added to this repo and included as part of the build. If you are up for it, be my guest. I do think there may be some areas where strings possibly still need to be extracted from the code into the resources files (some exceptions are very niche/edge case that I did not bother to add to resx file).

Note that you can ignore the IbanNet.CodeGen library, as this is only used at build time.

skwasjer commented 1 year ago

If you just want to customize the validation message itself using DataAnnotations or FluentValidation, you can also do that simply by:

DataAnnotations

[Iban(ErrorMessageResourceName = "MyResourceName", ErrorMessageResourceType = typeof(Resources))]
public string BankAccount { get; set; }

FluentValidation

RuleFor(x => x.BankAccount).Iban(ibanValidator).WithMessage(Resources.MyResourceName) // from resource file
jeroenheijmans commented 1 year ago

Ooh nice, the latter comment's option I'll investigate, seems to be just what I need.

I could add the nl-NL resources to the library itself in a PR as well. And you'd be most welcome to ping me for translations of new/changed strings. But... I can also imagine as an OSS maintainer you'd prefer not to have the burden of (potentially stale) extra locales.

Let me know if you want nl-NL translation contributions from my end, or prefer to leave it as is.

PS. If we leave it as is I'll check your other suggestion and instead write up some docs for the repository or wiki for others to find.

skwasjer commented 1 year ago

Any contrib is appreciated, both to the wiki and code. Localization is fine, yes it can be cumbersome to maintain but I don't expect many textual changes. Plus, I can help with Dutch ;P