annotate 0.9.x/doc/messages.txt @ 272:cec8c26302bd stable

Merged [295:296] via svnmerge from [source:trunk].
author cmlenz
date Wed, 22 Aug 2007 11:38:02 +0000
parents 5b7d3f9f7d74
children
rev   line source
263
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
2
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
3 =============================
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
4 Working with Message Catalogs
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
5 =============================
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
6
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
7 .. contents:: Contents
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
8 :depth: 3
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
9 .. sectnum::
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
10
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
11
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
12 Introduction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
13 ============
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
14
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
15 The ``gettext`` translation system enables you to mark any strings used in your
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
16 application as subject to localization, by wrapping them in functions such as
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
17 ``gettext(str)`` and ``ngettext(singular, plural, num)``. For brevity, the
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
18 ``gettext`` function is often aliased to ``_(str)``, so you can write:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
19
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
20 .. code-block:: python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
21
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
22 print _("Hello")
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
23
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
24 instead of just:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
25
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
26 .. code-block:: python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
27
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
28 print "Hello"
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
29
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
30 to make the string "Hello" localizable.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
31
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
32 Message catalogs are collections of translations for such localizable messages
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
33 used in an application. They are commonly stored in PO (Portable Object) and MO
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
34 (Machine Object) files, the formats of which are defined by the GNU `gettext`_
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
35 tools and the GNU `translation project`_.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
36
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
37 .. _`gettext`: http://www.gnu.org/software/gettext/
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
38 .. _`translation project`: http://sourceforge.net/projects/translation
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
39
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
40 The general procedure for building message catalogs looks something like this:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
41
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
42 * use a tool (such as ``xgettext``) to extract localizable strings from the
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
43 code base and write them to a POT (PO Template) file.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
44 * make a copy of the POT file for a specific locale (for example, "en_US")
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
45 and start translating the messages
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
46 * use a tool such as ``msgfmt`` to compile the locale PO file into an binary
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
47 MO file
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
48 * later, when code changes make it necessary to update the translations, you
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
49 regenerate the POT file and merge the changes into the various
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
50 locale-specific PO files, for example using ``msgmerge``
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
51
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
52 Python provides the `gettext module`_ as part of the standard library, which
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
53 enables applications to work with appropriately generated MO files.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
54
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
55 .. _`gettext module`: http://docs.python.org/lib/module-gettext.html
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
56
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
57 As ``gettext`` provides a solid and well supported foundation for translating
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
58 application messages, Babel does not reinvent the wheel, but rather reuses this
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
59 infrastructure, and makes it easier to build message catalogs for Python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
60 applications.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
61
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
62
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
63 Message Extraction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
64 ==================
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
65
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
66 Babel provides functionality similar to that of the ``xgettext`` program,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
67 except that only extraction from Python source files is built-in, while support
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
68 for other file formats can be added using a simple extension mechanism.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
69
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
70 Unlike ``xgettext``, which is usually invoked once for every file, the routines
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
71 for message extraction in Babel operate on directories. While the per-file
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
72 approach of ``xgettext`` works nicely with projects using a ``Makefile``,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
73 Python projects rarely use ``make``, and thus a different mechanism is needed
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
74 for extracting messages from the heterogeneous collection of source files that
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
75 many Python projects are composed of.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
76
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
77 When message extraction is based on directories instead of individual files,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
78 there needs to be a way to configure which files should be treated in which
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
79 manner. For example, while many projects may contain ``.html`` files, some of
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
80 those files may be static HTML files that don't contain localizable message,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
81 while others may be `Django`_ templates, and still others may contain `Genshi`_
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
82 markup templates. Some projects may even mix HTML files for different templates
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
83 languages (for whatever reason). Therefore the way in which messages are
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
84 extracted from source files can not only depend on the file extension, but
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
85 needs to be controllable in a precise manner.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
86
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
87 .. _`Django`: http://www.djangoproject.com/
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
88 .. _`Genshi`: http://genshi.edgewall.org/
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
89
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
90 Babel accepts a configuration file to specify this mapping of files to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
91 extraction methods, which is described below.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
92
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
93
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
94 .. _`frontends`:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
95
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
96 ----------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
97 Front-Ends
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
98 ----------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
99
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
100 Babel provides two different front-ends to access its functionality for working
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
101 with message catalogs:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
102
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
103 * A `Command-line interface <cmdline.html>`_, and
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
104 * `Integration with distutils/setuptools <setup.html>`_
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
105
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
106 Which one you choose depends on the nature of your project. For most modern
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
107 Python projects, the distutils/setuptools integration is probably more
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
108 convenient.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
109
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
110
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
111 .. _`mapping`:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
112
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
113 -------------------------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
114 Extraction Method Mapping and Configuration
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
115 -------------------------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
116
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
117 The mapping of extraction methods to files in Babel is done via a configuration
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
118 file. This file maps extended glob patterns to the names of the extraction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
119 methods, and can also set various options for each pattern (which options are
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
120 available depends on the specific extraction method).
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
121
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
122 For example, the following configuration adds extraction of messages from both
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
123 Genshi markup templates and text templates:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
124
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
125 .. code-block:: ini
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
126
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
127 # Extraction from Python source files
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
128
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
129 [python: **.py]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
130
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
131 # Extraction from Genshi HTML and text templates
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
132
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
133 [genshi: **/templates/**.html]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
134 ignore_tags = script,style
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
135 include_attrs = alt title summary
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
136
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
137 [genshi: **/templates/**.txt]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
138 template_class = genshi.template:TextTemplate
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
139 encoding = ISO-8819-15
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
140
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
141 The configuration file syntax is based on the format commonly found in ``.INI``
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
142 files on Windows systems, and as supported by the ``ConfigParser`` module in
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
143 the Python standard library. Section names (the strings enclosed in square
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
144 brackets) specify both the name of the extraction method, and the extended glob
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
145 pattern to specify the files that this extraction method should be used for,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
146 separated by a colon. The options in the sections are passed to the extraction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
147 method. Which options are available is specific to the extraction method used.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
148
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
149 The extended glob patterns used in this configuration are similar to the glob
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
150 patterns provided by most shells. A single asterisk (``*``) is a wildcard for
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
151 any number of characters (except for the pathname component separator "/"),
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
152 while a question mark (``?``) only matches a single character. In addition,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
153 two subsequent asterisk characters (``**``) can be used to make the wildcard
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
154 match any directory level, so the pattern ``**.txt`` matches any file with the
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
155 extension ``.txt`` in any directory.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
156
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
157 Lines that start with a ``#`` or ``;`` character are ignored and can be used
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
158 for comments. Empty lines are ignored, too.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
159
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
160 .. note:: if you're performing message extraction using the command Babel
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
161 provides for integration into ``setup.py`` scripts, you can also
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
162 provide this configuration in a different way, namely as a keyword
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
163 argument to the ``setup()`` function. See `Distutils/Setuptools
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
164 Integration`_ for more information.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
165
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
166 .. _`distutils/setuptools integration`: setup.html
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
167
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
168
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
169 Default Extraction Methods
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
170 --------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
171
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
172 Babel comes with only two builtin extractors: ``python`` (which extracts
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
173 messages from Python source files) and ``ignore`` (which extracts nothing).
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
174
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
175 The ``python`` extractor is by default mapped to the glob pattern ``**.py``,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
176 meaning it'll be applied to all files with the ``.py`` extension in any
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
177 directory. If you specify your own mapping configuration, this default mapping
272
cec8c26302bd Merged [295:296] via svnmerge from [source:trunk].
cmlenz
parents: 263
diff changeset
178 is discarded, so you need to explicitly add it to your mapping (as shown in the
cec8c26302bd Merged [295:296] via svnmerge from [source:trunk].
cmlenz
parents: 263
diff changeset
179 example above.)
263
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
180
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
181
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
182 .. _`referencing extraction methods`:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
183
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
184 Referencing Extraction Methods
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
185 ------------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
186
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
187 To be able to use short extraction method names such as “genshi”, you need to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
188 have `pkg_resources`_ installed, and the package implementing that extraction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
189 method needs to have been installed with its meta data (the `egg-info`_).
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
190
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
191 If this is not possible for some reason, you need to map the short names to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
192 fully qualified function names in an extract section in the mapping
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
193 configuration. For example:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
194
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
195 .. code-block:: ini
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
196
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
197 # Some custom extraction method
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
198
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
199 [extractors]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
200 custom = mypackage.module:extract_custom
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
201
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
202 [custom: **.ctm]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
203 some_option = foo
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
204
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
205 Note that the builtin extraction methods ``python`` and ``ignore`` are available
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
206 by default, even if `pkg_resources`_ is not installed. You should never need to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
207 explicitly define them in the ``[extractors]`` section.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
208
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
209 .. _`egg-info`: http://peak.telecommunity.com/DevCenter/PythonEggs
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
210 .. _`pkg_resources`: http://peak.telecommunity.com/DevCenter/PkgResources
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
211
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
212
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
213 --------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
214 Writing Extraction Methods
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
215 --------------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
216
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
217 Adding new methods for extracting localizable methods is easy. First, you'll
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
218 need to implement a function that complies with the following interface:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
219
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
220 .. code-block:: python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
221
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
222 def extract_xxx(fileobj, keywords, comment_tags, options):
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
223 """Extract messages from XXX files.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
224
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
225 :param fileobj: the file-like object the messages should be extracted
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
226 from
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
227 :param keywords: a list of keywords (i.e. function names) that should
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
228 be recognized as translation functions
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
229 :param comment_tags: a list of translator tags to search for and
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
230 include in the results
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
231 :param options: a dictionary of additional options (optional)
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
232 :return: an iterator over ``(lineno, funcname, message, comments)``
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
233 tuples
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
234 :rtype: ``iterator``
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
235 """
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
236
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
237 .. note:: Any strings in the tuples produced by this function must be either
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
238 ``unicode`` objects, or ``str`` objects using plain ASCII characters.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
239 That means that if sources contain strings using other encodings, it
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
240 is the job of the extractor implementation to do the decoding to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
241 ``unicode`` objects.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
242
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
243 Next, you should register that function as an entry point. This requires your
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
244 ``setup.py`` script to use `setuptools`_, and your package to be installed with
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
245 the necessary metadata. If that's taken care of, add something like the
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
246 following to your ``setup.py`` script:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
247
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
248 .. code-block:: python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
249
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
250 def setup(...
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
251
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
252 entry_points = """
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
253 [babel.extractors]
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
254 xxx = your.package:extract_xxx
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
255 """,
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
256
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
257 That is, add your extraction method to the entry point group
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
258 ``babel.extractors``, where the name of the entry point is the name that people
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
259 will use to reference the extraction method, and the value being the module and
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
260 the name of the function (separated by a colon) implementing the actual
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
261 extraction.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
262
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
263 .. note:: As shown in `Referencing Extraction Methods`_, declaring an entry
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
264 point is not strictly required, as users can still reference the
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
265 extraction function directly. But whenever possible, the entry point
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
266 should be declared to make configuration more convenient.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
267
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
268 .. _`setuptools`: http://peak.telecommunity.com/DevCenter/setuptools
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
269
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
270
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
271 -------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
272 Translator Comments
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
273 -------------------
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
274
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
275 First of all what are comments tags. Comments tags are excerpts of text to
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
276 search for in comments, only comments, right before the `python gettext`_
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
277 calls, as shown on the following example:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
278
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
279 .. _`python gettext`: http://docs.python.org/lib/module-gettext.html
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
280
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
281 .. code-block:: python
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
282
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
283 # NOTE: This is a comment about `Foo Bar`
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
284 _('Foo Bar')
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
285
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
286 The comments tag for the above example would be ``NOTE:``, and the translator
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
287 comment for that tag would be ``This is a comment about `Foo Bar```.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
288
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
289 The resulting output in the catalog template would be something like::
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
290
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
291 #. This is a comment about `Foo Bar`
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
292 #: main.py:2
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
293 msgid "Foo Bar"
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
294 msgstr ""
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
295
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
296 Now, you might ask, why would I need that?
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
297
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
298 Consider this simple case; you have a menu item called “manual”. You know what
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
299 it means, but when the translator sees this they will wonder did you mean:
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
300
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
301 1. a document or help manual, or
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
302 2. a manual process?
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
303
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
304 This is the simplest case where a translation comment such as
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
305 “The installation manual” helps to clarify the situation and makes a translator
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
306 more productive.
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
307
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
308 .. note:: Whether translator comments can be extracted depends on the extraction
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
309 method in use. The Python extractor provided by Babel does implement
5b7d3f9f7d74 Create branch for 0.9.x maintenance.
cmlenz
parents:
diff changeset
310 this feature, but others may not.
Copyright (C) 2012-2017 Edgewall Software