i18n.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. .. _using-translations-in-your-own-projects:
  2. ===============================================
  3. Using internationalization in your own projects
  4. ===============================================
  5. At runtime, Django looks for translations by following this algorithm:
  6. * First, it looks for a ``locale`` directory in the application directory
  7. of the view that's being called. If it finds a translation for the
  8. selected language, the translation will be installed.
  9. * Next, it looks for a ``locale`` directory in the project directory. If it
  10. finds a translation, the translation will be installed.
  11. * Finally, it checks the Django-provided base translation in
  12. ``django/conf/locale``.
  13. In all cases the name of the directory containing the translation is expected to
  14. be named using :term:`locale name` notation. E.g. ``de``, ``pt_BR``, ``es_AR``,
  15. etc.
  16. This way, you can write applications that include their own translations, and
  17. you can override base translations in your project path. Or, you can just build
  18. a big project out of several apps and put all translations into one big project
  19. message file. The choice is yours.
  20. .. note::
  21. If you're using manually configured settings, as described in
  22. :ref:`settings-without-django-settings-module`, the ``locale`` directory in
  23. the project directory will not be examined, since Django loses the ability
  24. to work out the location of the project directory. (Django normally uses the
  25. location of the settings file to determine this, and a settings file doesn't
  26. exist if you're manually configuring your settings.)
  27. All message file repositories are structured the same way. They are:
  28. * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
  29. * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
  30. * All paths listed in ``LOCALE_PATHS`` in your settings file are
  31. searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)``
  32. * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)``
  33. To create message files, you use the :djadmin:`django-admin.py makemessages <makemessages>`
  34. tool. You only need to be in the same directory where the ``locale/`` directory
  35. is located. And you use :djadmin:`django-admin.py compilemessages <compilemessages>`
  36. to produce the binary ``.mo`` files that are used by ``gettext``. Read the
  37. :doc:`/topics/i18n/localization` document for more details.
  38. You can also run ``django-admin.py compilemessages --settings=path.to.settings``
  39. to make the compiler process all the directories in your :setting:`LOCALE_PATHS`
  40. setting.
  41. Application message files are a bit complicated to discover -- they need the
  42. :class:`~django.middleware.locale.LocaleMiddleware`. If you don't use the
  43. middleware, only the Django message files and project message files will be
  44. installed and available at runtime.
  45. Finally, you should give some thought to the structure of your translation
  46. files. If your applications need to be delivered to other users and will
  47. be used in other projects, you might want to use app-specific translations.
  48. But using app-specific translations and project translations could produce
  49. weird problems with ``makemessages``: It will traverse all directories below
  50. the current path and so might put message IDs into the project message file
  51. that are already in application message files.
  52. The easiest way out is to store applications that are not part of the project
  53. (and so carry their own translations) outside the project tree. That way,
  54. ``django-admin.py makemessages`` on the project level will only translate
  55. strings that are connected to your explicit project and not strings that are
  56. distributed independently.
  57. Using translations outside views and templates
  58. ==============================================
  59. While Django provides a rich set of i18n tools for use in views and templates,
  60. it does not restrict the usage to Django-specific code. The Django translation
  61. mechanisms can be used to translate arbitrary texts to any language that is
  62. supported by Django (as long as an appropriate translation catalog exists, of
  63. course). You can load a translation catalog, activate it and translate text to
  64. language of your choice, but remember to switch back to original language, as
  65. activating a translation catalog is done on per-thread basis and such change
  66. will affect code running in the same thread.
  67. For example::
  68. from django.utils import translation
  69. def welcome_translated(language):
  70. cur_language = translation.get_language()
  71. try:
  72. translation.activate(language)
  73. text = translation.ugettext('welcome')
  74. finally:
  75. translation.activate(cur_language)
  76. return text
  77. Calling this function with the value 'de' will give you ``"Willkommen"``,
  78. regardless of :setting:`LANGUAGE_CODE` and language set by middleware.
  79. Functions of particular interest are ``django.utils.translation.get_language()``
  80. which returns the language used in the current thread,
  81. ``django.utils.translation.activate()`` which activates a translation catalog
  82. for the current thread, and ``django.utils.translation.check_for_language()``
  83. which checks if the given language is supported by Django.