i18n.txt 5.1 KB

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