deployment.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ==========================
  2. Deployment of translations
  3. ==========================
  4. If you don't need internationalization
  5. ======================================
  6. Django's internationalization hooks are on by default, and that means there's a
  7. bit of i18n-related overhead in certain places of the framework. If you don't
  8. use internationalization, you should take the two seconds to set
  9. :setting:`USE_I18N = False <USE_I18N>` in your settings file. If
  10. :setting:`USE_I18N` is set to ``False``, then Django will make some
  11. optimizations so as not to load the internationalization machinery.
  12. You'll probably also want to remove ``'django.core.context_processors.i18n'``
  13. from your ``TEMPLATE_CONTEXT_PROCESSORS`` setting.
  14. .. note::
  15. There is also an independent but related :setting:`USE_L10N` setting that
  16. controls if Django should implement format localization.
  17. If :setting:`USE_L10N` is set to ``True``, Django will handle numbers times,
  18. and dates in the format of the current locale. That includes representation
  19. of these field types on templates and allowed input formats for dates,
  20. times on model forms.
  21. See :ref:`format-localization` for more details.
  22. If you do need internationalization
  23. ===================================
  24. .. _how-django-discovers-language-preference:
  25. How Django discovers language preference
  26. ----------------------------------------
  27. Once you've prepared your translations -- or, if you just want to use the
  28. translations that come with Django -- you'll just need to activate translation
  29. for your app.
  30. Behind the scenes, Django has a very flexible model of deciding which language
  31. should be used -- installation-wide, for a particular user, or both.
  32. To set an installation-wide language preference, set :setting:`LANGUAGE_CODE`.
  33. Django uses this language as the default translation -- the final attempt if no
  34. other translator finds a translation.
  35. If all you want to do is run Django with your native language, and a language
  36. file is available for it, all you need to do is set ``LANGUAGE_CODE``.
  37. If you want to let each individual user specify which language he or she
  38. prefers, use ``LocaleMiddleware``. ``LocaleMiddleware`` enables language
  39. selection based on data from the request. It customizes content for each user.
  40. To use ``LocaleMiddleware``, add ``'django.middleware.locale.LocaleMiddleware'``
  41. to your ``MIDDLEWARE_CLASSES`` setting. Because middleware order matters, you
  42. should follow these guidelines:
  43. * Make sure it's one of the first middlewares installed.
  44. * It should come after ``SessionMiddleware``, because ``LocaleMiddleware``
  45. makes use of session data.
  46. * If you use ``CacheMiddleware``, put ``LocaleMiddleware`` after it.
  47. For example, your ``MIDDLEWARE_CLASSES`` might look like this::
  48. MIDDLEWARE_CLASSES = (
  49. 'django.contrib.sessions.middleware.SessionMiddleware',
  50. 'django.middleware.locale.LocaleMiddleware',
  51. 'django.middleware.common.CommonMiddleware',
  52. )
  53. (For more on middleware, see the :doc:`middleware documentation
  54. </topics/http/middleware>`.)
  55. ``LocaleMiddleware`` tries to determine the user's language preference by
  56. following this algorithm:
  57. * First, it looks for a ``django_language`` key in the current user's
  58. session.
  59. * Failing that, it looks for a cookie.
  60. The name of the cookie used is set by the ``LANGUAGE_COOKIE_NAME``
  61. setting. (The default name is ``django_language``.)
  62. * Failing that, it looks at the ``Accept-Language`` HTTP header. This
  63. header is sent by your browser and tells the server which language(s) you
  64. prefer, in order by priority. Django tries each language in the header
  65. until it finds one with available translations.
  66. * Failing that, it uses the global ``LANGUAGE_CODE`` setting.
  67. .. _locale-middleware-notes:
  68. Notes:
  69. * In each of these places, the language preference is expected to be in the
  70. standard :term:`language format<language code>`, as a string. For example,
  71. Brazilian Portuguese is ``pt-br``.
  72. * If a base language is available but the sublanguage specified is not,
  73. Django uses the base language. For example, if a user specifies ``de-at``
  74. (Austrian German) but Django only has ``de`` available, Django uses
  75. ``de``.
  76. * Only languages listed in the :setting:`LANGUAGES` setting can be selected.
  77. If you want to restrict the language selection to a subset of provided
  78. languages (because your application doesn't provide all those languages),
  79. set ``LANGUAGES`` to a list of languages. For example::
  80. LANGUAGES = (
  81. ('de', _('German')),
  82. ('en', _('English')),
  83. )
  84. This example restricts languages that are available for automatic
  85. selection to German and English (and any sublanguage, like de-ch or
  86. en-us).
  87. * If you define a custom ``LANGUAGES`` setting, as explained in the
  88. previous bullet, it's OK to mark the languages as translation strings
  89. -- but use a "dummy" ``ugettext()`` function, not the one in
  90. ``django.utils.translation``. You should *never* import
  91. ``django.utils.translation`` from within your settings file, because that
  92. module in itself depends on the settings, and that would cause a circular
  93. import.
  94. The solution is to use a "dummy" ``ugettext()`` function. Here's a sample
  95. settings file::
  96. ugettext = lambda s: s
  97. LANGUAGES = (
  98. ('de', ugettext('German')),
  99. ('en', ugettext('English')),
  100. )
  101. With this arrangement, ``django-admin.py makemessages`` will still find
  102. and mark these strings for translation, but the translation won't happen
  103. at runtime -- so you'll have to remember to wrap the languages in the
  104. *real* ``ugettext()`` in any code that uses ``LANGUAGES`` at runtime.
  105. * The ``LocaleMiddleware`` can only select languages for which there is a
  106. Django-provided base translation. If you want to provide translations
  107. for your application that aren't already in the set of translations
  108. in Django's source tree, you'll want to provide at least a basic
  109. one as described in the :ref:`Locale restrictions<locale-restrictions>`
  110. note.
  111. Once ``LocaleMiddleware`` determines the user's preference, it makes this
  112. preference available as ``request.LANGUAGE_CODE`` for each
  113. :class:`~django.http.HttpRequest`. Feel free to read this value in your view
  114. code. Here's a simple example::
  115. def hello_world(request, count):
  116. if request.LANGUAGE_CODE == 'de-at':
  117. return HttpResponse("You prefer to read Austrian German.")
  118. else:
  119. return HttpResponse("You prefer to read another language.")
  120. Note that, with static (middleware-less) translation, the language is in
  121. ``settings.LANGUAGE_CODE``, while with dynamic (middleware) translation, it's
  122. in ``request.LANGUAGE_CODE``.
  123. .. _settings file: ../settings/
  124. .. _middleware documentation: ../middleware/
  125. .. _session: ../sessions/
  126. .. _request object: ../request_response/#httprequest-objects
  127. How Django discovers translations
  128. ---------------------------------
  129. As described in :ref:`using-translations-in-your-own-projects`,
  130. at runtime, Django looks for translations by following this algorithm:
  131. * First, it looks for a ``locale`` directory in the directory containing
  132. your settings file.
  133. * Second, it looks for a ``locale`` directory in the project directory.
  134. * Third, it looks for a ``locale`` directory in each of the installed apps.
  135. It does this in the reverse order of INSTALLED_APPS
  136. * Finally, it checks the Django-provided base translation in
  137. ``django/conf/locale``.
  138. In all cases the name of the directory containing the translation is expected to
  139. be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,
  140. etc.