| 80 | | 1. For international character set (unicode) support, you really wanting |
|---|
| 81 | | to be using ``ugettext()``, rather than ``gettext()``. Sometimes, you |
|---|
| 82 | | should be using ``ugettext_lazy()`` as the default translation method |
|---|
| 83 | | for a particular file. By not installing ``_`` directly, the |
|---|
| 84 | | developer has to think about which is the most appropriate function |
|---|
| 85 | | to use. |
|---|
| 86 | | |
|---|
| 87 | | 2. Python's interactive shell uses ``_`` to represent "the previous |
|---|
| 88 | | result". This is also used in doctest tests and having ``_()`` causes |
|---|
| 89 | | interference. Explicitly importing ``ugettext()`` as ``_()`` avoids |
|---|
| 90 | | this problem. |
|---|
| | 79 | 1. For international character set (Unicode) support, ``ugettext()`` is |
|---|
| | 80 | more useful than ``gettext()``. Sometimes, you should be using |
|---|
| | 81 | ``ugettext_lazy()`` as the default translation method for a particular |
|---|
| | 82 | file. Without ``_()`` in the global namespace, the developer has to |
|---|
| | 83 | think about which is the most appropriate translation function. |
|---|
| | 84 | |
|---|
| | 85 | 2. The underscore character (``_``) is used to represent "the previous |
|---|
| | 86 | result" in Python's interactive shell and doctest tests. Installing a |
|---|
| | 87 | global ``_()`` function causes interference. Explicitly importing |
|---|
| | 88 | ``ugettext()`` as ``_()`` avoids this problem. |
|---|
| 331 | | There are a lot of useful utility functions in Django (particularly in |
|---|
| 332 | | ``django.utils``) that take a string as their first argument and do something |
|---|
| 333 | | to that string. These functions are used by template filters as well as |
|---|
| 334 | | directly in other code. |
|---|
| 335 | | |
|---|
| 336 | | If you write your own similar functions, you will rapidly come across the |
|---|
| 337 | | problem of what to do when the first argument is a lazy translation object. |
|---|
| 338 | | You don't want to convert it to a string immediately, because you may be using |
|---|
| 339 | | this function outside of a view (and hence the current thread's locale setting |
|---|
| 340 | | will not be correct). For cases like this, the |
|---|
| 341 | | ``django.utils.functional.allow_lazy()`` decorator will be useful. It modifies |
|---|
| 342 | | the function so that *if* it is called with a lazy translation as the first |
|---|
| 343 | | argument, the function evaluation is delayed until it needs to be converted to |
|---|
| 344 | | a string. |
|---|
| | 329 | Django offers many utility functions (particularly in ``django.utils``) that |
|---|
| | 330 | take a string as their first argument and do something to that string. These |
|---|
| | 331 | functions are used by template filters as well as directly in other code. |
|---|
| | 332 | |
|---|
| | 333 | If you write your own similar functions and deal with translations, you'll |
|---|
| | 334 | face the problem of what to do when the first argument is a lazy translation |
|---|
| | 335 | object. You don't want to convert it to a string immediately, because you might |
|---|
| | 336 | be using this function outside of a view (and hence the current thread's locale |
|---|
| | 337 | setting will not be correct). |
|---|
| | 338 | |
|---|
| | 339 | For cases like this, use the ``django.utils.functional.allow_lazy()`` |
|---|
| | 340 | decorator. It modifies the function so that *if* it's called with a lazy |
|---|
| | 341 | translation as the first argument, the function evaluation is delayed until it |
|---|
| | 342 | needs to be converted to a string. |
|---|