annotate doc/catalogs.txt @ 42:cf94e70a77f3

Syntax highlighting for the docs.
author cmlenz
date Wed, 06 Jun 2007 11:02:48 +0000
parents 2cada72b40ae
children b6497cfd06d6
rev   line source
4
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
2
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
3 =============================
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
4 Working with Message Catalogs
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
5 =============================
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
6
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
7 .. contents:: Contents
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
8 :depth: 2
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
9 .. sectnum::
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
10
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
11
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
12 Introduction
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
13 ============
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
14
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
15 The ``gettext`` translation system enables you to mark any strings used in your
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
16 application as subject to localization, by wrapping them in functions such as
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
17 ``gettext(str)`` and ``ngettext(singular, plural, num)``. For brevity, the
42
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
18 ``gettext`` function is often aliased to ``_(str)``, so you can write:
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
19
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
20 .. code-block:: python
4
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
21
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
22 print _("Hello")
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
23
42
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
24 instead of just:
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
25
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
26 .. code-block:: python
4
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
27
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
28 print "Hello"
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
29
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
30 to make the string "Hello" localizable.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
31
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
32 Message catalogs are collections of translations for such localizable messages
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
33 used in an application. They are commonly stored in PO (Portable Object) and MO
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
34 (Machine Object) files, the formats of which are defined by the GNU `gettext`_
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
35 tools and the GNU `translation project`_.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
36
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
37 .. _`gettext`: http://www.gnu.org/software/gettext/
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
38 .. _`translation project`: http://sourceforge.net/projects/translation
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
39
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
40 The general procedure for building message catalogs looks something like this:
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
41
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
42 * use a tool (such as ``xgettext``) to extract localizable strings from the
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
43 code base and write them to a POT (PO Template) file.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
44 * make a copy of the POT file for a specific locale (for example, "en_US")
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
45 and start translating the messages
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
46 * use a tool such as ``msgfmt`` to compile the locale PO file into an binary
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
47 MO file
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
48 * later, when code changes make it necessary to update the translations, you
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
49 regenerate the POT file and merge the changes into the various
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
50 locale-specific PO files, for example using ``msgmerge``
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
51
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
52 Python provides the `gettext module`_ as part of the standard library, which
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
53 enables applications to work with appropriately generated MO files.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
54
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
55 .. _`gettext module`: http://docs.python.org/lib/module-gettext.html
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
56
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
57 As ``gettext`` provides a solid and well supported foundation for translating
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
58 application messages, Babel does not reinvent the wheel, but rather reuses this
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
59 infrastructure, and makes it easier to build message catalogs for Python
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
60 applications.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
61
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
62
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
63 Message Extraction
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
64 ==================
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
65
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
66 Babel provides functionality similar to that of the ``xgettext`` program,
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
67 except that only extraction from Python source files is built-in, while support
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
68 for other file formats can be added using a simple extension mechanism.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
69
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
70 (TODO: more)
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
71
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
72
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
73 --------------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
74 Writing Extraction Methods
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
75 --------------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
76
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
77 (TODO: write)
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
78
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
79 ---------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
80 ``setup.py`` Commands
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
81 ---------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
82
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
83 (TODO: overview)
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
84
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
85 See `Distutils/Setuptools Integration <setup.html>`_ for more information.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
86
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
87 -------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
88 Command-line script
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
89 -------------------
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
90
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
91 (TODO: overview)
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
92
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
93 See `Command-Line Interface <cmdline.html>`_ for more information.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
94
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
95
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
96 Extended ``Translations`` Class
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
97 ===============================
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
98
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
99 Many web-based applications are composed of a variety of different components
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
100 (possibly using some kind of plugin system), and some of those components may
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
101 provide their own message catalogs that need to be integrated into the larger
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
102 system.
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
103
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
104 To support this usage pattern, Babel provides a ``Translations`` class that is
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
105 derived from the ``GNUTranslations`` class in the ``gettext`` module. This
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
106 class adds a ``merge()`` method that takes another ``Translations`` instance,
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
107 and merges its contents into the catalog::
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
108
42
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
109 .. code-block:: python
cf94e70a77f3 Syntax highlighting for the docs.
cmlenz
parents: 4
diff changeset
110
4
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
111 translations = Translations.load('main')
2cada72b40ae Forgot to check in the doc directory.
cmlenz
parents:
diff changeset
112 translations.merge(Translations.load('plugin1'))
Copyright (C) 2012-2017 Edgewall Software