summaryrefslogtreecommitdiff
path: root/docs/setup.rst
blob: fd7ee754df3d27d0b8c1f9987a6ef5fff4eb6a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
.. -*- mode: rst; encoding: utf-8 -*-

.. _setup-integration:

================================
Distutils/Setuptools Integration
================================

Babel provides commands for integration into ``setup.py`` scripts, based on
either the ``distutils`` package that is part of the Python standard library,
or the third-party ``setuptools`` package.

These commands are available by default when Babel has been properly installed,
and ``setup.py`` is using ``setuptools``. For projects that use plain old
``distutils``, the commands need to be registered explicitly, for example:

.. code-block:: python

    from distutils.core import setup
    from babel.messages import frontend as babel

    setup(
        ...
        cmdclass = {'compile_catalog': babel.compile_catalog,
                    'extract_messages': babel.extract_messages,
                    'init_catalog': babel.init_catalog,
                    'update_catalog': babel.update_catalog}
    )


compile_catalog
===============

The ``compile_catalog`` command is similar to the GNU ``msgfmt`` tool, in that
it takes a message catalog from a PO file and compiles it to a binary MO file.

If the command has been correctly installed or registered, a project's
``setup.py`` script should allow you to use the command::

    $ ./setup.py compile_catalog --help
    Global options:
      --verbose (-v)  run verbosely (default)
      --quiet (-q)    run quietly (turns verbosity off)
      --dry-run (-n)  don't actually do anything
      --help (-h)     show detailed help message

    Options for 'compile_catalog' command:
       ...

Running the command will produce a binary MO file::

    $ ./setup.py compile_catalog --directory foobar/locale --locale pt_BR
    running compile_catalog
    compiling catalog to foobar/locale/pt_BR/LC_MESSAGES/messages.mo


Options
-------

The ``compile_catalog`` command accepts the following options:

  +-----------------------------+---------------------------------------------+
  | Option                      | Description                                 |
  +=============================+=============================================+
  | ``--domain``                | domain of the PO file (defaults to          |
  |                             | lower-cased project name)                   |
  +-----------------------------+---------------------------------------------+
  | ``--directory`` (``-d``)    | name of the base directory                  |
  +-----------------------------+---------------------------------------------+
  | ``--input-file`` (``-i``)   | name of the input file                      |
  +-----------------------------+---------------------------------------------+
  | ``--output-file`` (``-o``)  | name of the output file                     |
  +-----------------------------+---------------------------------------------+
  | ``--locale`` (``-l``)       | locale for the new localized string         |
  +-----------------------------+---------------------------------------------+
  | ``--use-fuzzy`` (``-f``)    | also include "fuzzy" translations           |
  +-----------------------------+---------------------------------------------+
  | ``--statistics``            | print statistics about translations         |
  +-----------------------------+---------------------------------------------+

If ``directory`` is specified, but ``output-file`` is not, the default filename
of the output file will be::

    <directory>/<locale>/LC_MESSAGES/<domain>.mo

If neither the ``input_file`` nor the ``locale`` option is set, this command
looks for all catalog files in the base directory that match the given domain,
and compiles each of them to MO files in the same directory.

These options can either be specified on the command-line, or in the
``setup.cfg`` file.


extract_messages
================

The ``extract_messages`` command is comparable to the GNU ``xgettext`` program:
it can extract localizable messages from a variety of difference source files,
and generate a PO (portable object) template file from the collected messages.

If the command has been correctly installed or registered, a project's
``setup.py`` script should allow you to use the command::

    $ ./setup.py extract_messages --help
    Global options:
      --verbose (-v)  run verbosely (default)
      --quiet (-q)    run quietly (turns verbosity off)
      --dry-run (-n)  don't actually do anything
      --help (-h)     show detailed help message

    Options for 'extract_messages' command:
       ...

Running the command will produce a PO template file::

    $ ./setup.py extract_messages --output-file foobar/locale/messages.pot
    running extract_messages
    extracting messages from foobar/__init__.py
    extracting messages from foobar/core.py
    ...
    writing PO template file to foobar/locale/messages.pot


Method Mapping
--------------

The mapping of file patterns to extraction methods (and options) can be
specified using a configuration file that is pointed to using the
``--mapping-file`` option shown above. Alternatively, you can configure the
mapping directly in ``setup.py`` using a keyword argument to the ``setup()``
function:

.. code-block:: python

    setup(...

        message_extractors = {
            'foobar': [
                ('**.py',                'python', None),
                ('**/templates/**.html', 'genshi', None),
                ('**/templates/**.txt',  'genshi', {
                    'template_class': 'genshi.template:TextTemplate'
                })
            ],
        },

        ...
    )


Options
-------

The ``extract_messages`` command accepts the following options:

  +-----------------------------+----------------------------------------------+
  | Option                      | Description                                  |
  +=============================+==============================================+
  | ``--charset``               | charset to use in the output file            |
  +-----------------------------+----------------------------------------------+
  | ``--keywords`` (``-k``)     | space-separated list of keywords to look for |
  |                             | in addition to the defaults                  |
  +-----------------------------+----------------------------------------------+
  | ``--no-default-keywords``   | do not include the default keywords          |
  +-----------------------------+----------------------------------------------+
  | ``--mapping-file`` (``-F``) | path to the mapping configuration file       |
  +-----------------------------+----------------------------------------------+
  | ``--no-location``           | do not include location comments with        |
  |                             | filename and line number                     |
  +-----------------------------+----------------------------------------------+
  | ``--omit-header``           | do not include msgid "" entry in header      |
  +-----------------------------+----------------------------------------------+
  | ``--output-file`` (``-o``)  | name of the output file                      |
  +-----------------------------+----------------------------------------------+
  | ``--width`` (``-w``)        | set output line width (default 76)           |
  +-----------------------------+----------------------------------------------+
  | ``--no-wrap``               | do not break long message lines, longer than |
  |                             | the output line width, into several lines    |
  +-----------------------------+----------------------------------------------+
  | ``--input-dirs``            | directories that should be scanned for       |
  |                             | messages                                     |
  +-----------------------------+----------------------------------------------+
  | ``--sort-output``           | generate sorted output (default False)       |
  +-----------------------------+----------------------------------------------+
  | ``--sort-by-file``          | sort output by file location (default False) |
  +-----------------------------+----------------------------------------------+
  | ``--msgid-bugs-address``    | set email address for message bug reports    |
  +-----------------------------+----------------------------------------------+
  | ``--copyright-holder``      | set copyright holder in output               |
  +-----------------------------+----------------------------------------------+
  | ``--add-comments (-c)``     | place comment block with TAG (or those       |
  |                             | preceding keyword lines) in output file.     |
  |                             | Separate multiple TAGs with commas(,)        |
  +-----------------------------+----------------------------------------------+

These options can either be specified on the command-line, or in the
``setup.cfg`` file. In the latter case, the options above become entries of the
section ``[extract_messages]``, and the option names are changed to use
underscore characters instead of dashes, for example:

.. code-block:: ini

    [extract_messages]
    keywords = _ gettext ngettext
    mapping_file = mapping.cfg
    width = 80

This would be equivalent to invoking the command from the command-line as
follows::

    $ setup.py extract_messages -k _ -k gettext -k ngettext -F mapping.cfg -w 80

Any path names are interpreted relative to the location of the ``setup.py``
file. For boolean options, use "true" or "false" values.


init_catalog
============

The ``init_catalog`` command is basically equivalent to the GNU ``msginit``
program: it creates a new translation catalog based on a PO template file (POT).

If the command has been correctly installed or registered, a project's
``setup.py`` script should allow you to use the command::

    $ ./setup.py init_catalog --help
    Global options:
      --verbose (-v)  run verbosely (default)
      --quiet (-q)    run quietly (turns verbosity off)
      --dry-run (-n)  don't actually do anything
      --help (-h)     show detailed help message

    Options for 'init_catalog' command:
      ...

Running the command will produce a PO file::

    $ ./setup.py init_catalog -l fr -i foobar/locales/messages.pot \
                             -o foobar/locales/fr/messages.po
    running init_catalog
    creating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'


Options
-------

The ``init_catalog`` command accepts the following options:

  +-----------------------------+---------------------------------------------+
  | Option                      | Description                                 |
  +=============================+=============================================+
  | ``--domain``                | domain of the PO file (defaults to          |
  |                             | lower-cased project name)                   |
  +-----------------------------+---------------------------------------------+
  | ``--input-file`` (``-i``)   | name of the input file                      |
  +-----------------------------+---------------------------------------------+
  | ``--output-dir`` (``-d``)   | name of the output directory                |
  +-----------------------------+---------------------------------------------+
  | ``--output-file`` (``-o``)  | name of the output file                     |
  +-----------------------------+---------------------------------------------+
  | ``--locale``                | locale for the new localized string         |
  +-----------------------------+---------------------------------------------+

If ``output-dir`` is specified, but ``output-file`` is not, the default filename
of the output file will be::

    <output_dir>/<locale>/LC_MESSAGES/<domain>.po

These options can either be specified on the command-line, or in the
``setup.cfg`` file.


update_catalog
==============

The ``update_catalog`` command is basically equivalent to the GNU ``msgmerge``
program: it updates an existing translations catalog based on a PO template
file (POT).

If the command has been correctly installed or registered, a project's
``setup.py`` script should allow you to use the command::

    $ ./setup.py update_catalog --help
    Global options:
      --verbose (-v)  run verbosely (default)
      --quiet (-q)    run quietly (turns verbosity off)
      --dry-run (-n)  don't actually do anything
      --help (-h)     show detailed help message

    Options for 'update_catalog' command:
      ...

Running the command will update a PO file::

    $ ./setup.py update_catalog -l fr -i foobar/locales/messages.pot \
                                -o foobar/locales/fr/messages.po
    running update_catalog
    updating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'


Options
-------

The ``update_catalog`` command accepts the following options:

  +-------------------------------------+-------------------------------------+
  | Option                              | Description                         |
  +=====================================+=====================================+
  | ``--domain``                        | domain of the PO file (defaults to  |
  |                                     | lower-cased project name)           |
  +-------------------------------------+-------------------------------------+
  | ``--input-file`` (``-i``)           | name of the input file              |
  +-------------------------------------+-------------------------------------+
  | ``--output-dir`` (``-d``)           | name of the output directory        |
  +-------------------------------------+-------------------------------------+
  | ``--output-file`` (``-o``)          | name of the output file             |
  +-------------------------------------+-------------------------------------+
  | ``--locale``                        | locale for the new localized string |
  +-------------------------------------+-------------------------------------+
  | ``--ignore-obsolete``               | do not include obsolete messages in |
  |                                     | the output                          |
  +-------------------------------------+-------------------------------------+
  | ``--no-fuzzy-matching`` (``-N``)    | do not use fuzzy matching           |
  +-------------------------------------+-------------------------------------+
  | ``--previous``                      | keep previous msgids of translated  |
  |                                     | messages                            |
  +-------------------------------------+-------------------------------------+

If ``output-dir`` is specified, but ``output-file`` is not, the default filename
of the output file will be::

    <output_dir>/<locale>/LC_MESSAGES/<domain>.po

If neither the ``input_file`` nor the ``locale`` option is set, this command
looks for all catalog files in the base directory that match the given domain,
and updates each of them.

These options can either be specified on the command-line, or in the
``setup.cfg`` file.