mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #19160 -- Made lazy plural translations usable.
Many thanks to Alexey Boriskin, Claude Paroz and Julien Phalip.
This commit is contained in:
@@ -35,6 +35,10 @@ Minor features
|
||||
:class:`~django.forms.URLField` use the new type attributes available in
|
||||
HTML5 (type='email', type='url').
|
||||
|
||||
* The ``number`` argument for :ref:`lazy plural translations
|
||||
<lazy-plural-translations>` can be provided at translation time rather than
|
||||
at definition time.
|
||||
|
||||
Backwards incompatible changes in 1.6
|
||||
=====================================
|
||||
|
||||
|
||||
@@ -414,6 +414,41 @@ convert them to strings, because they should be converted as late as possible
|
||||
(so that the correct locale is in effect). This necessitates the use of the
|
||||
helper function described next.
|
||||
|
||||
.. _lazy-plural-translations:
|
||||
|
||||
Lazy translations and plural
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. versionadded:: 1.6
|
||||
|
||||
When using lazy translation for a plural string (``[u]n[p]gettext_lazy``), you
|
||||
generally don't know the ``number`` argument at the time of the string
|
||||
definition. Therefore, you are authorized to pass a key name instead of an
|
||||
integer as the ``number`` argument. Then ``number`` will be looked up in the
|
||||
dictionary under that key during string interpolation. Here's example::
|
||||
|
||||
class MyForm(forms.Form):
|
||||
error_message = ungettext_lazy("You only provided %(num)d argument",
|
||||
"You only provided %(num)d arguments", 'num')
|
||||
|
||||
def clean(self):
|
||||
# ...
|
||||
if error:
|
||||
raise forms.ValidationError(self.error_message % {'num': number})
|
||||
|
||||
If the string contains exactly one unnamed placeholder, you can interpolate
|
||||
directly with the ``number`` argument::
|
||||
|
||||
class MyForm(forms.Form):
|
||||
error_message = ungettext_lazy("You provided %d argument",
|
||||
"You provided %d arguments")
|
||||
|
||||
def clean(self):
|
||||
# ...
|
||||
if error:
|
||||
raise forms.ValidationError(self.error_message % number)
|
||||
|
||||
|
||||
Joining strings: string_concat()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user