Fixed #20582 -- Allowed default Form.label_suffix to be translated

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2013-07-13 11:43:14 +02:00
parent 73f86f4441
commit 7557207983
4 changed files with 31 additions and 10 deletions

View File

@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Django\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-05-25 14:27+0200\n"
"POT-Creation-Date: 2013-06-11 18:44+0200\n"
"PO-Revision-Date: 2010-05-13 15:35+0200\n"
"Last-Translator: Django team\n"
"Language-Team: English <en@li.org>\n"
@ -699,11 +699,22 @@ msgstr ""
msgid "Enter a list of values."
msgstr ""
#: forms/forms.py:158
#. Translators: This is the default suffix added to form field labels
#: forms/forms.py:90
msgid ":"
msgstr ""
#: forms/forms.py:159
#, python-format
msgid "(Hidden field %(name)s) %(error)s"
msgstr ""
#. Translators: If found as last label character, these punctuation
#. characters will prevent the default label_suffix to be appended to the label
#: forms/forms.py:525
msgid ":?.!"
msgstr ""
#: forms/formsets.py:310
#, python-format
msgid "Please submit %d or fewer forms."

View File

@ -77,7 +77,7 @@ class BaseForm(object):
# information. Any improvements to the form API should be made to *this*
# class, not to the Form class.
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=':',
initial=None, error_class=ErrorList, label_suffix=None,
empty_permitted=False):
self.is_bound = data is not None or files is not None
self.data = data or {}
@ -86,7 +86,8 @@ class BaseForm(object):
self.prefix = prefix
self.initial = initial or {}
self.error_class = error_class
self.label_suffix = label_suffix
# Translators: This is the default suffix added to form field labels
self.label_suffix = label_suffix if label_suffix is not None else _(':')
self.empty_permitted = empty_permitted
self._errors = None # Stores the errors after clean() has been called.
self._changed_data = None
@ -518,7 +519,9 @@ class BoundField(object):
"""
contents = contents or self.label
# Only add the suffix if the label does not end in punctuation.
if self.form.label_suffix and contents and contents[-1] not in ':?.!':
# Translators: If found as last label character, these punctuation
# characters will prevent the default label_suffix to be appended to the label
if self.form.label_suffix and contents and contents[-1] not in _(':?.!'):
contents = format_html('{0}{1}', contents, self.form.label_suffix)
widget = self.field.widget
id_ = widget.attrs.get('id') or self.auto_id

View File

@ -292,7 +292,7 @@ class ModelFormMetaclass(type):
class BaseModelForm(BaseForm):
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=':',
initial=None, error_class=ErrorList, label_suffix=None,
empty_permitted=False, instance=None):
opts = self._meta
if opts.model is None:

View File

@ -500,9 +500,15 @@ By default, ``auto_id`` is set to the string ``'id_%s'``.
.. attribute:: Form.label_suffix
Normally, a colon (``:``) will be appended after any label name when a form is
rendered. It's possible to change the colon to another character, or omit it
entirely, using the ``label_suffix`` parameter::
A translatable string (defaults to a colon (``:``) in English) that will be
appended after any label name when a form is rendered.
.. versionchanged:: 1.6
The default ``label_suffix`` is translatable.
It's possible to customize that character, or omit it entirely, using the
``label_suffix`` parameter::
>>> f = ContactForm(auto_id='id_for_%s', label_suffix='')
>>> print(f.as_ul())
@ -518,7 +524,8 @@ entirely, using the ``label_suffix`` parameter::
<li><label for="id_for_cc_myself">Cc myself -></label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>
Note that the label suffix is added only if the last character of the
label isn't a punctuation character (``.``, ``!``, ``?`` or ``:``)
label isn't a punctuation character (in English, those are ``.``, ``!``, ``?``
or ``:``).
Notes on field ordering
~~~~~~~~~~~~~~~~~~~~~~~