Ticket #8768: ugettext_lazy-string-context.diff

File ugettext_lazy-string-context.diff, 2.0 KB (added by mrts, 7 years ago)
  • docs/topics/i18n.txt

    159159Lazy translation
    162 Use the function ``django.utils.translation.ugettext_lazy()`` to translate
     162Use the function :func:`django.utils.translation.ugettext_lazy` to translate
    163163strings lazily -- when the value is accessed rather than when the
    164 ``ugettext_lazy()`` function is called.
     164:func:`~django.utils.translation.ugettext_lazy` function is called.
    166166For example, to translate a model's ``help_text``, do the following::
    170170    class MyThing(models.Model):
    171171        name = models.CharField(help_text=ugettext_lazy('This is the help text'))
    173 In this example, ``ugettext_lazy()`` stores a lazy reference to the string --
    174 not the actual translation. The translation itself will be done when the string
    175 is used in a string context, such as template rendering on the Django admin site.
     173In this example, :func:`~django.utils.translation.ugettext_lazy` stores a lazy
     174reference to the string, not the actual translation. The translation itself
     175will be done when the string is used in unicode context, such as template
     176rendering on the Django admin site -- but not in ordinary string context, e.g.
     177not with the string formatting operator ``%``. If you need to enforce
     178translation in your own code, use :func:`django.utils.encoding.force_unicode`.
     180For example::
     182    >>> from django.utils.encoding import force_unicode
     183    >>> from django.utils.translation import ugettext_lazy
     185    # no translation in ordinary string context
     186    >>> str(ugettext_lazy("Hello there"))
     187    '<django.utils.functional.__proxy__ object at 0xf2b2d0>'
     189    # translation performed in explicit unicode context
     190    >>> force_unicode(ugettext_lazy("Hello there"))
     191    u'Hello there'
    177193If you don't like the verbose name ``ugettext_lazy``, you can just alias it as
    178194``_`` (underscore), like so::
Back to Top