Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10260 - Refactored internationalization documentation. Thanks,…

… Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12440 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f93f056c32f614c9a130ec66824d94ec20526cdf 1 parent 9b630a0
Jannis Leidel authored February 16, 2010
2  django/conf/global_settings.py
@@ -306,7 +306,7 @@
306 306
 # http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now
307 307
 MONTH_DAY_FORMAT = 'F j'
308 308
 
309  
-# Default shortformatting for date objects. See all available format strings here:
  309
+# Default short formatting for date objects. See all available format strings here:
310 310
 # http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now
311 311
 SHORT_DATE_FORMAT = 'm/d/Y'
312 312
 
72  docs/howto/i18n.txt
... ...
@@ -0,0 +1,72 @@
  1
+.. _howto-i18n:
  2
+
  3
+.. _using-translations-in-your-own-projects:
  4
+
  5
+===============================================
  6
+Using internationalization in your own projects
  7
+===============================================
  8
+
  9
+At runtime, Django looks for translations by following this algorithm:
  10
+
  11
+    * First, it looks for a ``locale`` directory in the application directory
  12
+      of the view that's being called. If it finds a translation for the
  13
+      selected language, the translation will be installed.
  14
+    * Next, it looks for a ``locale`` directory in the project directory. If it
  15
+      finds a translation, the translation will be installed.
  16
+    * Finally, it checks the Django-provided base translation in
  17
+      ``django/conf/locale``.
  18
+
  19
+In all cases the name of the directory containing the translation is expected to
  20
+be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,
  21
+etc.
  22
+
  23
+This way, you can write applications that include their own translations, and
  24
+you can override base translations in your project path. Or, you can just build
  25
+a big project out of several apps and put all translations into one big project
  26
+message file. The choice is yours.
  27
+
  28
+.. note::
  29
+
  30
+    If you're using manually configured settings, as described in
  31
+    :ref:`settings-without-django-settings-module`, the ``locale`` directory in
  32
+    the project directory will not be examined, since Django loses the ability
  33
+    to work out the location of the project directory. (Django normally uses the
  34
+    location of the settings file to determine this, and a settings file doesn't
  35
+    exist if you're manually configuring your settings.)
  36
+
  37
+All message file repositories are structured the same way. They are:
  38
+
  39
+    * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
  40
+    * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
  41
+    * All paths listed in ``LOCALE_PATHS`` in your settings file are
  42
+      searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)``
  43
+    * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)``
  44
+
  45
+To create message files, you use the :djadmin:`django-admin.py makemessages <makemessages>`
  46
+tool. You only need to be in the same directory where the ``locale/`` directory
  47
+is located. And you use :djadmin:`django-admin.py compilemessages <compilemessages>`
  48
+to produce the binary ``.mo`` files that are used by ``gettext``. Read the
  49
+:ref:`topics-i18n-localization` document for more details.
  50
+
  51
+You can also run ``django-admin.py compilemessages --settings=path.to.settings``
  52
+to make the compiler process all the directories in your :setting:`LOCALE_PATHS`
  53
+setting.
  54
+
  55
+Application message files are a bit complicated to discover -- they need the
  56
+:class:`~django.middleware.locale.LocaleMiddleware`. If you don't use the
  57
+middleware, only the Django message files and project message files will be
  58
+installed and available at runtime.
  59
+
  60
+Finally, you should give some thought to the structure of your translation
  61
+files. If your applications need to be delivered to other users and will
  62
+be used in other projects, you might want to use app-specific translations.
  63
+But using app-specific translations and project translations could produce
  64
+weird problems with ``makemessages``: It will traverse all directories below
  65
+the current path and so might put message IDs into the project message file
  66
+that are already in application message files.
  67
+
  68
+The easiest way out is to store applications that are not part of the project
  69
+(and so carry their own translations) outside the project tree. That way,
  70
+``django-admin.py makemessages`` on the project level will only translate
  71
+strings that are connected to your explicit project and not strings that are
  72
+distributed independently.
1  docs/howto/index.txt
@@ -20,6 +20,7 @@ you quickly accomplish common tasks.
20 20
    deployment/index
21 21
    error-reporting
22 22
    initial-data
  23
+   i18n
23 24
    jython
24 25
    legacy-databases
25 26
    outputting-csv
19  docs/internals/contributing.txt
@@ -402,15 +402,18 @@ translated, here's what to do:
402 402
 
403 403
     * Join the `Django i18n mailing list`_ and introduce yourself.
404 404
 
  405
+    * Make sure you read the notes about :ref:`specialties-of-django-i18n`.
  406
+
405 407
     * Create translations using the methods described in the
406  
-      :ref:`i18n documentation <topics-i18n>`. For this you will use the
407  
-      ``django-admin.py makemessages`` tool. In this particular case it should
408  
-      be run from the top-level ``django`` directory of the Django source tree.
  408
+      :ref:`localization documentation <topics-i18n-localization>`. For this
  409
+      you will use the ``django-admin.py makemessages`` tool. In this
  410
+      particular case it should be run from the top-level ``django`` directory
  411
+      of the Django source tree.
409 412
 
410 413
       The script runs over the entire Django source tree and pulls out all
411 414
       strings marked for translation. It creates (or updates) a message file in
412  
-      the directory ``conf/locale`` (for example for ``pt-BR``, the file will be
413  
-      ``conf/locale/pt-br/LC_MESSAGES/django.po``).
  415
+      the directory ``conf/locale`` (for example for ``pt_BR``, the file will be
  416
+      ``conf/locale/pt_BR/LC_MESSAGES/django.po``).
414 417
 
415 418
     * Make sure that ``django-admin.py compilemessages -l <lang>`` runs without
416 419
       producing any warnings.
@@ -419,7 +422,11 @@ translated, here's what to do:
419 422
       ``-d djangojs`` command line option to the ``django-admin.py``
420 423
       invocations).
421 424
 
422  
-    * Create a diff of the ``.po`` file(s) against the current Subversion trunk.
  425
+    * Optionally, review and update the ``conf/locale/<locale>/formats.py``
  426
+      file to describe the date, time and numbers formatting particularities of
  427
+      your locale. See :ref:`format-localization` for details.
  428
+
  429
+    * Create a diff against the current Subversion trunk.
423 430
 
424 431
     * Open a ticket in Django's ticket system, set its ``Component`` field to
425 432
       ``Translations``, and attach the patch to it.
14  docs/ref/settings.txt
@@ -883,8 +883,8 @@ LANGUAGE_CODE
883 883
 Default: ``'en-us'``
884 884
 
885 885
 A string representing the language code for this installation. This should be in
886  
-standard language format. For example, U.S. English is ``"en-us"``. See
887  
-:ref:`topics-i18n`.
  886
+standard :term:`language format<language code>`. For example, U.S. English is
  887
+``"en-us"``. See :ref:`topics-i18n`.
888 888
 
889 889
 .. setting:: LANGUAGE_COOKIE_NAME
890 890
 
@@ -911,9 +911,11 @@ see the current list of translated languages by looking in
911 911
 
912 912
 .. _online source: http://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py
913 913
 
914  
-The list is a tuple of two-tuples in the format (language code, language
915  
-name) -- for example, ``('ja', 'Japanese')``. This specifies which languages
916  
-are available for language selection. See :ref:`topics-i18n`.
  914
+The list is a tuple of two-tuples in the format ``(language code, language
  915
+name)``, the ``language code`` part should be a
  916
+:term:`language name<language code>` -- for example, ``('ja', 'Japanese')``.
  917
+This specifies which languages are available for language selection. See
  918
+:ref:`topics-i18n`.
917 919
 
918 920
 Generally, the default value should suffice. Only set this setting if you want
919 921
 to restrict language selection to a subset of the Django-provided languages.
@@ -948,7 +950,7 @@ LOCALE_PATHS
948 950
 Default: ``()`` (Empty tuple)
949 951
 
950 952
 A tuple of directories where Django looks for translation files.
951  
-See :ref:`translations-in-your-own-projects`.
  953
+See :ref:`using-translations-in-your-own-projects`.
952 954
 
953 955
 .. setting:: LOGIN_REDIRECT_URL
954 956
 
17  docs/releases/1.2.txt
@@ -516,6 +516,19 @@ documentation <ref-contrib-syndication>`.
516 516
 
517 517
 .. _RSS best practices: http://www.rssboard.org/rss-profile
518 518
 
  519
+Technical message IDs
  520
+---------------------
  521
+
  522
+Up to version 1.1 Django used :ref:`technical message IDs<technical-messages>`
  523
+to provide localizers the possibility to translate date and time formats. They
  524
+were translatable :term:`translation strings <translation string>` that could
  525
+be recognized because they were all upper case (for example
  526
+``DATETIME_FORMAT``, ``DATE_FORMAT``, ``TIME_FORMAT``). They have been
  527
+deprecated in favor of the new :ref:`Format localization
  528
+<format-localization>` infrastructure that allows localizers to specify that
  529
+information in a ``formats.py`` file in the corresponding
  530
+``django/conf/locale/<locale name>/`` directory.
  531
+
519 532
 What's new in Django 1.2
520 533
 ========================
521 534
 
@@ -577,7 +590,7 @@ added support for comparison operators. No longer will you have to type:
577 590
 .. code-block:: html+django
578 591
 
579 592
     {% ifnotequal a b %}
580  
-    ...
  593
+     ...
581 594
     {% endifnotequal %}
582 595
 
583 596
 You can now do this:
@@ -585,7 +598,7 @@ You can now do this:
585 598
 .. code-block:: html+django
586 599
 
587 600
     {% if a != b %}
588  
-    ...
  601
+     ...
589 602
     {% endif %}
590 603
 
591 604
 There's really no reason to use ``{% ifequal %}`` or ``{% ifnotequal %}``
1,144  docs/topics/i18n.txt
... ...
@@ -1,1144 +0,0 @@
1  
-.. _topics-i18n:
2  
-
3  
-====================
4  
-Internationalization
5  
-====================
6  
-
7  
-Django has full support for internationalization of text in code and
8  
-templates, and format localization of dates and numbers. Here's how it works.
9  
-
10  
-Overview
11  
-========
12  
-
13  
-The goal of internationalization is to allow a single Web application to offer
14  
-its content and functionality in multiple languages and locales.
15  
-
16  
-For text translation, you, the Django developer, can accomplish this goal by
17  
-adding a minimal amount of hooks to your Python code and templates. These hooks
18  
-are called **translation strings**. They tell Django: "This text should be
19  
-translated into the end user's language, if a translation for this text is
20  
-available in that language."
21  
-
22  
-Django takes care of using these hooks to translate Web apps, on the fly,
23  
-according to users' language preferences.
24  
-
25  
-Essentially, Django does two things:
26  
-
27  
-    * It lets developers and template authors specify which parts of their apps
28  
-      should be translatable.
29  
-    * It uses these hooks to translate Web apps for particular users according
30  
-      to their language preferences.
31  
-
32  
-For format localization, it's just necessary to set
33  
-:setting:`USE_L10N = True <USE_L10N>` in your settings file. If
34  
-:setting:`USE_L10N` is set to ``True``, Django will display
35  
-numbers and dates in the format of the current locale. That includes field
36  
-representation on templates, and allowed input formats on the admin.
37  
-
38  
-If you don't need internationalization in your app
39  
-==================================================
40  
-
41  
-Django's internationalization hooks are on by default, and that means there's a
42  
-bit of i18n-related overhead in certain places of the framework. If you don't
43  
-use internationalization, you should take the two seconds to set
44  
-:setting:`USE_I18N = False <USE_I18N>` in your settings file. If
45  
-:setting:`USE_I18N` is set to ``False``, then Django will make some
46  
-optimizations so as not to load the internationalization machinery.
47  
-
48  
-You'll probably also want to remove ``'django.core.context_processors.i18n'``
49  
-from your ``TEMPLATE_CONTEXT_PROCESSORS`` setting.
50  
-
51  
-If you do need internationalization: three steps
52  
-================================================
53  
-
54  
-    1. Embed translation strings in your Python code and templates.
55  
-    2. Get translations for those strings, in whichever languages you want to
56  
-       support.
57  
-    3. Activate the locale middleware in your Django settings.
58  
-
59  
-.. admonition:: Behind the scenes
60  
-
61  
-    Django's translation machinery uses the standard ``gettext`` module that
62  
-    comes with Python.
63  
-
64  
-1. How to specify translation strings
65  
-=====================================
66  
-
67  
-Translation strings specify "This text should be translated." These strings can
68  
-appear in your Python code and templates. It's your responsibility to mark
69  
-translatable strings; the system can only translate strings it knows about.
70  
-
71  
-In Python code
72  
---------------
73  
-
74  
-Standard translation
75  
-~~~~~~~~~~~~~~~~~~~~
76  
-
77  
-Specify a translation string by using the function ``ugettext()``. It's
78  
-convention to import this as a shorter alias, ``_``, to save typing.
79  
-
80  
-.. note::
81  
-    Python's standard library ``gettext`` module installs ``_()`` into the
82  
-    global namespace, as an alias for ``gettext()``. In Django, we have chosen
83  
-    not to follow this practice, for a couple of reasons:
84  
-
85  
-      1. For international character set (Unicode) support, ``ugettext()`` is
86  
-         more useful than ``gettext()``. Sometimes, you should be using
87  
-         ``ugettext_lazy()`` as the default translation method for a particular
88  
-         file. Without ``_()`` in the global namespace, the developer has to
89  
-         think about which is the most appropriate translation function.
90  
-
91  
-      2. The underscore character (``_``) is used to represent "the previous
92  
-         result" in Python's interactive shell and doctest tests. Installing a
93  
-         global ``_()`` function causes interference. Explicitly importing
94  
-         ``ugettext()`` as ``_()`` avoids this problem.
95  
-
96  
-.. highlightlang:: python
97  
-
98  
-In this example, the text ``"Welcome to my site."`` is marked as a translation
99  
-string::
100  
-
101  
-    from django.utils.translation import ugettext as _
102  
-
103  
-    def my_view(request):
104  
-        output = _("Welcome to my site.")
105  
-        return HttpResponse(output)
106  
-
107  
-Obviously, you could code this without using the alias. This example is
108  
-identical to the previous one::
109  
-
110  
-    from django.utils.translation import ugettext
111  
-
112  
-    def my_view(request):
113  
-        output = ugettext("Welcome to my site.")
114  
-        return HttpResponse(output)
115  
-
116  
-Translation works on computed values. This example is identical to the previous
117  
-two::
118  
-
119  
-    def my_view(request):
120  
-        words = ['Welcome', 'to', 'my', 'site.']
121  
-        output = _(' '.join(words))
122  
-        return HttpResponse(output)
123  
-
124  
-Translation works on variables. Again, here's an identical example::
125  
-
126  
-    def my_view(request):
127  
-        sentence = 'Welcome to my site.'
128  
-        output = _(sentence)
129  
-        return HttpResponse(output)
130  
-
131  
-(The caveat with using variables or computed values, as in the previous two
132  
-examples, is that Django's translation-string-detecting utility,
133  
-``django-admin.py makemessages``, won't be able to find these strings. More on
134  
-``makemessages`` later.)
135  
-
136  
-The strings you pass to ``_()`` or ``ugettext()`` can take placeholders,
137  
-specified with Python's standard named-string interpolation syntax. Example::
138  
-
139  
-    def my_view(request, m, d):
140  
-        output = _('Today is %(month)s, %(day)s.') % {'month': m, 'day': d}
141  
-        return HttpResponse(output)
142  
-
143  
-This technique lets language-specific translations reorder the placeholder
144  
-text. For example, an English translation may be ``"Today is November, 26."``,
145  
-while a Spanish translation may be ``"Hoy es 26 de Noviembre."`` -- with the
146  
-placeholders (the month and the day) with their positions swapped.
147  
-
148  
-For this reason, you should use named-string interpolation (e.g., ``%(day)s``)
149  
-instead of positional interpolation (e.g., ``%s`` or ``%d``) whenever you
150  
-have more than a single parameter. If you used positional interpolation,
151  
-translations wouldn't be able to reorder placeholder text.
152  
-
153  
-Marking strings as no-op
154  
-~~~~~~~~~~~~~~~~~~~~~~~~
155  
-
156  
-Use the function ``django.utils.translation.ugettext_noop()`` to mark a string
157  
-as a translation string without translating it. The string is later translated
158  
-from a variable.
159  
-
160  
-Use this if you have constant strings that should be stored in the source
161  
-language because they are exchanged over systems or users -- such as strings in
162  
-a database -- but should be translated at the last possible point in time, such
163  
-as when the string is presented to the user.
164  
-
165  
-.. _lazy-translations:
166  
-
167  
-Lazy translation
168  
-~~~~~~~~~~~~~~~~
169  
-
170  
-Use the function ``django.utils.translation.ugettext_lazy()`` to translate
171  
-strings lazily -- when the value is accessed rather than when the
172  
-``ugettext_lazy()`` function is called.
173  
-
174  
-For example, to translate a model's ``help_text``, do the following::
175  
-
176  
-    from django.utils.translation import ugettext_lazy
177  
-
178  
-    class MyThing(models.Model):
179  
-        name = models.CharField(help_text=ugettext_lazy('This is the help text'))
180  
-
181  
-In this example, ``ugettext_lazy()`` stores a lazy reference to the string --
182  
-not the actual translation. The translation itself will be done when the string
183  
-is used in a string context, such as template rendering on the Django admin
184  
-site.
185  
-
186  
-The result of a ``ugettext_lazy()`` call can be used wherever you would use a
187  
-unicode string (an object with type ``unicode``) in Python. If you try to use
188  
-it where a bytestring (a ``str`` object) is expected, things will not work as
189  
-expected, since a ``ugettext_lazy()`` object doesn't know how to convert
190  
-itself to a bytestring.  You can't use a unicode string inside a bytestring,
191  
-either, so this is consistent with normal Python behavior. For example::
192  
-
193  
-    # This is fine: putting a unicode proxy into a unicode string.
194  
-    u"Hello %s" % ugettext_lazy("people")
195  
-
196  
-    # This will not work, since you cannot insert a unicode object
197  
-    # into a bytestring (nor can you insert our unicode proxy there)
198  
-    "Hello %s" % ugettext_lazy("people")
199  
-
200  
-If you ever see output that looks like ``"hello
201  
-<django.utils.functional...>"``, you have tried to insert the result of
202  
-``ugettext_lazy()`` into a bytestring. That's a bug in your code.
203  
-
204  
-If you don't like the verbose name ``ugettext_lazy``, you can just alias it as
205  
-``_`` (underscore), like so::
206  
-
207  
-    from django.utils.translation import ugettext_lazy as _
208  
-
209  
-    class MyThing(models.Model):
210  
-        name = models.CharField(help_text=_('This is the help text'))
211  
-
212  
-Always use lazy translations in :ref:`Django models <topics-db-models>`.
213  
-Field names and table names should be marked for translation (otherwise, they
214  
-won't be translated in the admin interface). This means writing explicit
215  
-``verbose_name`` and ``verbose_name_plural`` options in the ``Meta`` class,
216  
-though, rather than relying on Django's default determination of
217  
-``verbose_name`` and ``verbose_name_plural`` by looking at the model's class
218  
-name::
219  
-
220  
-    from django.utils.translation import ugettext_lazy as _
221  
-
222  
-    class MyThing(models.Model):
223  
-        name = models.CharField(_('name'), help_text=_('This is the help text'))
224  
-        class Meta:
225  
-            verbose_name = _('my thing')
226  
-            verbose_name_plural = _('mythings')
227  
-
228  
-Pluralization
229  
-~~~~~~~~~~~~~
230  
-
231  
-Use the function ``django.utils.translation.ungettext()`` to specify pluralized
232  
-messages.
233  
-
234  
-``ungettext`` takes three arguments: the singular translation string, the
235  
-plural translation string and the number of objects.
236  
-
237  
-This function is useful when you need your Django application to be localizable
238  
-to languages where the number and complexity of `plural forms
239  
-<http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms>`_ is
240  
-greater than the two forms used in English ('object' for the singular and
241  
-'objects' for all the cases where ``count`` is different from zero,
242  
-irrespective of its value).
243  
-
244  
-For example::
245  
-
246  
-    from django.utils.translation import ungettext
247  
-    def hello_world(request, count):
248  
-        page = ungettext('there is %(count)d object', 'there are %(count)d objects', count) % {
249  
-            'count': count,
250  
-        }
251  
-        return HttpResponse(page)
252  
-
253  
-In this example the number of objects is passed to the translation languages as
254  
-the ``count`` variable.
255  
-
256  
-Lets see a slightly more complex usage example::
257  
-
258  
-    from django.utils.translation import ungettext
259  
-
260  
-    count = Report.objects.count()
261  
-    if count == 1:
262  
-        name = Report._meta.verbose_name
263  
-    else:
264  
-        name = Report._meta.verbose_name_plural
265  
-
266  
-    text = ungettext(
267  
-            'There is %(count)d %(name)s available.',
268  
-            'There are %(count)d %(name)s available.',
269  
-            count
270  
-    ) % {
271  
-        'count': count,
272  
-        'name': name
273  
-    }
274  
-
275  
-Here we reuse localizable, hopefully already translated literals (contained in
276  
-the ``verbose_name`` and ``verbose_name_plural`` model ``Meta`` options) for
277  
-other parts of the sentence so all of it is consistently based on the
278  
-cardinality of the elements at play.
279  
-
280  
-.. _pluralization-var-notes:
281  
-
282  
-.. note::
283  
-
284  
-    When using this technique, make sure you use a single name for every
285  
-    extrapolated variable included in the literal. In the example above note
286  
-    how we used the ``name`` Python variable in both translation strings. This
287  
-    example would fail::
288  
-
289  
-        from django.utils.translation import ungettext
290  
-        from myapp.models import Report
291  
-
292  
-        count = Report.objects.count()
293  
-        d = {
294  
-            'count': count,
295  
-            'name': Report._meta.verbose_name
296  
-            'plural_name': Report._meta.verbose_name_plural
297  
-        }
298  
-        text = ungettext(
299  
-                'There is %(count)d %(name)s available.',
300  
-                'There are %(count)d %(plural_name)s available.',
301  
-                count
302  
-        ) % d
303  
-
304  
-    You would get a ``a format specification for argument 'name', as in
305  
-    'msgstr[0]', doesn't exist in 'msgid'`` error when running
306  
-    ``django-admin.py compilemessages`` or a ``KeyError`` Python exception at
307  
-    runtime.
308  
-
309  
-In template code
310  
-----------------
311  
-
312  
-.. highlightlang:: html+django
313  
-
314  
-Translations in :ref:`Django templates <topics-templates>` uses two template
315  
-tags and a slightly different syntax than in Python code. To give your template
316  
-access to these tags, put ``{% load i18n %}`` toward the top of your template.
317  
-
318  
-The ``{% trans %}`` template tag translates either a constant string
319  
-(enclosed in single or double quotes) or variable content::
320  
-
321  
-    <title>{% trans "This is the title." %}</title>
322  
-    <title>{% trans myvar %}</title>
323  
-
324  
-If the ``noop`` option is present, variable lookup still takes place, but the
325  
-original text will be returned unchanged. This is useful when "stubbing out"
326  
-content that will require translation in the future::
327  
-
328  
-    <title>{% trans "myvar" noop %}</title>
329  
-
330  
-Internally, inline translations use an ``ugettext`` call.
331  
-
332  
-It's not possible to mix a template variable inside a string within ``{% trans
333  
-%}``. If your translations require strings with variables (placeholders), use
334  
-``{% blocktrans %}``::
335  
-
336  
-    {% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %}
337  
-
338  
-To translate a template expression -- say, using template filters -- you need
339  
-to bind the expression to a local variable for use within the translation
340  
-block::
341  
-
342  
-    {% blocktrans with value|filter as myvar %}
343  
-    This will have {{ myvar }} inside.
344  
-    {% endblocktrans %}
345  
-
346  
-If you need to bind more than one expression inside a ``blocktrans`` tag,
347  
-separate the pieces with ``and``::
348  
-
349  
-    {% blocktrans with book|title as book_t and author|title as author_t %}
350  
-    This is {{ book_t }} by {{ author_t }}
351  
-    {% endblocktrans %}
352  
-
353  
-To pluralize, specify both the singular and plural forms with the
354  
-``{% plural %}`` tag, which appears within ``{% blocktrans %}`` and
355  
-``{% endblocktrans %}``. Example::
356  
-
357  
-    {% blocktrans count list|length as counter %}
358  
-    There is only one {{ name }} object.
359  
-    {% plural %}
360  
-    There are {{ counter }} {{ name }} objects.
361  
-    {% endblocktrans %}
362  
-
363  
-When you use the pluralization feature and bind additional values to local
364  
-variables apart from the counter value that selects the translated literal to
365  
-be used, have in mind that the ``blocktrans`` construct is internally converted
366  
-to an ``ungettext`` call. This means the same :ref:`notes regarding ungettext
367  
-variables <pluralization-var-notes>` apply.
368  
-
369  
-Each ``RequestContext`` has access to three translation-specific variables:
370  
-
371  
-    * ``LANGUAGES`` is a list of tuples in which the first element is the
372  
-      language code and the second is the language name (translated into the
373  
-      currently active locale).
374  
-
375  
-    * ``LANGUAGE_CODE`` is the current user's preferred language, as a string.
376  
-      Example: ``en-us``. (See :ref:`how-django-discovers-language-preference`,
377  
-      below.)
378  
-
379  
-    * ``LANGUAGE_BIDI`` is the current locale's direction. If True, it's a
380  
-      right-to-left language, e.g.: Hebrew, Arabic. If False it's a
381  
-      left-to-right language, e.g.: English, French, German etc.
382  
-
383  
-
384  
-If you don't use the ``RequestContext`` extension, you can get those values
385  
-with three tags::
386  
-
387  
-    {% get_current_language as LANGUAGE_CODE %}
388  
-    {% get_available_languages as LANGUAGES %}
389  
-    {% get_current_language_bidi as LANGUAGE_BIDI %}
390  
-
391  
-These tags also require a ``{% load i18n %}``.
392  
-
393  
-Translation hooks are also available within any template block tag that accepts
394  
-constant strings. In those cases, just use ``_()`` syntax to specify a
395  
-translation string::
396  
-
397  
-    {% some_special_tag _("Page not found") value|yesno:_("yes,no") %}
398  
-
399  
-In this case, both the tag and the filter will see the already-translated
400  
-string, so they don't need to be aware of translations.
401  
-
402  
-.. note::
403  
-    In this example, the translation infrastructure will be passed the string
404  
-    ``"yes,no"``, not the individual strings ``"yes"`` and ``"no"``. The
405  
-    translated string will need to contain the comma so that the filter
406  
-    parsing code knows how to split up the arguments. For example, a German
407  
-    translator might translate the string ``"yes,no"`` as ``"ja,nein"``
408  
-    (keeping the comma intact).
409  
-
410  
-.. _Django templates: ../templates_python/
411  
-
412  
-Working with lazy translation objects
413  
--------------------------------------
414  
-
415  
-.. highlightlang:: python
416  
-
417  
-Using ``ugettext_lazy()`` and ``ungettext_lazy()`` to mark strings in models
418  
-and utility functions is a common operation. When you're working with these
419  
-objects elsewhere in your code, you should ensure that you don't accidentally
420  
-convert them to strings, because they should be converted as late as possible
421  
-(so that the correct locale is in effect). This necessitates the use of a
422  
-couple of helper functions.
423  
-
424  
-Joining strings: string_concat()
425  
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
426  
-
427  
-Standard Python string joins (``''.join([...])``) will not work on lists
428  
-containing lazy translation objects. Instead, you can use
429  
-``django.utils.translation.string_concat()``, which creates a lazy object that
430  
-concatenates its contents *and* converts them to strings only when the result
431  
-is included in a string. For example::
432  
-
433  
-    from django.utils.translation import string_concat
434  
-    ...
435  
-    name = ugettext_lazy(u'John Lennon')
436  
-    instrument = ugettext_lazy(u'guitar')
437  
-    result = string_concat([name, ': ', instrument])
438  
-
439  
-In this case, the lazy translations in ``result`` will only be converted to
440  
-strings when ``result`` itself is used in a string (usually at template
441  
-rendering time).
442  
-
443  
-The allow_lazy() decorator
444  
-~~~~~~~~~~~~~~~~~~~~~~~~~~
445  
-
446  
-Django offers many utility functions (particularly in ``django.utils``) that
447  
-take a string as their first argument and do something to that string. These
448  
-functions are used by template filters as well as directly in other code.
449  
-
450  
-If you write your own similar functions and deal with translations, you'll
451  
-face the problem of what to do when the first argument is a lazy translation
452  
-object. You don't want to convert it to a string immediately, because you might
453  
-be using this function outside of a view (and hence the current thread's locale
454  
-setting will not be correct).
455  
-
456  
-For cases like this, use the ``django.utils.functional.allow_lazy()``
457  
-decorator. It modifies the function so that *if* it's called with a lazy
458  
-translation as the first argument, the function evaluation is delayed until it
459  
-needs to be converted to a string.
460  
-
461  
-For example::
462  
-
463  
-    from django.utils.functional import allow_lazy
464  
-
465  
-    def fancy_utility_function(s, ...):
466  
-        # Do some conversion on string 's'
467  
-        ...
468  
-    fancy_utility_function = allow_lazy(fancy_utility_function, unicode)
469  
-
470  
-The ``allow_lazy()`` decorator takes, in addition to the function to decorate,
471  
-a number of extra arguments (``*args``) specifying the type(s) that the
472  
-original function can return. Usually, it's enough to include ``unicode`` here
473  
-and ensure that your function returns only Unicode strings.
474  
-
475  
-Using this decorator means you can write your function and assume that the
476  
-input is a proper string, then add support for lazy translation objects at the
477  
-end.
478  
-
479  
-.. _how-to-create-language-files:
480  
-
481  
-2. How to create language files
482  
-===============================
483  
-
484  
-Once you've tagged your strings for later translation, you need to write (or
485  
-obtain) the language translations themselves. Here's how that works.
486  
-
487  
-.. admonition:: Locale restrictions
488  
-
489  
-    Django does not support localizing your application into a locale for
490  
-    which Django itself has not been translated. In this case, it will ignore
491  
-    your translation files. If you were to try this and Django supported it,
492  
-    you would inevitably see a mixture of translated strings (from your
493  
-    application) and English strings (from Django itself). If you want to
494  
-    support a locale for your application that is not already part of
495  
-    Django, you'll need to make at least a minimal translation of the Django
496  
-    core. See the relevant :ref:`LocaleMiddleware note<locale-middleware-notes>`
497  
-    for more details.
498  
-
499  
-Message files
500  
--------------
501  
-
502  
-The first step is to create a **message file** for a new language. A message
503  
-file is a plain-text file, representing a single language, that contains all
504  
-available translation strings and how they should be represented in the given
505  
-language. Message files have a ``.po`` file extension.
506  
-
507  
-Django comes with a tool, ``django-admin.py makemessages``, that automates the
508  
-creation and upkeep of these files.
509  
-
510  
-.. admonition:: A note to Django veterans
511  
-
512  
-    The old tool ``bin/make-messages.py`` has been moved to the command
513  
-    ``django-admin.py makemessages`` to provide consistency throughout Django.
514  
-
515  
-.. admonition:: Gettext utilities
516  
-
517  
-    The ``makemessages`` command (and ``compilemessages`` discussed later) use
518  
-    commands from the GNU gettext toolset: ``xgetetxt``, ``msgfmt``,
519  
-    ``msgmerge`` and ``msguniq``.
520  
-
521  
-    .. versionchanged:: 1.2
522  
-
523  
-    The minimum version of the ``gettext`` utilities supported is 0.15.
524  
-
525  
-To create or update a message file, run this command::
526  
-
527  
-    django-admin.py makemessages -l de
528  
-
529  
-...where ``de`` is the language code for the message file you want to create.
530  
-The language code, in this case, is in locale format. For example, it's
531  
-``pt_BR`` for Brazilian Portuguese and ``de_AT`` for Austrian German.
532  
-
533  
-The script should be run from one of three places:
534  
-
535  
-    * The root directory of your Django project.
536  
-    * The root directory of your Django app.
537  
-    * The root ``django`` directory (not a Subversion checkout, but the one
538  
-      that is linked-to via ``$PYTHONPATH`` or is located somewhere on that
539  
-      path). This is only relevant when you are creating a translation for
540  
-      Django itself, see :ref:`contributing-translations`.
541  
-
542  
-The script runs over your project source tree or your application source tree
543  
-and pulls out all strings marked for translation. It creates (or updates) a
544  
-message file in the directory ``locale/LANG/LC_MESSAGES``. In the ``de``
545  
-example, the file will be ``locale/de/LC_MESSAGES/django.po``.
546  
-
547  
-By default ``django-admin.py makemessages`` examines every file that has the
548  
-``.html`` file extension. In case you want to override that default, use the
549  
-``--extension`` or ``-e`` option to specify the file extensions to examine::
550  
-
551  
-    django-admin.py makemessages -l de -e txt
552  
-
553  
-Separate multiple extensions with commas and/or use ``-e`` or ``--extension``
554  
-multiple times::
555  
-
556  
-    django-admin.py makemessages -l=de -e=html,txt -e xml
557  
-
558  
-When `creating JavaScript translation catalogs`_ you need to use the special
559  
-'djangojs' domain, **not** ``-e js``.
560  
-
561  
-.. admonition:: No gettext?
562  
-
563  
-    If you don't have the ``gettext`` utilities installed, ``django-admin.py
564  
-    makemessages`` will create empty files. If that's the case, either install
565  
-    the ``gettext`` utilities or just copy the English message file
566  
-    (``locale/en/LC_MESSAGES/django.po``) if available and use it as a starting
567  
-    point; it's just an empty translation file.
568  
-
569  
-.. admonition:: Working on Windows?
570  
-
571  
-   If you're using Windows and need to install the GNU gettext utilities so
572  
-   ``django-admin makemessages`` works see `gettext on Windows`_ for more
573  
-   information.
574  
-
575  
-The format of ``.po`` files is straightforward. Each ``.po`` file contains a
576  
-small bit of metadata, such as the translation maintainer's contact
577  
-information, but the bulk of the file is a list of **messages** -- simple
578  
-mappings between translation strings and the actual translated text for the
579  
-particular language.
580  
-
581  
-For example, if your Django app contained a translation string for the text
582  
-``"Welcome to my site."``, like so::
583  
-
584  
-    _("Welcome to my site.")
585  
-
586  
-...then ``django-admin.py makemessages`` will have created a ``.po`` file
587  
-containing the following snippet -- a message::
588  
-
589  
-    #: path/to/python/module.py:23
590  
-    msgid "Welcome to my site."
591  
-    msgstr ""
592  
-
593  
-A quick explanation:
594  
-
595  
-    * ``msgid`` is the translation string, which appears in the source. Don't
596  
-      change it.
597  
-    * ``msgstr`` is where you put the language-specific translation. It starts
598  
-      out empty, so it's your responsibility to change it. Make sure you keep
599  
-      the quotes around your translation.
600  
-    * As a convenience, each message includes, in the form of a comment line
601  
-      prefixed with ``#`` and located above the ``msgid`` line, the filename
602  
-      and line number from which the translation string was gleaned.
603  
-
604  
-Long messages are a special case. There, the first string directly after the
605  
-``msgstr`` (or ``msgid``) is an empty string. Then the content itself will be
606  
-written over the next few lines as one string per line. Those strings are
607  
-directly concatenated. Don't forget trailing spaces within the strings;
608  
-otherwise, they'll be tacked together without whitespace!
609  
-
610  
-.. admonition:: Mind your charset
611  
-
612  
-    When creating a PO file with your favorite text editor, first edit
613  
-    the charset line (search for ``"CHARSET"``) and set it to the charset
614  
-    you'll be using to edit the content. Due to the way the ``gettext`` tools
615  
-    work internally and because we want to allow non-ASCII source strings in
616  
-    Django's core and your applications, you **must** use UTF-8 as the encoding
617  
-    for your PO file. This means that everybody will be using the same
618  
-    encoding, which is important when Django processes the PO files.
619  
-
620  
-To reexamine all source code and templates for new translation strings and
621  
-update all message files for **all** languages, run this::
622  
-
623  
-    django-admin.py makemessages -a
624  
-
625  
-Compiling message files
626  
------------------------
627  
-
628  
-After you create your message file -- and each time you make changes to it --
629  
-you'll need to compile it into a more efficient form, for use by ``gettext``.
630  
-Do this with the ``django-admin.py compilemessages`` utility.
631  
-
632  
-This tool runs over all available ``.po`` files and creates ``.mo`` files,
633  
-which are binary files optimized for use by ``gettext``. In the same directory
634  
-from which you ran ``django-admin.py makemessages``, run ``django-admin.py
635  
-compilemessages`` like this::
636  
-
637  
-   django-admin.py compilemessages
638  
-
639  
-That's it. Your translations are ready for use.
640  
-
641  
-.. admonition:: A note to Django veterans
642  
-
643  
-    The old tool ``bin/compile-messages.py`` has been moved to the command
644  
-    ``django-admin.py compilemessages`` to provide consistency throughout
645  
-    Django.
646  
-
647  
-.. admonition:: Working on Windows?
648  
-
649  
-   If you're using Windows and need to install the GNU gettext utilities so
650  
-   ``django-admin compilemessages`` works see `gettext on Windows`_ for more
651  
-   information.
652  
-
653  
-.. _how-django-discovers-language-preference:
654  
-
655  
-3. How Django discovers language preference
656  
-===========================================
657  
-
658  
-Once you've prepared your translations -- or, if you just want to use the
659  
-translations that come with Django -- you'll just need to activate translation
660  
-for your app.
661  
-
662  
-Behind the scenes, Django has a very flexible model of deciding which language
663  
-should be used -- installation-wide, for a particular user, or both.
664  
-
665  
-To set an installation-wide language preference, set :setting:`LANGUAGE_CODE`.
666  
-Django uses this language as the default translation -- the final attempt if no
667  
-other translator finds a translation.
668  
-
669  
-If all you want to do is run Django with your native language, and a language
670  
-file is available for your language, all you need to do is set
671  
-``LANGUAGE_CODE``.
672  
-
673  
-If you want to let each individual user specify which language he or she
674  
-prefers, use ``LocaleMiddleware``. ``LocaleMiddleware`` enables language
675  
-selection based on data from the request. It customizes content for each user.
676  
-
677  
-To use ``LocaleMiddleware``, add ``'django.middleware.locale.LocaleMiddleware'``
678  
-to your ``MIDDLEWARE_CLASSES`` setting. Because middleware order matters, you
679  
-should follow these guidelines:
680  
-
681  
-    * Make sure it's one of the first middlewares installed.
682  
-    * It should come after ``SessionMiddleware``, because ``LocaleMiddleware``
683  
-      makes use of session data.
684  
-    * If you use ``CacheMiddleware``, put ``LocaleMiddleware`` after it.
685  
-
686  
-For example, your ``MIDDLEWARE_CLASSES`` might look like this::
687  
-
688  
-    MIDDLEWARE_CLASSES = (
689  
-       'django.contrib.sessions.middleware.SessionMiddleware',
690  
-       'django.middleware.locale.LocaleMiddleware',
691  
-       'django.middleware.common.CommonMiddleware',
692  
-    )
693  
-
694  
-(For more on middleware, see the :ref:`middleware documentation
695  
-<topics-http-middleware>`.)
696  
-
697  
-``LocaleMiddleware`` tries to determine the user's language preference by
698  
-following this algorithm:
699  
-
700  
-    * First, it looks for a ``django_language`` key in the current user's
701  
-      session.
702  
-
703  
-    * Failing that, it looks for a cookie.
704  
-
705  
-      .. versionchanged:: 1.0
706  
-
707  
-      In Django version 0.96 and before, the cookie's name is hard-coded to
708  
-      ``django_language``. In Django 1,0, The cookie name is set by the
709  
-      ``LANGUAGE_COOKIE_NAME`` setting. (The default name is
710  
-      ``django_language``.)
711  
-
712  
-    * Failing that, it looks at the ``Accept-Language`` HTTP header. This
713  
-      header is sent by your browser and tells the server which language(s) you
714  
-      prefer, in order by priority. Django tries each language in the header
715  
-      until it finds one with available translations.
716  
-
717  
-    * Failing that, it uses the global ``LANGUAGE_CODE`` setting.
718  
-
719  
-.. _locale-middleware-notes:
720  
-
721  
-Notes:
722  
-
723  
-    * In each of these places, the language preference is expected to be in the
724  
-      standard language format, as a string. For example, Brazilian Portuguese
725  
-      is ``pt-br``.
726  
-
727  
-    * If a base language is available but the sublanguage specified is not,
728  
-      Django uses the base language. For example, if a user specifies ``de-at``
729  
-      (Austrian German) but Django only has ``de`` available, Django uses
730  
-      ``de``.
731  
-
732  
-    * Only languages listed in the :setting:`LANGUAGES` setting can be selected.
733  
-      If you want to restrict the language selection to a subset of provided
734  
-      languages (because your application doesn't provide all those languages),
735  
-      set ``LANGUAGES`` to a list of languages. For example::
736  
-
737  
-          LANGUAGES = (
738  
-            ('de', _('German')),
739  
-            ('en', _('English')),
740  
-          )
741  
-
742  
-      This example restricts languages that are available for automatic
743  
-      selection to German and English (and any sublanguage, like de-ch or
744  
-      en-us).
745  
-
746  
-      .. _LANGUAGES setting: ../settings/#languages
747  
-
748  
-    * If you define a custom ``LANGUAGES`` setting, as explained in the
749  
-      previous bullet, it's OK to mark the languages as translation strings
750  
-      -- but use a "dummy" ``ugettext()`` function, not the one in
751  
-      ``django.utils.translation``. You should *never* import
752  
-      ``django.utils.translation`` from within your settings file, because that
753  
-      module in itself depends on the settings, and that would cause a circular
754  
-      import.
755  
-
756  
-      The solution is to use a "dummy" ``ugettext()`` function. Here's a sample
757  
-      settings file::
758  
-
759  
-          ugettext = lambda s: s
760  
-
761  
-          LANGUAGES = (
762  
-              ('de', ugettext('German')),
763  
-              ('en', ugettext('English')),
764  
-          )
765  
-
766  
-      With this arrangement, ``django-admin.py makemessages`` will still find
767  
-      and mark these strings for translation, but the translation won't happen
768  
-      at runtime -- so you'll have to remember to wrap the languages in the
769  
-      *real* ``ugettext()`` in any code that uses ``LANGUAGES`` at runtime.
770  
-
771  
-    * The ``LocaleMiddleware`` can only select languages for which there is a
772  
-      Django-provided base translation. If you want to provide translations
773  
-      for your application that aren't already in the set of translations
774  
-      in Django's source tree, you'll want to provide at least basic
775  
-      translations for that language. For example, Django uses technical
776  
-      message IDs to translate date formats and time formats -- so you will
777  
-      need at least those translations for the system to work correctly.
778  
-
779  
-      A good starting point is to copy the English ``.po`` file and to
780  
-      translate at least the technical messages -- maybe the validation
781  
-      messages, too.
782  
-
783  
-      Technical message IDs are easily recognized; they're all upper case. You
784  
-      don't translate the message ID as with other messages, you provide the
785  
-      correct local variant on the provided English value. For example, with
786  
-      ``DATETIME_FORMAT`` (or ``DATE_FORMAT`` or ``TIME_FORMAT``), this would
787  
-      be the format string that you want to use in your language. The format
788  
-      is identical to the format strings used by the ``now`` template tag.
789  
-
790  
-Once ``LocaleMiddleware`` determines the user's preference, it makes this
791  
-preference available as ``request.LANGUAGE_CODE`` for each
792  
-:class:`~django.http.HttpRequest`. Feel free to read this value in your view
793  
-code. Here's a simple example::
794  
-
795  
-    def hello_world(request, count):
796  
-        if request.LANGUAGE_CODE == 'de-at':
797  
-            return HttpResponse("You prefer to read Austrian German.")
798  
-        else:
799  
-            return HttpResponse("You prefer to read another language.")
800  
-
801  
-Note that, with static (middleware-less) translation, the language is in
802  
-``settings.LANGUAGE_CODE``, while with dynamic (middleware) translation, it's
803  
-in ``request.LANGUAGE_CODE``.
804  
-
805  
-.. _settings file: ../settings/
806  
-.. _middleware documentation: ../middleware/
807  
-.. _session: ../sessions/
808  
-.. _request object: ../request_response/#httprequest-objects
809  
-
810  
-.. _translations-in-your-own-projects:
811  
-
812  
-Using translations in your own projects
813  
-=======================================
814  
-
815  
-Django looks for translations by following this algorithm:
816  
-
817  
-    * First, it looks for a ``locale`` directory in the application directory
818  
-      of the view that's being called. If it finds a translation for the
819  
-      selected language, the translation will be installed.
820  
-    * Next, it looks for a ``locale`` directory in the project directory. If it
821  
-      finds a translation, the translation will be installed.
822  
-    * Finally, it checks the Django-provided base translation in
823  
-      ``django/conf/locale``.
824  
-
825  
-This way, you can write applications that include their own translations, and
826  
-you can override base translations in your project path. Or, you can just build
827  
-a big project out of several apps and put all translations into one big project
828  
-message file. The choice is yours.
829  
-
830  
-.. note::
831  
-
832  
-    If you're using manually configured settings, as described
833  
-    :ref:`settings-without-django-settings-module`, the ``locale`` directory in
834  
-    the project directory will not be examined, since Django loses the ability
835  
-    to work out the location of the project directory. (Django normally uses
836  
-    the location of the settings file to determine this, and a settings file
837  
-    doesn't exist if you're manually configuring your settings.)
838  
-
839  
-All message file repositories are structured the same way. They are:
840  
-
841  
-    * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
842  
-    * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
843  
-    * All paths listed in ``LOCALE_PATHS`` in your settings file are
844  
-      searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)``
845  
-    * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)``
846  
-
847  
-To create message files, you use the same ``django-admin.py makemessages``
848  
-tool as with the Django message files. You only need to be in the right place
849  
--- in the directory where either the ``conf/locale`` (in case of the source
850  
-tree) or the ``locale/`` (in case of app messages or project messages)
851  
-directory are located. And you use the same ``django-admin.py
852  
-compilemessages`` to produce the binary ``django.mo`` files that are used by
853  
-``gettext``.
854  
-
855  
-You can also run ``django-admin.py compilemessages
856  
---settings=path.to.settings`` to make the compiler process all the directories
857  
-in your ``LOCALE_PATHS`` setting.
858  
-
859  
-Application message files are a bit complicated to discover -- they need the
860  
-``LocaleMiddleware``. If you don't use the middleware, only the Django message
861  
-files and project message files will be processed.
862  
-
863  
-Finally, you should give some thought to the structure of your translation
864  
-files. If your applications need to be delivered to other users and will
865  
-be used in other projects, you might want to use app-specific translations.
866  
-But using app-specific translations and project translations could produce
867  
-weird problems with ``makemessages``: ``makemessages`` will traverse all
868  
-directories below the current path and so might put message IDs into the
869  
-project message file that are already in application message files.
870  
-
871  
-The easiest way out is to store applications that are not part of the project
872  
-(and so carry their own translations) outside the project tree. That way,
873  
-``django-admin.py makemessages`` on the project level will only translate
874  
-strings that are connected to your explicit project and not strings that are
875  
-distributed independently.
876  
-
877  
-The ``set_language`` redirect view
878  
-==================================
879  
-
880  
-As a convenience, Django comes with a view, ``django.views.i18n.set_language``,
881  
-that sets a user's language preference and redirects back to the previous page.
882  
-
883  
-Activate this view by adding the following line to your URLconf::
884  
-
885  
-    (r'^i18n/', include('django.conf.urls.i18n')),
886  
-
887  
-(Note that this example makes the view available at ``/i18n/setlang/``.)
888  
-
889  
-The view expects to be called via the ``POST`` method, with a ``language``
890  
-parameter set in request. If session support is enabled, the view
891  
-saves the language choice in the user's session. Otherwise, it saves the
892  
-language choice in a cookie that is by default named ``django_language``.
893  
-(The name can be changed through the ``LANGUAGE_COOKIE_NAME`` setting.)
894  
-
895  
-After setting the language choice, Django redirects the user, following this
896  
-algorithm:
897  
-
898  
-    * Django looks for a ``next`` parameter in the ``POST`` data.
899  
-    * If that doesn't exist, or is empty, Django tries the URL in the
900  
-      ``Referrer`` header.
901  
-    * If that's empty -- say, if a user's browser suppresses that header --
902  
-      then the user will be redirected to ``/`` (the site root) as a fallback.
903  
-
904  
-Here's example HTML template code:
905  
-
906  
-.. code-block:: html+django
907  
-
908  
-    <form action="/i18n/setlang/" method="post">
909  
-    <input name="next" type="hidden" value="/next/page/" />
910  
-    <select name="language">
911  
-    {% for lang in LANGUAGES %}
912  
-    <option value="{{ lang.0 }}">{{ lang.1 }}</option>
913  
-    {% endfor %}
914  
-    </select>
915  
-    <input type="submit" value="Go" />
916  
-    </form>
917  
-
918  
-Translations and JavaScript
919  
-===========================
920  
-
921  
-Adding translations to JavaScript poses some problems:
922  
-
923  
-    * JavaScript code doesn't have access to a ``gettext`` implementation.
924  
-
925  
-    * JavaScript code doesn't have access to .po or .mo files; they need to be
926  
-      delivered by the server.
927  
-
928  
-    * The translation catalogs for JavaScript should be kept as small as
929  
-      possible.
930  
-
931  
-Django provides an integrated solution for these problems: It passes the
932  
-translations into JavaScript, so you can call ``gettext``, etc., from within
933  
-JavaScript.
934  
-
935  
-The ``javascript_catalog`` view
936  
--------------------------------
937  
-
938  
-The main solution to these problems is the ``javascript_catalog`` view, which
939  
-sends out a JavaScript code library with functions that mimic the ``gettext``
940  
-interface, plus an array of translation strings. Those translation strings are
941  
-taken from the application, project or Django core, according to what you
942  
-specify in either the info_dict or the URL.
943  
-
944  
-You hook it up like this::
945  
-
946  
-    js_info_dict = {
947  
-        'packages': ('your.app.package',),
948  
-    }
949  
-
950  
-    urlpatterns = patterns('',
951  
-        (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
952  
-    )
953  
-
954  
-Each string in ``packages`` should be in Python dotted-package syntax (the
955  
-same format as the strings in ``INSTALLED_APPS``) and should refer to a package
956  
-that contains a ``locale`` directory. If you specify multiple packages, all
957  
-those catalogs are merged into one catalog. This is useful if you have
958  
-JavaScript that uses strings from different applications.
959  
-
960  
-You can make the view dynamic by putting the packages into the URL pattern::
961  
-
962  
-    urlpatterns = patterns('',
963  
-        (r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
964  
-    )
965  
-
966  
-With this, you specify the packages as a list of package names delimited by '+'
967  
-signs in the URL. This is especially useful if your pages use code from
968  
-different apps and this changes often and you don't want to pull in one big