Skip to content

Commit 62141d9

Browse files
committed
Update documentation for extraction configuration
1 parent 69d2b65 commit 62141d9

File tree

1 file changed

+123
-8
lines changed

1 file changed

+123
-8
lines changed

docs/messages.rst

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,6 @@ Genshi markup templates and text templates:
139139
[javascript: **.js]
140140
extract_messages = $._, jQuery._
141141
142-
The configuration file syntax is based on the format commonly found in ``.INI``
143-
files on Windows systems, and as supported by the ``ConfigParser`` module in
144-
the Python standard library. Section names (the strings enclosed in square
145-
brackets) specify both the name of the extraction method, and the extended glob
146-
pattern to specify the files that this extraction method should be used for,
147-
separated by a colon. The options in the sections are passed to the extraction
148-
method. Which options are available is specific to the extraction method used.
149-
150142
The extended glob patterns used in this configuration are similar to the glob
151143
patterns provided by most shells. A single asterisk (``*``) is a wildcard for
152144
any number of characters (except for the pathname component separator "/"),
@@ -155,9 +147,132 @@ two subsequent asterisk characters (``**``) can be used to make the wildcard
155147
match any directory level, so the pattern ``**.txt`` matches any file with the
156148
extension ``.txt`` in any directory.
157149

150+
Babel supports two configuration file formats: INI and TOML.
151+
152+
INI Configuration Format
153+
^^^^^^^^^^^^^^^^^^^^^^^^
154+
155+
The INI configuration file syntax is based on the format commonly found in ``.INI``
156+
files on Windows systems, and as supported by the ``ConfigParser`` module in
157+
the Python standard library. Section names (the strings enclosed in square
158+
brackets) specify both the name of the extraction method, and the extended glob
159+
pattern to specify the files that this extraction method should be used for,
160+
separated by a colon. The options in the sections are passed to the extraction
161+
method. Which options are available is specific to the extraction method used.
162+
158163
Lines that start with a ``#`` or ``;`` character are ignored and can be used
159164
for comments. Empty lines are ignored, too.
160165

166+
TOML Configuration Format
167+
^^^^^^^^^^^^^^^^^^^^^^^^^^
168+
169+
Babel also supports TOML format for configuration files, when the ``tomllib``
170+
module is available (Python 3.11+), or when the ``tomli`` package is installed
171+
(for Python versions prior to 3.11).
172+
173+
TOML provides a more structured format and is particularly useful when combined
174+
with ``pyproject.toml``.
175+
176+
The same configuration examples shown above can be written in TOML format:
177+
178+
.. code-block:: toml
179+
180+
# Extraction from Python source files
181+
[[mappings]]
182+
method = "python"
183+
pattern = "**.py"
184+
185+
# Extraction from Genshi HTML and text templates
186+
[[mappings]]
187+
method = "genshi"
188+
pattern = "**/templates/**.html"
189+
ignore_tags = "script,style"
190+
include_attrs = "alt title summary"
191+
192+
[[mappings]]
193+
method = "genshi"
194+
pattern = "**/templates/**.txt"
195+
template_class = "genshi.template:TextTemplate"
196+
encoding = "ISO-8819-15"
197+
198+
# Extraction from JavaScript files
199+
[[mappings]]
200+
method = "javascript"
201+
pattern = "**.js"
202+
extract_messages = "$._, jQuery._"
203+
204+
In TOML format, each ``[[mappings]]`` section defines a mapping. The ``method``
205+
and ``pattern`` fields are required. The ``pattern`` field can be a string or
206+
an array of strings to match multiple patterns with the same configuration.
207+
208+
If you're using ``pyproject.toml``, nest the configuration under ``[tool.babel]``:
209+
210+
.. code-block:: toml
211+
212+
[tool.babel]
213+
[[tool.babel.mappings]]
214+
method = "python"
215+
pattern = "**.py"
216+
217+
You can reference custom extractors in both formats. In TOML:
218+
219+
.. code-block:: toml
220+
221+
[extractors]
222+
custom = "mypackage.module:extract_custom"
223+
224+
[[mappings]]
225+
method = "custom"
226+
pattern = "**.ctm"
227+
some_option = "foo"
228+
229+
Common Options
230+
^^^^^^^^^^^^^^
231+
232+
In addition to extractor-specific options, the following options can be specified
233+
in any mapping section and will be merged with global settings:
234+
235+
``keywords``
236+
A list of keywords (function names) to extract messages from.
237+
This uses the same syntax as the ``--keyword`` command-line option.
238+
Keywords specified here are added to (not replacing) the default keywords or
239+
those specified via command-line.
240+
241+
In INI format, whitespace-separated: ``keywords = _ gettext ngettext:1,2 pgettext:1c,2``
242+
243+
In TOML format, use either a whitespace-separated string or an array:
244+
``keywords = "_ gettext ngettext:1,2"`` or
245+
``keywords = ["_", "gettext", "ngettext:1,2"]``
246+
247+
``add_comments``
248+
A list of comment tag prefixes to extract and include in the
249+
output. This uses the same syntax as the ``--add-comments`` command-line option.
250+
Comment tags specified here are added to those specified via command-line.
251+
252+
In INI format, whitespace-separated: ``add_comments = TRANSLATOR: NOTE:``
253+
254+
In TOML format, use either a string or an array:
255+
``add_comments = "TRANSLATOR NOTE:"`` (parsed as a single string!) or
256+
``add_comments = ["TRANSLATOR:", "NOTE:"]``
257+
258+
**Example in INI format:**
259+
260+
.. code-block:: ini
261+
262+
[python: **.py]
263+
keywords = _ _l _n:1,2
264+
add_comments = TRANSLATOR:
265+
266+
**Example in TOML format:**
267+
268+
.. code-block:: toml
269+
270+
[[mappings]]
271+
method = "python"
272+
pattern = "**.py"
273+
keywords = ["_", "_l", "_n:1,2"]
274+
add_comments = ["TRANSLATOR:"]
275+
161276
.. note:: if you're performing message extraction using the command Babel
162277
provides for integration into ``setup.py`` scripts, you can also
163278
provide this configuration in a different way, namely as a keyword

0 commit comments

Comments
 (0)