i18n.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. .. _using-translations-in-your-own-projects:
  2. ===============================================
  3. Using internationalization in your own projects
  4. ===============================================
  5. At runtime, Django builds an in-memory unified catalog of literals-translations.
  6. To achieve this it looks for translations by following this algorithm regarding
  7. the order in which it examines the different file paths to load the compiled
  8. :term:`message files <message file>` (``.mo``) and the precedence of multiple
  9. translations for the same literal:
  10. 1. The directories listed in :setting:`LOCALE_PATHS` have the highest
  11. precedence, with the ones appearing first having higher precedence than
  12. the ones appearing later.
  13. 2. Then, it looks for and uses if it exists a ``locale`` directory in each
  14. of the installed apps listed in :setting:`INSTALLED_APPS`. The ones
  15. appearing first have higher precedence than the ones appearing later.
  16. 3. Then, it looks for a ``locale`` directory in the project directory, or
  17. more accurately, in the directory containing your settings file.
  18. 4. Finally, the Django-provided base translation in ``django/conf/locale``
  19. is used as a fallback.
  20. .. deprecated:: 1.3
  21. Lookup in the ``locale`` subdirectory of the directory containing your
  22. settings file (item 3 above) is deprecated since the 1.3 release and will be
  23. removed in Django 1.5. You can use the :setting:`LOCALE_PATHS` setting
  24. instead, by listing the absolute filesystem path of such ``locale``
  25. directory in the setting value.
  26. .. seealso::
  27. The translations for literals included in JavaScript assets are looked up
  28. following a similar but not identical algorithm. See the
  29. :ref:`javascript_catalog view documentation <javascript_catalog-view>` for
  30. more details.
  31. In all cases the name of the directory containing the translation is expected to
  32. be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,
  33. etc.
  34. This way, you can write applications that include their own translations, and
  35. you can override base translations in your project path. Or, you can just build
  36. a big project out of several apps and put all translations into one big common
  37. message file specific to the project you are composing. The choice is yours.
  38. .. note::
  39. If you're using manually configured settings, as described in
  40. :ref:`settings-without-django-settings-module`, the ``locale`` directory in
  41. the project directory will not be examined, since Django loses the ability
  42. to work out the location of the project directory. (Django normally uses the
  43. location of the settings file to determine this, and a settings file doesn't
  44. exist if you're manually configuring your settings.)
  45. All message file repositories are structured the same way. They are:
  46. * All paths listed in :setting:`LOCALE_PATHS` in your settings file are
  47. searched for ``<language>/LC_MESSAGES/django.(po|mo)``
  48. * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` --
  49. deprecated, see above.
  50. * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
  51. * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)``
  52. To create message files, you use the :djadmin:`django-admin.py makemessages <makemessages>`
  53. tool. You only need to be in the same directory where the ``locale/`` directory
  54. is located. And you use :djadmin:`django-admin.py compilemessages <compilemessages>`
  55. to produce the binary ``.mo`` files that are used by ``gettext``. Read the
  56. :doc:`/topics/i18n/localization` document for more details.
  57. You can also run ``django-admin.py compilemessages --settings=path.to.settings``
  58. to make the compiler process all the directories in your :setting:`LOCALE_PATHS`
  59. setting.
  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-specific translations could
  64. produce weird problems with ``makemessages``: It will traverse all directories
  65. below the current path and so might put message IDs into a unified, common
  66. message file for the current project that are already in application message
  67. files.
  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``, when ran on a project level will only extract
  71. strings that are connected to your explicit project and not strings that are
  72. distributed independently.
  73. Using translations outside views and templates
  74. ==============================================
  75. While Django provides a rich set of i18n tools for use in views and templates,
  76. it does not restrict the usage to Django-specific code. The Django translation
  77. mechanisms can be used to translate arbitrary texts to any language that is
  78. supported by Django (as long as an appropriate translation catalog exists, of
  79. course). You can load a translation catalog, activate it and translate text to
  80. language of your choice, but remember to switch back to original language, as
  81. activating a translation catalog is done on per-thread basis and such change
  82. will affect code running in the same thread.
  83. For example::
  84. from django.utils import translation
  85. def welcome_translated(language):
  86. cur_language = translation.get_language()
  87. try:
  88. translation.activate(language)
  89. text = translation.ugettext('welcome')
  90. finally:
  91. translation.activate(cur_language)
  92. return text
  93. Calling this function with the value 'de' will give you ``"Willkommen"``,
  94. regardless of :setting:`LANGUAGE_CODE` and language set by middleware.
  95. Functions of particular interest are ``django.utils.translation.get_language()``
  96. which returns the language used in the current thread,
  97. ``django.utils.translation.activate()`` which activates a translation catalog
  98. for the current thread, and ``django.utils.translation.check_for_language()``
  99. which checks if the given language is supported by Django.