#19034 closed Bug (fixed)
Plural to MinLength, MaxLengt validation messages
Reported by: | Owned by: | Claude Paroz | |
---|---|---|---|
Component: | Internationalization | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
The validator messages Ensure this value has at least/most %(limit_value)d characters (it has %(show_value)d). should use plural format.
Must have at least 1 characters? My native language (slovak) has 3 forms so this get even worse.
Devil is in the detail, and such details will make Django even more awesome.
Thank You.
Attachments (1)
Change History (10)
by , 12 years ago
Attachment: | 19034.diff added |
---|
comment:1 by , 12 years ago
Component: | Uncategorized → Internationalization |
---|---|
Has patch: | set |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:3 by , 12 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
Sorry, but I'm retracting my own patch, because it prevents customization of the validators message. See #19160 for another possible approach to this problem.
comment:4 by , 12 years ago
Alternative patch: https://github.com/django/django/pull/462 (also includes patch for #19158)
comment:5 by , 12 years ago
Thanks Alexey for the patch. It may be used as a temporary workaround if we cannot find a good way to resolve #19160.
Basically, if you use a lambda, then the lazy aspect of the translation function is not needed anymore. The advantage of the lazy over lambda is that you can transparently use the string as if it was a normal string. Imagine a user wants to customize the error messages himself, he'd have to set message_function (and this should be documented) unless he would have the same original issue with plurals.
So I'd really like to find a way to make ungettext_lazy really usable, if possible.
comment:6 by , 12 years ago
i don't know if its any help, but this is my solution to the problem. Im using it in my project.
class NewBaseValidator(BaseValidator):
def init(self, *args, kwargs):
# make possible to customise messages
if "message" in kwargs:
self.message = kwargs.pop("message")
# make possible to customise code
if "code" in kwargs:
self.code = kwargs.pop("code")
super(NewBaseValidator, self).init(*args, kwargs)
# if no message is defined at class level, call the _get_message method
if not self.message:
self.message = self._get_message()
def _get_message(self):
raise NotImplementedError
class MinLengthValidator(NewBaseValidator):
compare = lambda self, a, b: a < b
clean = lambda self, x: len(x)
message = None
code = 'min_length'
def _get_message(self):
return ungettext_lazy(u'Ensure this value has at least %(limit_value)d character (it has %(show_value)d).',
u'Ensure this value has at least %(limit_value)d characters (it has %(show_value)d).', self.limit_value)
class MaxLengthValidator(NewBaseValidator):
compare = lambda self, a, b: a > b
clean = lambda self, x: len(x)
message = None
code = 'max_length'
def _get_message(self):
return ungettext_lazy(u'Ensure this value has at most %(limit_value)d character (it has %(show_value)d).',
u'Ensure this value has at most %(limit_value)d characters (it has %(show_value)d).',self.limit_value)
comment:7 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Now that #19160 is fixed, the resolution will be trivial.
comment:8 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Use ungettext for min/max length validators