setup.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. .. -*- mode: rst; encoding: utf-8 -*-
  2. .. _setup-integration:
  3. ================================
  4. Distutils/Setuptools Integration
  5. ================================
  6. Babel provides commands for integration into ``setup.py`` scripts, based on
  7. either the ``distutils`` package that is part of the Python standard library,
  8. or the third-party ``setuptools`` package.
  9. These commands are available by default when Babel has been properly installed,
  10. and ``setup.py`` is using ``setuptools``. For projects that use plain old
  11. ``distutils``, the commands need to be registered explicitly, for example:
  12. .. code-block:: python
  13. from distutils.core import setup
  14. from babel.messages import frontend as babel
  15. setup(
  16. ...
  17. cmdclass = {'compile_catalog': babel.compile_catalog,
  18. 'extract_messages': babel.extract_messages,
  19. 'init_catalog': babel.init_catalog,
  20. 'update_catalog': babel.update_catalog}
  21. )
  22. compile_catalog
  23. ===============
  24. The ``compile_catalog`` command is similar to the GNU ``msgfmt`` tool, in that
  25. it takes a message catalog from a PO file and compiles it to a binary MO file.
  26. If the command has been correctly installed or registered, a project's
  27. ``setup.py`` script should allow you to use the command::
  28. $ ./setup.py compile_catalog --help
  29. Global options:
  30. --verbose (-v) run verbosely (default)
  31. --quiet (-q) run quietly (turns verbosity off)
  32. --dry-run (-n) don't actually do anything
  33. --help (-h) show detailed help message
  34. Options for 'compile_catalog' command:
  35. ...
  36. Running the command will produce a binary MO file::
  37. $ ./setup.py compile_catalog --directory foobar/locale --locale pt_BR
  38. running compile_catalog
  39. compiling catalog to foobar/locale/pt_BR/LC_MESSAGES/messages.mo
  40. Options
  41. -------
  42. The ``compile_catalog`` command accepts the following options:
  43. +-----------------------------+---------------------------------------------+
  44. | Option | Description |
  45. +=============================+=============================================+
  46. | ``--domain`` | domain of the PO file (defaults to |
  47. | | lower-cased project name) |
  48. +-----------------------------+---------------------------------------------+
  49. | ``--directory`` (``-d``) | name of the base directory |
  50. +-----------------------------+---------------------------------------------+
  51. | ``--input-file`` (``-i``) | name of the input file |
  52. +-----------------------------+---------------------------------------------+
  53. | ``--output-file`` (``-o``) | name of the output file |
  54. +-----------------------------+---------------------------------------------+
  55. | ``--locale`` (``-l``) | locale for the new localized string |
  56. +-----------------------------+---------------------------------------------+
  57. | ``--use-fuzzy`` (``-f``) | also include "fuzzy" translations |
  58. +-----------------------------+---------------------------------------------+
  59. | ``--statistics`` | print statistics about translations |
  60. +-----------------------------+---------------------------------------------+
  61. If ``directory`` is specified, but ``output-file`` is not, the default filename
  62. of the output file will be::
  63. <directory>/<locale>/LC_MESSAGES/<domain>.mo
  64. If neither the ``input_file`` nor the ``locale`` option is set, this command
  65. looks for all catalog files in the base directory that match the given domain,
  66. and compiles each of them to MO files in the same directory.
  67. These options can either be specified on the command-line, or in the
  68. ``setup.cfg`` file.
  69. extract_messages
  70. ================
  71. The ``extract_messages`` command is comparable to the GNU ``xgettext`` program:
  72. it can extract localizable messages from a variety of difference source files,
  73. and generate a PO (portable object) template file from the collected messages.
  74. If the command has been correctly installed or registered, a project's
  75. ``setup.py`` script should allow you to use the command::
  76. $ ./setup.py extract_messages --help
  77. Global options:
  78. --verbose (-v) run verbosely (default)
  79. --quiet (-q) run quietly (turns verbosity off)
  80. --dry-run (-n) don't actually do anything
  81. --help (-h) show detailed help message
  82. Options for 'extract_messages' command:
  83. ...
  84. Running the command will produce a PO template file::
  85. $ ./setup.py extract_messages --output-file foobar/locale/messages.pot
  86. running extract_messages
  87. extracting messages from foobar/__init__.py
  88. extracting messages from foobar/core.py
  89. ...
  90. writing PO template file to foobar/locale/messages.pot
  91. Method Mapping
  92. --------------
  93. The mapping of file patterns to extraction methods (and options) can be
  94. specified using a configuration file that is pointed to using the
  95. ``--mapping-file`` option shown above. Alternatively, you can configure the
  96. mapping directly in ``setup.py`` using a keyword argument to the ``setup()``
  97. function:
  98. .. code-block:: python
  99. setup(...
  100. message_extractors = {
  101. 'foobar': [
  102. ('**.py', 'python', None),
  103. ('**/templates/**.html', 'genshi', None),
  104. ('**/templates/**.txt', 'genshi', {
  105. 'template_class': 'genshi.template:TextTemplate'
  106. })
  107. ],
  108. },
  109. ...
  110. )
  111. Options
  112. -------
  113. The ``extract_messages`` command accepts the following options:
  114. +-----------------------------+----------------------------------------------+
  115. | Option | Description |
  116. +=============================+==============================================+
  117. | ``--charset`` | charset to use in the output file |
  118. +-----------------------------+----------------------------------------------+
  119. | ``--keywords`` (``-k``) | space-separated list of keywords to look for |
  120. | | in addition to the defaults |
  121. +-----------------------------+----------------------------------------------+
  122. | ``--no-default-keywords`` | do not include the default keywords |
  123. +-----------------------------+----------------------------------------------+
  124. | ``--mapping-file`` (``-F``) | path to the mapping configuration file |
  125. +-----------------------------+----------------------------------------------+
  126. | ``--no-location`` | do not include location comments with |
  127. | | filename and line number |
  128. +-----------------------------+----------------------------------------------+
  129. | ``--omit-header`` | do not include msgid "" entry in header |
  130. +-----------------------------+----------------------------------------------+
  131. | ``--output-file`` (``-o``) | name of the output file |
  132. +-----------------------------+----------------------------------------------+
  133. | ``--width`` (``-w``) | set output line width (default 76) |
  134. +-----------------------------+----------------------------------------------+
  135. | ``--no-wrap`` | do not break long message lines, longer than |
  136. | | the output line width, into several lines |
  137. +-----------------------------+----------------------------------------------+
  138. | ``--input-dirs`` | directories that should be scanned for |
  139. | | messages |
  140. +-----------------------------+----------------------------------------------+
  141. | ``--sort-output`` | generate sorted output (default False) |
  142. +-----------------------------+----------------------------------------------+
  143. | ``--sort-by-file`` | sort output by file location (default False) |
  144. +-----------------------------+----------------------------------------------+
  145. | ``--msgid-bugs-address`` | set email address for message bug reports |
  146. +-----------------------------+----------------------------------------------+
  147. | ``--copyright-holder`` | set copyright holder in output |
  148. +-----------------------------+----------------------------------------------+
  149. | ``--add-comments (-c)`` | place comment block with TAG (or those |
  150. | | preceding keyword lines) in output file. |
  151. | | Separate multiple TAGs with commas(,) |
  152. +-----------------------------+----------------------------------------------+
  153. These options can either be specified on the command-line, or in the
  154. ``setup.cfg`` file. In the latter case, the options above become entries of the
  155. section ``[extract_messages]``, and the option names are changed to use
  156. underscore characters instead of dashes, for example:
  157. .. code-block:: ini
  158. [extract_messages]
  159. keywords = _ gettext ngettext
  160. mapping_file = mapping.cfg
  161. width = 80
  162. This would be equivalent to invoking the command from the command-line as
  163. follows::
  164. $ setup.py extract_messages -k _ -k gettext -k ngettext -F mapping.cfg -w 80
  165. Any path names are interpreted relative to the location of the ``setup.py``
  166. file. For boolean options, use "true" or "false" values.
  167. init_catalog
  168. ============
  169. The ``init_catalog`` command is basically equivalent to the GNU ``msginit``
  170. program: it creates a new translation catalog based on a PO template file (POT).
  171. If the command has been correctly installed or registered, a project's
  172. ``setup.py`` script should allow you to use the command::
  173. $ ./setup.py init_catalog --help
  174. Global options:
  175. --verbose (-v) run verbosely (default)
  176. --quiet (-q) run quietly (turns verbosity off)
  177. --dry-run (-n) don't actually do anything
  178. --help (-h) show detailed help message
  179. Options for 'init_catalog' command:
  180. ...
  181. Running the command will produce a PO file::
  182. $ ./setup.py init_catalog -l fr -i foobar/locales/messages.pot \
  183. -o foobar/locales/fr/messages.po
  184. running init_catalog
  185. creating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'
  186. Options
  187. -------
  188. The ``init_catalog`` command accepts the following options:
  189. +-----------------------------+---------------------------------------------+
  190. | Option | Description |
  191. +=============================+=============================================+
  192. | ``--domain`` | domain of the PO file (defaults to |
  193. | | lower-cased project name) |
  194. +-----------------------------+---------------------------------------------+
  195. | ``--input-file`` (``-i``) | name of the input file |
  196. +-----------------------------+---------------------------------------------+
  197. | ``--output-dir`` (``-d``) | name of the output directory |
  198. +-----------------------------+---------------------------------------------+
  199. | ``--output-file`` (``-o``) | name of the output file |
  200. +-----------------------------+---------------------------------------------+
  201. | ``--locale`` | locale for the new localized string |
  202. +-----------------------------+---------------------------------------------+
  203. If ``output-dir`` is specified, but ``output-file`` is not, the default filename
  204. of the output file will be::
  205. <output_dir>/<locale>/LC_MESSAGES/<domain>.po
  206. These options can either be specified on the command-line, or in the
  207. ``setup.cfg`` file.
  208. update_catalog
  209. ==============
  210. The ``update_catalog`` command is basically equivalent to the GNU ``msgmerge``
  211. program: it updates an existing translations catalog based on a PO template
  212. file (POT).
  213. If the command has been correctly installed or registered, a project's
  214. ``setup.py`` script should allow you to use the command::
  215. $ ./setup.py update_catalog --help
  216. Global options:
  217. --verbose (-v) run verbosely (default)
  218. --quiet (-q) run quietly (turns verbosity off)
  219. --dry-run (-n) don't actually do anything
  220. --help (-h) show detailed help message
  221. Options for 'update_catalog' command:
  222. ...
  223. Running the command will update a PO file::
  224. $ ./setup.py update_catalog -l fr -i foobar/locales/messages.pot \
  225. -o foobar/locales/fr/messages.po
  226. running update_catalog
  227. updating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'
  228. Options
  229. -------
  230. The ``update_catalog`` command accepts the following options:
  231. +-------------------------------------+-------------------------------------+
  232. | Option | Description |
  233. +=====================================+=====================================+
  234. | ``--domain`` | domain of the PO file (defaults to |
  235. | | lower-cased project name) |
  236. +-------------------------------------+-------------------------------------+
  237. | ``--input-file`` (``-i``) | name of the input file |
  238. +-------------------------------------+-------------------------------------+
  239. | ``--output-dir`` (``-d``) | name of the output directory |
  240. +-------------------------------------+-------------------------------------+
  241. | ``--output-file`` (``-o``) | name of the output file |
  242. +-------------------------------------+-------------------------------------+
  243. | ``--locale`` | locale for the new localized string |
  244. +-------------------------------------+-------------------------------------+
  245. | ``--ignore-obsolete`` | do not include obsolete messages in |
  246. | | the output |
  247. +-------------------------------------+-------------------------------------+
  248. | ``--no-fuzzy-matching`` (``-N``) | do not use fuzzy matching |
  249. +-------------------------------------+-------------------------------------+
  250. | ``--previous`` | keep previous msgids of translated |
  251. | | messages |
  252. +-------------------------------------+-------------------------------------+
  253. If ``output-dir`` is specified, but ``output-file`` is not, the default filename
  254. of the output file will be::
  255. <output_dir>/<locale>/LC_MESSAGES/<domain>.po
  256. If neither the ``input_file`` nor the ``locale`` option is set, this command
  257. looks for all catalog files in the base directory that match the given domain,
  258. and updates each of them.
  259. These options can either be specified on the command-line, or in the
  260. ``setup.cfg`` file.