Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#19034 closed Bug (fixed)

Plural to MinLength, MaxLengt validation messages

Reported by: clay.evil@… 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)

19034.diff (1.8 KB ) - added by Claude Paroz 12 years ago.
Use ungettext for min/max length validators

Download all attachments as: .zip

Change History (10)

by Claude Paroz, 12 years ago

Attachment: 19034.diff added

Use ungettext for min/max length validators

comment:1 by Claude Paroz, 12 years ago

Component: UncategorizedInternationalization
Has patch: set
Triage Stage: UnreviewedAccepted

comment:2 by Jan Bednařík, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:3 by Claude Paroz, 12 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

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 Alexey Boriskin, 12 years ago

Alternative patch: https://github.com/django/django/pull/462 (also includes patch for #19158)

comment:5 by Claude Paroz, 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 anonymous, 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 Claude Paroz, 11 years ago

Owner: changed from nobody to Claude Paroz
Status: newassigned

Now that #19160 is fixed, the resolution will be trivial.

comment:8 by Claude Paroz <claude@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In fc8efc2d9e693923d3da272e0978e5b0631006f9:

Fixed #19034 -- Added proper i18n pluralization for max/min length validation messages

This was made possible by commit 3f1a0c0040b9. Thanks Evil Clay
for the report and Alexey Boriskin his work on the ticket.

comment:9 by anonymous, 11 years ago

Good Job, thank you all.

Note: See TracTickets for help on using tickets.
Back to Top