Code

Opened 19 months ago

Closed 15 months ago

Last modified 15 months ago

#19034 closed Bug (fixed)

Plural to MinLength, MaxLengt validation messages

Reported by: clay.evil@… Owned by: claudep
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 claudep 19 months ago.
Use ungettext for min/max length validators

Download all attachments as: .zip

Change History (10)

Changed 19 months ago by claudep

Use ungettext for min/max length validators

comment:1 Changed 18 months ago by claudep

  • Component changed from Uncategorized to Internationalization
  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 18 months ago by Architekt

  • Triage Stage changed from Accepted to Ready for checkin

comment:3 Changed 18 months ago by claudep

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to 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 Changed 18 months ago by void

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

comment:5 Changed 18 months ago by claudep

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 Changed 18 months ago by anonymous

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 Changed 15 months ago by claudep

  • Owner changed from nobody to claudep
  • Status changed from new to assigned

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

comment:8 Changed 15 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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 Changed 15 months ago by anonymous

Good Job, thank you all.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.