Collect translated messages from other applications.
Add application
collectmessages
toINSTALLED_APPS
insettings.py
:INSTALLED_APPS = ( ... 'collectmessages', ... )
Add the following options to
settings.py
:COLLECTMESSAGES_PATH
The path of the locale directory where the generated translation files will be placed. This option is required.
COLLECTMESSAGES_LANGUAGES
A list of language codes. Translation files will be generated for each code. This option is required.
COLLECTMESSAGES_FINDERS
A list of message finders. Each message finder returns a sequence of translated messages for the translation files. This option is required.
COLLECTMESSAGES_METADATA
A dictionary with the metadata for the PO-files. This setting is optional.
Example:
COLLECTMESSAGES_PATH = "collected/locale" COLLECTMESSAGES_LANGUAGES = ("en", "nl") COLLECTMESSAGES_FINDERS = ( "firstexampleapp.messages.MessageFinder", "secondexampleapp.messages.MessageFinder", ) COLLECTMESSAGES_METADATA = { "Project-Id-Version": "1.0", "Report-Msgid-Bugs-To": "[email protected]", "POT-Creation-Date": "2007-10-18 14:00+0100", "PO-Revision-Date": "2007-10-18 14:00+0100", "Last-Translator": "John Doe <[email protected]>", "Language-Team": "Translations <[email protected]>", "MIME-Version": "1.0", "Content-Type": "text/plain; charset=utf-8", "Content-Transfer-Encoding": "8bit", }
Add
COLLECTMESSAGES_PATH
toLOCALE_PATHS
insettings.py
:LOCALE_PATHS = ( ... "collected/locale", ... )
Use the custom command
collect_messages
to generate the translation files:$ python django-manage.py collect_messages
This command has the following options:
-c, --compile Compile the PO-files.
-t, --pot-file Create a POT-file.
-s, --verbose Show more information.
Create custom message finders by subclassing
BaseMessageFinder
and customizing methodfind()
:from collectmessages.finders import BaseMessageFinder from polib import POEntry class MessageFinder(BaseMessageFinder): def find(self, language=None): ...
Method
find()
returns a sequence ofPOEntry
objects with the translated messages. It takes one (optional) parameterlanguage
. If given, this parameter has the language code for the translated messages. If not given,find()
should returnPOEntry
objects for a POT-file, with an empty translated string.
- Bert Vanderbauwhede <[email protected]>, Author