i18n.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.