| 30 | | How to internationalize your app: in three steps |
|---|
| 31 | | ------------------------------------------------ |
|---|
| 32 | | |
|---|
| 33 | | 1. Embed translation strings in your Python code and templates. |
|---|
| 34 | | 2. Get translations for those strings, in whichever languages you want to |
|---|
| 35 | | support. |
|---|
| 36 | | 3. Activate the locale middleware in your Django settings. |
|---|
| 37 | | |
|---|
| 38 | | .. admonition:: Behind the scenes |
|---|
| 39 | | |
|---|
| 40 | | Django's translation machinery uses the standard ``gettext`` module that |
|---|
| 41 | | comes with Python. |
|---|
| 42 | | |
|---|
| 43 | | If you don't need internationalization |
|---|
| 44 | | ====================================== |
|---|
| | 30 | If you don't need internationalization in your app |
|---|
| | 31 | ================================================== |
|---|
| 58 | | How to specify translation strings |
|---|
| 59 | | ================================== |
|---|
| | 45 | If you do need internationalization: three steps |
|---|
| | 46 | ================================================ |
|---|
| | 47 | |
|---|
| | 48 | 1. Embed translation strings in your Python code and templates. |
|---|
| | 49 | 2. Get translations for those strings, in whichever languages you want to |
|---|
| | 50 | support. |
|---|
| | 51 | 3. Activate the locale middleware in your Django settings. |
|---|
| | 52 | |
|---|
| | 53 | .. admonition:: Behind the scenes |
|---|
| | 54 | |
|---|
| | 55 | Django's translation machinery uses the standard ``gettext`` module that |
|---|
| | 56 | comes with Python. |
|---|
| | 57 | |
|---|
| | 58 | 1. How to specify translation strings |
|---|
| | 59 | ===================================== |
|---|
| | 632 | Using translations in your own projects |
|---|
| | 633 | ======================================= |
|---|
| | 634 | |
|---|
| | 635 | Django looks for translations by following this algorithm: |
|---|
| | 636 | |
|---|
| | 637 | * First, it looks for a ``locale`` directory in the application directory |
|---|
| | 638 | of the view that's being called. If it finds a translation for the |
|---|
| | 639 | selected language, the translation will be installed. |
|---|
| | 640 | * Next, it looks for a ``locale`` directory in the project directory. If it |
|---|
| | 641 | finds a translation, the translation will be installed. |
|---|
| | 642 | * Finally, it checks the base translation in ``django/conf/locale``. |
|---|
| | 643 | |
|---|
| | 644 | This way, you can write applications that include their own translations, and |
|---|
| | 645 | you can override base translations in your project path. Or, you can just build |
|---|
| | 646 | a big project out of several apps and put all translations into one big project |
|---|
| | 647 | message file. The choice is yours. |
|---|
| | 648 | |
|---|
| | 649 | .. note:: |
|---|
| | 650 | |
|---|
| | 651 | If you're using manually configured settings, as described in the |
|---|
| | 652 | `settings documentation`_, the ``locale`` directory in the project |
|---|
| | 653 | directory will not be examined, since Django loses the ability to work out |
|---|
| | 654 | the location of the project directory. (Django normally uses the location |
|---|
| | 655 | of the settings file to determine this, and a settings file doesn't exist |
|---|
| | 656 | if you're manually configuring your settings.) |
|---|
| | 657 | |
|---|
| | 658 | .. _settings documentation: ../settings/#using-settings-without-the-django-settings-module-environment-variable |
|---|
| | 659 | |
|---|
| | 660 | All message file repositories are structured the same way. They are: |
|---|
| | 661 | |
|---|
| | 662 | * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| | 663 | * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| | 664 | * All paths listed in ``LOCALE_PATHS`` in your settings file are |
|---|
| | 665 | searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| | 666 | * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| | 667 | |
|---|
| | 668 | To create message files, you use the same ``make-messages.py`` tool as with the |
|---|
| | 669 | Django message files. You only need to be in the right place -- in the directory |
|---|
| | 670 | where either the ``conf/locale`` (in case of the source tree) or the ``locale/`` |
|---|
| | 671 | (in case of app messages or project messages) directory are located. And you |
|---|
| | 672 | use the same ``compile-messages.py`` to produce the binary ``django.mo`` files that |
|---|
| | 673 | are used by ``gettext``. |
|---|
| | 674 | |
|---|
| | 675 | Application message files are a bit complicated to discover -- they need the |
|---|
| | 676 | ``LocaleMiddleware``. If you don't use the middleware, only the Django message |
|---|
| | 677 | files and project message files will be processed. |
|---|
| | 678 | |
|---|
| | 679 | Finally, you should give some thought to the structure of your translation |
|---|
| | 680 | files. If your applications need to be delivered to other users and will |
|---|
| | 681 | be used in other projects, you might want to use app-specific translations. |
|---|
| | 682 | But using app-specific translations and project translations could produce |
|---|
| | 683 | weird problems with ``make-messages``: ``make-messages`` will traverse all |
|---|
| | 684 | directories below the current path and so might put message IDs into the |
|---|
| | 685 | project message file that are already in application message files. |
|---|
| | 686 | |
|---|
| | 687 | The easiest way out is to store applications that are not part of the project |
|---|
| | 688 | (and so carry their own translations) outside the project tree. That way, |
|---|
| | 689 | ``make-messages`` on the project level will only translate strings that are |
|---|
| | 690 | connected to your explicit project and not strings that are distributed |
|---|
| | 691 | independently. |
|---|
| | 692 | |
|---|
| 670 | | Using translations in your own projects |
|---|
| 671 | | ======================================= |
|---|
| 672 | | |
|---|
| 673 | | Django looks for translations by following this algorithm: |
|---|
| 674 | | |
|---|
| 675 | | * First, it looks for a ``locale`` directory in the application directory |
|---|
| 676 | | of the view that's being called. If it finds a translation for the |
|---|
| 677 | | selected language, the translation will be installed. |
|---|
| 678 | | * Next, it looks for a ``locale`` directory in the project directory. If it |
|---|
| 679 | | finds a translation, the translation will be installed. |
|---|
| 680 | | * Finally, it checks the base translation in ``django/conf/locale``. |
|---|
| 681 | | |
|---|
| 682 | | This way, you can write applications that include their own translations, and |
|---|
| 683 | | you can override base translations in your project path. Or, you can just build |
|---|
| 684 | | a big project out of several apps and put all translations into one big project |
|---|
| 685 | | message file. The choice is yours. |
|---|
| 686 | | |
|---|
| 687 | | .. note:: |
|---|
| 688 | | |
|---|
| 689 | | If you're using manually configured settings, as described in the |
|---|
| 690 | | `settings documentation`_, the ``locale`` directory in the project |
|---|
| 691 | | directory will not be examined, since Django loses the ability to work out |
|---|
| 692 | | the location of the project directory. (Django normally uses the location |
|---|
| 693 | | of the settings file to determine this, and a settings file doesn't exist |
|---|
| 694 | | if you're manually configuring your settings.) |
|---|
| 695 | | |
|---|
| 696 | | .. _settings documentation: ../settings/#using-settings-without-the-django-settings-module-environment-variable |
|---|
| 697 | | |
|---|
| 698 | | All message file repositories are structured the same way. They are: |
|---|
| 699 | | |
|---|
| 700 | | * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| 701 | | * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| 702 | | * All paths listed in ``LOCALE_PATHS`` in your settings file are |
|---|
| 703 | | searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| 704 | | * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)`` |
|---|
| 705 | | |
|---|
| 706 | | To create message files, you use the same ``make-messages.py`` tool as with the |
|---|
| 707 | | Django message files. You only need to be in the right place -- in the directory |
|---|
| 708 | | where either the ``conf/locale`` (in case of the source tree) or the ``locale/`` |
|---|
| 709 | | (in case of app messages or project messages) directory are located. And you |
|---|
| 710 | | use the same ``compile-messages.py`` to produce the binary ``django.mo`` files that |
|---|
| 711 | | are used by ``gettext``. |
|---|
| 712 | | |
|---|
| 713 | | Application message files are a bit complicated to discover -- they need the |
|---|
| 714 | | ``LocaleMiddleware``. If you don't use the middleware, only the Django message |
|---|
| 715 | | files and project message files will be processed. |
|---|
| 716 | | |
|---|
| 717 | | Finally, you should give some thought to the structure of your translation |
|---|
| 718 | | files. If your applications need to be delivered to other users and will |
|---|
| 719 | | be used in other projects, you might want to use app-specific translations. |
|---|
| 720 | | But using app-specific translations and project translations could produce |
|---|
| 721 | | weird problems with ``make-messages``: ``make-messages`` will traverse all |
|---|
| 722 | | directories below the current path and so might put message IDs into the |
|---|
| 723 | | project message file that are already in application message files. |
|---|
| 724 | | |
|---|
| 725 | | The easiest way out is to store applications that are not part of the project |
|---|
| 726 | | (and so carry their own translations) outside the project tree. That way, |
|---|
| 727 | | ``make-messages`` on the project level will only translate strings that are |
|---|
| 728 | | connected to your explicit project and not strings that are distributed |
|---|
| 729 | | independently. |
|---|
| 730 | | |
|---|