Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for DataAnnotations #9

Open
Kirlu opened this issue May 7, 2018 · 1 comment
Open

Support for DataAnnotations #9

Kirlu opened this issue May 7, 2018 · 1 comment

Comments

@Kirlu
Copy link

Kirlu commented May 7, 2018

Would it be possible to support DataAnnotations since these could have a ErrorMessage associated with them?
In our Entity models we fx. use the Required DataAnnotation attribute, this can have an ErrorMessage, a string which could be translated. In our case we manually translate the ErrorMessage, but we are having trouble localizing this since it can only be a const string when initialized.
As I see it we have three options:

  1. making a PowerShell script manually extracting all ErrorMessage = "Something" in DataAnnotations.
  2. adding a prefix to strings which would then all be captured using PowerShell fx. "i18n:Something", this would require ngettext-wpf to remove the prefix before adding it to the po files.
  3. making a new localized edition of the DataAnnotations, this would work although some of the DataAnnotations are used by fx EntityFramework and are sealed so no inheritance is possible.

What are your thoughts on this?

@robert-j-engdahl
Copy link
Owner

This is an annoying issue, because we don't have c style macros in C# (and I guess we aren't about to run the c preprocessor either).

I assume the localization part is something you (we) are already handling, and it is only the internationalization (extraction of untranslated messages) that is the issue here.

The classical work-around for "this string should be translated, but not right now" is string Translation.Noop(string msgId) which returns the msgId unchanged but can be recognized by the original xgettext tool given -kNoop option.

[Required(ErrorMessage="This field is required!")]
public int? SomeProperty {get; set;}

Translation.Noop("This field is required")

And given the badness of other options, I think this is what I would go for.

Now that would be an easy solution, but it is error prone as the error message is duplicated.

An awesome solution would be to add support for named attribute parameters in xgettext. Given xgettext already has some ability to parse C# that might actually be an option. Then the folowing would be enough:

[Required(ErrorMessage="This field is required!")]
public int? SomeProperty {get; set;}

when xgettext is invoked with something imaginary option like -kRequired(ErrorMessage). Changing the options might be harder than supporting the named attribute parametes, so -kErrorMessage is perhaps cleaner.

Finally, we could just make a PowerShell script that extracted all strings assigned to a property named ErrorMessage. I.e.

"ErrorMessage *= *\"([^"]*)\""

The rest of the script would look somewhat like Xgettext.Xaml.ps1

@D-Bullock D-Bullock mentioned this issue Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants