/docs/topics/i18n/deployment.txt

https://code.google.com/p/mango-py/ · Plain Text · 208 lines · 158 code · 50 blank · 0 comment · 0 complexity · 2e091f445b1e3a2e1d7399ceb3da4407 MD5 · raw file

  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 :setting:`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 :setting:`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 :setting:`MIDDLEWARE_CLASSES` setting. Because middleware order
  42. matters, you 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 :setting:`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 :setting:`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 :setting:`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 :setting:`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 :setting:`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 :setting:`LANGUAGES` at
  105. runtime.
  106. * The ``LocaleMiddleware`` can only select languages for which there is a
  107. Django-provided base translation. If you want to provide translations
  108. for your application that aren't already in the set of translations
  109. in Django's source tree, you'll want to provide at least a basic
  110. one as described in the :ref:`Locale restrictions<locale-restrictions>`
  111. note.
  112. Once ``LocaleMiddleware`` determines the user's preference, it makes this
  113. preference available as ``request.LANGUAGE_CODE`` for each
  114. :class:`~django.http.HttpRequest`. Feel free to read this value in your view
  115. code. Here's a simple example::
  116. def hello_world(request, count):
  117. if request.LANGUAGE_CODE == 'de-at':
  118. return HttpResponse("You prefer to read Austrian German.")
  119. else:
  120. return HttpResponse("You prefer to read another language.")
  121. Note that, with static (middleware-less) translation, the language is in
  122. ``settings.LANGUAGE_CODE``, while with dynamic (middleware) translation, it's
  123. in ``request.LANGUAGE_CODE``.
  124. .. _settings file: ../settings/
  125. .. _middleware documentation: ../middleware/
  126. .. _session: ../sessions/
  127. .. _request object: ../request_response/#httprequest-objects
  128. How Django discovers translations
  129. ---------------------------------
  130. As described in :ref:`using-translations-in-your-own-projects`, Django looks for
  131. translations by following this algorithm regarding the order in which it
  132. examines the different file paths to load the compiled :term:`message files
  133. <message file>` (``.mo``) and the precedence of multiple translations for the
  134. same literal:
  135. 1. The directories listed in :setting:`LOCALE_PATHS` have the highest
  136. precedence, with the ones appearing first having higher precedence than
  137. the ones appearing later.
  138. 2. Then, it looks for and uses if it exists a ``locale`` directory in each
  139. of the installed apps listed in :setting:`INSTALLED_APPS`. The ones
  140. appearing first have higher precedence than the ones appearing later.
  141. 3. Then, it looks for a ``locale`` directory in the project directory, or
  142. more accurately, in the directory containing your settings file.
  143. 4. Finally, the Django-provided base translation in ``django/conf/locale``
  144. is used as a fallback.
  145. .. deprecated:: 1.3
  146. Lookup in the ``locale`` subdirectory of the directory containing your
  147. settings file (item 3 above) is deprecated since the 1.3 release and will be
  148. removed in Django 1.5. You can use the :setting:`LOCALE_PATHS` setting
  149. instead, by listing the absolute filesystem path of such ``locale``
  150. directory in the setting value.
  151. .. seealso::
  152. The translations for literals included in JavaScript assets are looked up
  153. following a similar but not identical algorithm. See the
  154. :ref:`javascript_catalog view documentation <javascript_catalog-view>` for
  155. more details.
  156. In all cases the name of the directory containing the translation is expected to
  157. be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,
  158. etc.