cmlenz@263: .. -*- mode: rst; encoding: utf-8 -*- cmlenz@263: cmlenz@263: ================================ cmlenz@263: Distutils/Setuptools Integration cmlenz@263: ================================ cmlenz@263: cmlenz@263: Babel provides commands for integration into ``setup.py`` scripts, based on cmlenz@263: either the ``distutils`` package that is part of the Python standard library, cmlenz@263: or the third-party ``setuptools`` package. cmlenz@263: cmlenz@263: These commands are available by default when Babel has been properly installed, cmlenz@263: and ``setup.py`` is using ``setuptools``. For projects that use plain old cmlenz@263: ``distutils``, the commands need to be registered explicitly, for example: cmlenz@263: cmlenz@263: .. code-block:: python cmlenz@263: cmlenz@263: from distutils.core import setup cmlenz@263: from babel.messages import frontend as babel cmlenz@263: cmlenz@263: setup( cmlenz@263: ... cmlenz@263: cmdclass = {'compile_catalog': babel.compile_catalog, cmlenz@263: 'extract_messages': babel.extract_messages, cmlenz@263: 'init_catalog': babel.init_catalog, cmlenz@263: 'update_catalog': babel.update_catalog} cmlenz@263: ) cmlenz@263: cmlenz@263: cmlenz@263: .. contents:: Contents cmlenz@263: :depth: 2 cmlenz@263: .. sectnum:: cmlenz@263: cmlenz@263: cmlenz@263: compile_catalog cmlenz@263: =============== cmlenz@263: cmlenz@263: The ``compile_catalog`` command is similar to the GNU ``msgfmt`` tool, in that cmlenz@263: it takes a message catalog from a PO file and compiles it to a binary MO file. cmlenz@263: cmlenz@263: If the command has been correctly installed or registered, a project's cmlenz@263: ``setup.py`` script should allow you to use the command:: cmlenz@263: cmlenz@263: $ ./setup.py compile_catalog --help cmlenz@263: Global options: cmlenz@263: --verbose (-v) run verbosely (default) cmlenz@263: --quiet (-q) run quietly (turns verbosity off) cmlenz@263: --dry-run (-n) don't actually do anything cmlenz@263: --help (-h) show detailed help message cmlenz@263: cmlenz@263: Options for 'compile_catalog' command: cmlenz@263: ... cmlenz@263: cmlenz@263: Running the command will produce a PO template file:: cmlenz@263: cmlenz@263: $ ./setup.py compile_catalog --directory foobar/locale --locale pt_BR cmlenz@263: running compile_catalog cmlenz@263: compiling catalog to to foobar/locale/pt_BR/LC_MESSAGES/messages.mo cmlenz@263: cmlenz@263: cmlenz@263: Options cmlenz@263: ------- cmlenz@263: cmlenz@263: The ``compile_catalog`` command accepts the following options: cmlenz@263: cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | Option | Description | cmlenz@323: +=============================+=============================================+ cmlenz@323: | ``--domain`` | domain of the PO file (defaults to | cmlenz@323: | | lower-cased project name) | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--directory`` (``-d``) | name of the base directory | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--input-file`` (``-i``) | name of the input file | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--output-file`` (``-o``) | name of the output file | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--locale`` (``-l``) | locale for the new localized string | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--use-fuzzy`` (``-f``) | also include "fuzzy" translations | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--statistics`` | print statistics about translations | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@263: cmlenz@263: If ``directory`` is specified, but ``output-file`` is not, the default filename cmlenz@263: of the output file will be:: cmlenz@263: cmlenz@263: //LC_MESSAGES/.mo cmlenz@263: cmlenz@263: If neither the ``input_file`` nor the ``locale`` option is set, this command cmlenz@263: looks for all catalog files in the base directory that match the given domain, cmlenz@263: and compiles each of them to MO files in the same directory. cmlenz@263: cmlenz@263: These options can either be specified on the command-line, or in the cmlenz@263: ``setup.cfg`` file. cmlenz@263: cmlenz@263: cmlenz@263: extract_messages cmlenz@263: ================ cmlenz@263: cmlenz@263: The ``extract_messages`` command is comparable to the GNU ``xgettext`` program: cmlenz@263: it can extract localizable messages from a variety of difference source files, cmlenz@263: and generate a PO (portable object) template file from the collected messages. cmlenz@263: cmlenz@263: If the command has been correctly installed or registered, a project's cmlenz@263: ``setup.py`` script should allow you to use the command:: cmlenz@263: cmlenz@263: $ ./setup.py extract_messages --help cmlenz@263: Global options: cmlenz@263: --verbose (-v) run verbosely (default) cmlenz@263: --quiet (-q) run quietly (turns verbosity off) cmlenz@263: --dry-run (-n) don't actually do anything cmlenz@263: --help (-h) show detailed help message cmlenz@263: cmlenz@263: Options for 'extract_messages' command: cmlenz@263: ... cmlenz@263: cmlenz@263: Running the command will produce a PO template file:: cmlenz@263: cmlenz@263: $ ./setup.py extract_messages --output-file foobar/locale/messages.pot cmlenz@263: running extract_messages cmlenz@263: extracting messages from foobar/__init__.py cmlenz@263: extracting messages from foobar/core.py cmlenz@263: ... cmlenz@263: writing PO template file to foobar/locale/messages.pot cmlenz@263: cmlenz@263: cmlenz@263: Method Mapping cmlenz@263: -------------- cmlenz@263: cmlenz@263: The mapping of file patterns to extraction methods (and options) can be cmlenz@263: specified using a configuration file that is pointed to using the cmlenz@263: ``--mapping-file`` option shown above. Alternatively, you can configure the cmlenz@263: mapping directly in ``setup.py`` using a keyword argument to the ``setup()`` cmlenz@263: function: cmlenz@263: cmlenz@263: .. code-block:: python cmlenz@263: cmlenz@263: setup(... cmlenz@263: cmlenz@263: message_extractors = { cmlenz@263: 'foobar': [ cmlenz@263: ('**.py', 'python', None), cmlenz@263: ('**/templates/**.html', 'genshi', None), cmlenz@263: ('**/templates/**.txt', 'genshi', { cmlenz@263: 'template_class': 'genshi.template:TextTemplate' cmlenz@263: }) cmlenz@263: ], cmlenz@263: }, cmlenz@263: cmlenz@263: ... cmlenz@263: ) cmlenz@263: cmlenz@263: cmlenz@263: Options cmlenz@263: ------- cmlenz@263: cmlenz@263: The ``extract_messages`` command accepts the following options: cmlenz@263: cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | Option | Description | cmlenz@263: +=============================+==============================================+ cmlenz@263: | ``--charset`` | charset to use in the output file | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--keywords`` (``-k``) | space-separated list of keywords to look for | cmlenz@263: | | in addition to the defaults | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--no-default-keywords`` | do not include the default keywords | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--mapping-file`` (``-F``) | path to the mapping configuration file | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--no-location`` | do not include location comments with | cmlenz@263: | | filename and line number | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--omit-header`` | do not include msgid "" entry in header | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--output-file`` (``-o``) | name of the output file | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--width`` (``-w``) | set output line width (default 76) | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--no-wrap`` | do not break long message lines, longer than | cmlenz@263: | | the output line width, into several lines | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--input-dirs`` | directories that should be scanned for | cmlenz@263: | | messages | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--sort-output`` | generate sorted output (default False) | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--sort-by-file`` | sort output by file location (default False) | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--msgid-bugs-address`` | set email address for message bug reports | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--copyright-holder`` | set copyright holder in output | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: | ``--add-comments (-c)`` | place comment block with TAG (or those | cmlenz@263: | | preceding keyword lines) in output file. | cmlenz@263: | | Separate multiple TAGs with commas(,) | cmlenz@263: +-----------------------------+----------------------------------------------+ cmlenz@263: cmlenz@263: These options can either be specified on the command-line, or in the cmlenz@263: ``setup.cfg`` file. In the latter case, the options above become entries of the cmlenz@263: section ``[extract_messages]``, and the option names are changed to use cmlenz@263: underscore characters instead of dashes, for example: cmlenz@263: cmlenz@263: .. code-block:: ini cmlenz@263: cmlenz@263: [extract_messages] cmlenz@263: keywords = _, gettext, ngettext cmlenz@263: mapping_file = babel.cfg cmlenz@263: width = 80 cmlenz@263: cmlenz@263: This would be equivalent to invoking the command from the command-line as cmlenz@263: follows:: cmlenz@263: cmlenz@263: $ setup.py extract_messages -k _ -k gettext -k ngettext -F mapping.cfg -w 80 cmlenz@263: cmlenz@263: Any path names are interpreted relative to the location of the ``setup.py`` cmlenz@263: file. For boolean options, use "true" or "false" values. cmlenz@263: cmlenz@263: cmlenz@263: init_catalog cmlenz@263: ============ cmlenz@263: cmlenz@263: The ``init_catalog`` command is basically equivalent to the GNU ``msginit`` cmlenz@263: program: it creates a new translation catalog based on a PO template file (POT). cmlenz@263: cmlenz@263: If the command has been correctly installed or registered, a project's cmlenz@263: ``setup.py`` script should allow you to use the command:: cmlenz@263: cmlenz@263: $ ./setup.py init_catalog --help cmlenz@263: Global options: cmlenz@263: --verbose (-v) run verbosely (default) cmlenz@263: --quiet (-q) run quietly (turns verbosity off) cmlenz@263: --dry-run (-n) don't actually do anything cmlenz@263: --help (-h) show detailed help message cmlenz@263: cmlenz@263: Options for 'init_catalog' command: cmlenz@263: ... cmlenz@263: cmlenz@263: Running the command will produce a PO file:: cmlenz@263: cmlenz@263: $ ./setup.py init_catalog -l fr -i foobar/locales/messages.pot \ cmlenz@263: -o foobar/locales/fr/messages.po cmlenz@263: running init_catalog cmlenz@263: creating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot' cmlenz@263: cmlenz@263: cmlenz@263: Options cmlenz@263: ------- cmlenz@263: cmlenz@263: The ``init_catalog`` command accepts the following options: cmlenz@263: cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | Option | Description | cmlenz@323: +=============================+=============================================+ cmlenz@323: | ``--domain`` | domain of the PO file (defaults to | cmlenz@323: | | lower-cased project name) | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--input-file`` (``-i``) | name of the input file | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--output-dir`` (``-d``) | name of the output directory | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--output-file`` (``-o``) | name of the output file | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@323: | ``--locale`` | locale for the new localized string | cmlenz@323: +-----------------------------+---------------------------------------------+ cmlenz@263: cmlenz@263: If ``output-dir`` is specified, but ``output-file`` is not, the default filename cmlenz@263: of the output file will be:: cmlenz@263: cmlenz@263: //LC_MESSAGES/.po cmlenz@263: cmlenz@263: These options can either be specified on the command-line, or in the cmlenz@263: ``setup.cfg`` file. cmlenz@263: cmlenz@263: cmlenz@263: update_catalog cmlenz@263: ============== cmlenz@263: cmlenz@263: The ``update_catalog`` command is basically equivalent to the GNU ``msgmerge`` cmlenz@263: program: it updates an existing translations catalog based on a PO template cmlenz@263: file (POT). cmlenz@263: cmlenz@263: If the command has been correctly installed or registered, a project's cmlenz@263: ``setup.py`` script should allow you to use the command:: cmlenz@263: cmlenz@263: $ ./setup.py update_catalog --help cmlenz@263: Global options: cmlenz@263: --verbose (-v) run verbosely (default) cmlenz@263: --quiet (-q) run quietly (turns verbosity off) cmlenz@263: --dry-run (-n) don't actually do anything cmlenz@263: --help (-h) show detailed help message cmlenz@263: cmlenz@263: Options for 'update_catalog' command: cmlenz@263: ... cmlenz@263: cmlenz@263: Running the command will update a PO file:: cmlenz@263: cmlenz@263: $ ./setup.py update_catalog -l fr -i foobar/locales/messages.pot \ cmlenz@263: -o foobar/locales/fr/messages.po cmlenz@263: running update_catalog cmlenz@263: updating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot' cmlenz@263: cmlenz@263: cmlenz@263: Options cmlenz@263: ------- cmlenz@263: cmlenz@263: The ``update_catalog`` command accepts the following options: cmlenz@263: cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | Option | Description | cmlenz@323: +=====================================+=====================================+ cmlenz@323: | ``--domain`` | domain of the PO file (defaults to | cmlenz@323: | | lower-cased project name) | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--input-file`` (``-i``) | name of the input file | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--output-dir`` (``-d``) | name of the output directory | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--output-file`` (``-o``) | name of the output file | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--locale`` | locale for the new localized string | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--ignore-obsolete`` | do not include obsolete messages in | cmlenz@323: | | the output | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--no-fuzzy-matching`` (``-N``) | do not use fuzzy matching | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@323: | ``--previous`` | keep previous msgids of translated | cmlenz@323: | | messages | cmlenz@323: +-------------------------------------+-------------------------------------+ cmlenz@263: cmlenz@263: If ``output-dir`` is specified, but ``output-file`` is not, the default filename cmlenz@263: of the output file will be:: cmlenz@263: cmlenz@263: //LC_MESSAGES/.po cmlenz@263: cmlenz@263: If neither the ``input_file`` nor the ``locale`` option is set, this command cmlenz@263: looks for all catalog files in the base directory that match the given domain, cmlenz@263: and updates each of them. cmlenz@263: cmlenz@263: These options can either be specified on the command-line, or in the cmlenz@263: ``setup.cfg`` file.