Ticket #8768: ugettext_lazy-string-context.diff
File ugettext_lazy-string-context.diff, 2.0 KB (added by , 16 years ago) |
---|
-
docs/topics/i18n.txt
159 159 Lazy translation 160 160 ~~~~~~~~~~~~~~~~ 161 161 162 Use the function ``django.utils.translation.ugettext_lazy()`` to translate162 Use the function :func:`django.utils.translation.ugettext_lazy` to translate 163 163 strings 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. 165 165 166 166 For example, to translate a model's ``help_text``, do the following:: 167 167 … … 170 170 class MyThing(models.Model): 171 171 name = models.CharField(help_text=ugettext_lazy('This is the help text')) 172 172 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. 173 In this example, :func:`~django.utils.translation.ugettext_lazy` stores a lazy 174 reference to the string, not the actual translation. The translation itself 175 will be done when the string is used in unicode context, such as template 176 rendering on the Django admin site -- but not in ordinary string context, e.g. 177 not with the string formatting operator ``%``. If you need to enforce 178 translation in your own code, use :func:`django.utils.encoding.force_unicode`. 176 179 180 For example:: 181 182 >>> from django.utils.encoding import force_unicode 183 >>> from django.utils.translation import ugettext_lazy 184 185 # no translation in ordinary string context 186 >>> str(ugettext_lazy("Hello there")) 187 '<django.utils.functional.__proxy__ object at 0xf2b2d0>' 188 189 # translation performed in explicit unicode context 190 >>> force_unicode(ugettext_lazy("Hello there")) 191 u'Hello there' 192 177 193 If you don't like the verbose name ``ugettext_lazy``, you can just alias it as 178 194 ``_`` (underscore), like so:: 179 195