1
.. _topics-i18n-deployment:
3
==========================
4
Deployment of translations
5
==========================
7
If you don't need internationalization
8
======================================
10
Django's internationalization hooks are on by default, and that means there's a
11
bit of i18n-related overhead in certain places of the framework. If you don't
12
use internationalization, you should take the two seconds to set
13
:setting:`USE_I18N = False <USE_I18N>` in your settings file. If
14
:setting:`USE_I18N` is set to ``False``, then Django will make some
15
optimizations so as not to load the internationalization machinery.
17
You'll probably also want to remove ``'django.core.context_processors.i18n'``
18
from your ``TEMPLATE_CONTEXT_PROCESSORS`` setting.
22
There is also an independent but related :setting:`USE_L10N` setting that
23
controls if Django should implement format localization.
25
If :setting:`USE_L10N` is set to ``True``, Django will handle numbers times,
26
and dates in the format of the current locale. That includes representation
27
of these field types on templates and allowed input formats for dates,
30
See :ref:`format-localization` for more details.
32
If you do need internationalization
33
===================================
35
.. _how-django-discovers-language-preference:
37
How Django discovers language preference
38
----------------------------------------
40
Once you've prepared your translations -- or, if you just want to use the
41
translations that come with Django -- you'll just need to activate translation
44
Behind the scenes, Django has a very flexible model of deciding which language
45
should be used -- installation-wide, for a particular user, or both.
47
To set an installation-wide language preference, set :setting:`LANGUAGE_CODE`.
48
Django uses this language as the default translation -- the final attempt if no
49
other translator finds a translation.
51
If all you want to do is run Django with your native language, and a language
52
file is available for it, all you need to do is set ``LANGUAGE_CODE``.
54
If you want to let each individual user specify which language he or she
55
prefers, use ``LocaleMiddleware``. ``LocaleMiddleware`` enables language
56
selection based on data from the request. It customizes content for each user.
58
To use ``LocaleMiddleware``, add ``'django.middleware.locale.LocaleMiddleware'``
59
to your ``MIDDLEWARE_CLASSES`` setting. Because middleware order matters, you
60
should follow these guidelines:
62
* Make sure it's one of the first middlewares installed.
63
* It should come after ``SessionMiddleware``, because ``LocaleMiddleware``
64
makes use of session data.
65
* If you use ``CacheMiddleware``, put ``LocaleMiddleware`` after it.
67
For example, your ``MIDDLEWARE_CLASSES`` might look like this::
69
MIDDLEWARE_CLASSES = (
70
'django.contrib.sessions.middleware.SessionMiddleware',
71
'django.middleware.locale.LocaleMiddleware',
72
'django.middleware.common.CommonMiddleware',
75
(For more on middleware, see the :ref:`middleware documentation
76
<topics-http-middleware>`.)
78
``LocaleMiddleware`` tries to determine the user's language preference by
79
following this algorithm:
81
* First, it looks for a ``django_language`` key in the current user's
84
* Failing that, it looks for a cookie.
86
.. versionchanged:: 1.0
88
In Django version 0.96 and before, the cookie's name is hard-coded to
89
``django_language``. In Django 1,0, The cookie name is set by the
90
``LANGUAGE_COOKIE_NAME`` setting. (The default name is
93
* Failing that, it looks at the ``Accept-Language`` HTTP header. This
94
header is sent by your browser and tells the server which language(s) you
95
prefer, in order by priority. Django tries each language in the header
96
until it finds one with available translations.
98
* Failing that, it uses the global ``LANGUAGE_CODE`` setting.
100
.. _locale-middleware-notes:
104
* In each of these places, the language preference is expected to be in the
105
standard :term:`language format<language code>`, as a string. For example,
106
Brazilian Portuguese is ``pt-br``.
108
* If a base language is available but the sublanguage specified is not,
109
Django uses the base language. For example, if a user specifies ``de-at``
110
(Austrian German) but Django only has ``de`` available, Django uses
113
* Only languages listed in the :setting:`LANGUAGES` setting can be selected.
114
If you want to restrict the language selection to a subset of provided
115
languages (because your application doesn't provide all those languages),
116
set ``LANGUAGES`` to a list of languages. For example::
120
('en', _('English')),
123
This example restricts languages that are available for automatic
124
selection to German and English (and any sublanguage, like de-ch or
127
* If you define a custom ``LANGUAGES`` setting, as explained in the
128
previous bullet, it's OK to mark the languages as translation strings
129
-- but use a "dummy" ``ugettext()`` function, not the one in
130
``django.utils.translation``. You should *never* import
131
``django.utils.translation`` from within your settings file, because that
132
module in itself depends on the settings, and that would cause a circular
135
The solution is to use a "dummy" ``ugettext()`` function. Here's a sample
138
ugettext = lambda s: s
141
('de', ugettext('German')),
142
('en', ugettext('English')),
145
With this arrangement, ``django-admin.py makemessages`` will still find
146
and mark these strings for translation, but the translation won't happen
147
at runtime -- so you'll have to remember to wrap the languages in the
148
*real* ``ugettext()`` in any code that uses ``LANGUAGES`` at runtime.
150
* The ``LocaleMiddleware`` can only select languages for which there is a
151
Django-provided base translation. If you want to provide translations
152
for your application that aren't already in the set of translations
153
in Django's source tree, you'll want to provide at least a basic
154
one as described in the :ref:`Locale restrictions<locale-restrictions>`
157
Once ``LocaleMiddleware`` determines the user's preference, it makes this
158
preference available as ``request.LANGUAGE_CODE`` for each
159
:class:`~django.http.HttpRequest`. Feel free to read this value in your view
160
code. Here's a simple example::
162
def hello_world(request, count):
163
if request.LANGUAGE_CODE == 'de-at':
164
return HttpResponse("You prefer to read Austrian German.")
166
return HttpResponse("You prefer to read another language.")
168
Note that, with static (middleware-less) translation, the language is in
169
``settings.LANGUAGE_CODE``, while with dynamic (middleware) translation, it's
170
in ``request.LANGUAGE_CODE``.
172
.. _settings file: ../settings/
173
.. _middleware documentation: ../middleware/
174
.. _session: ../sessions/
175
.. _request object: ../request_response/#httprequest-objects
177
How Django discovers translations
178
---------------------------------
180
As described in :ref:`using-translations-in-your-own-projects`,
181
at runtime, Django looks for translations by following this algorithm:
183
* First, it looks for a ``locale`` directory in the application directory
184
of the view that's being called. If it finds a translation for the
185
selected language, the translation will be installed.
186
* Next, it looks for a ``locale`` directory in the project directory. If it
187
finds a translation, the translation will be installed.
188
* Finally, it checks the Django-provided base translation in
189
``django/conf/locale``.
191
In all cases the name of the directory containing the translation is expected to
192
be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,