annotate doc/i18n.txt @ 821:f807fb8900ca experimental-inline

inline-branch: Fix for `for` directive.
author cmlenz
date Wed, 11 Mar 2009 18:13:12 +0000
parents 9755836bb396
children fe25855324dd
rev   line source
820
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
2
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
3 =====================================
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
4 Internationalization and Localization
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
5 =====================================
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
6
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
7 Genshi provides basic supporting infrastructure for internationalizing
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
8 and localizing templates. That includes functionality for extracting localizable
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
9 strings from templates, as well as a template filter that can apply translations
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
10 to templates as they get rendered.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
11
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
12 This support is based on `gettext`_ message catalogs and the `gettext Python
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
13 module`_. The extraction process can be used from the API level, or through the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
14 front-ends implemented by the `Babel`_ project, for which Genshi provides a
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
15 plugin.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
16
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
17 .. _`gettext`: http://www.gnu.org/software/gettext/
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
18 .. _`gettext python module`: http://docs.python.org/lib/module-gettext.html
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
19 .. _`babel`: http://babel.edgewall.org/
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
20
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
21
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
22 .. contents:: Contents
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
23 :depth: 2
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
24 .. sectnum::
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
25
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
26
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
27 Basics
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
28 ======
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
29
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
30 The simplest way to internationalize and translate templates would be to wrap
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
31 all localizable strings in a ``gettext()`` function call (which is often aliased
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
32 to ``_()`` for brevity). In that case, no extra template filter is required.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
33
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
34 .. code-block:: genshi
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
35
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
36 <p>${_("Hello, world!")}</p>
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
37
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
38 However, this approach results in significant “character noise” in templates,
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
39 making them harder to read and preview.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
40
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
41 The ``genshi.filters.Translator`` filter allows you to get rid of the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
42 explicit `gettext`_ function calls, so you can continue to just write:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
43
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
44 .. code-block:: genshi
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
45
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
46 <p>Hello, world!</p>
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
47
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
48 This text will still be extracted and translated as if you had wrapped it in a
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
49 ``_()`` call.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
50
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
51 .. note:: For parameterized or pluralizable messages, you need to continue using
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
52 the appropriate ``gettext`` functions.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
53
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
54 You can control which tags should be ignored by this process; for example, it
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
55 doesn't really make sense to translate the content of the HTML
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
56 ``<script></script>`` element. Both ``<script>`` and ``<style>`` are excluded
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
57 by default.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
58
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
59 Attribute values can also be automatically translated. The default is to
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
60 consider the attributes ``abbr``, ``alt``, ``label``, ``prompt``, ``standby``,
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
61 ``summary``, and ``title``, which is a list that makes sense for HTML documents.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
62 Of course, you can tell the translator to use a different set of attribute
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
63 names, or none at all.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
64
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
65 In addition, you can control automatic translation in your templates using the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
66 ``xml:lang`` attribute. If the value of that attribute is a literal string, the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
67 contents and attributes of the element will be ignored:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
68
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
69 .. code-block:: genshi
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
70
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
71 <p xml:lang="en">Hello, world!</p>
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
72
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
73 On the other hand, if the value of the ``xml:lang`` attribute contains a Python
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
74 expression, the element contents and attributes are still considered for
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
75 automatic translation:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
76
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
77 .. code-block:: genshi
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
78
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
79 <html xml:lang="$locale">
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
80 ...
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
81 </html>
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
82
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
83
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
84 Extraction
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
85 ==========
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
86
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
87 The ``Translator`` class provides a class method called ``extract``, which is
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
88 a generator yielding all localizable strings found in a template or markup
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
89 stream. This includes both literal strings in text nodes and attribute values,
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
90 as well as strings in ``gettext()`` calls in embedded Python code. See the API
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
91 documentation for details on how to use this method directly.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
92
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
93 This functionality is integrated into the message extraction framework provided
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
94 by the `Babel`_ project. Babel provides a command-line interface as well as
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
95 commands that can be used from ``setup.py`` scripts using `Setuptools`_ or
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
96 `Distutils`_.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
97
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
98 .. _`setuptools`: http://peak.telecommunity.com/DevCenter/setuptools
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
99 .. _`distutils`: http://docs.python.org/dist/dist.html
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
100
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
101 The first thing you need to do to make Babel extract messages from Genshi
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
102 templates is to let Babel know which files are Genshi templates. This is done
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
103 using a “mapping configuration”, which can be stored in a configuration file,
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
104 or specified directly in your ``setup.py``.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
105
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
106 In a configuration file, the mapping may look like this:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
107
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
108 .. code-block:: ini
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
109
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
110 # Python souce
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
111 [python:**.py]
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
112
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
113 # Genshi templates
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
114 [genshi:**/templates/**.html]
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
115 include_attrs = title
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
116
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
117 [genshi:**/templates/**.txt]
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
118 template_class = genshi.template.TextTemplate
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
119 encoding = latin-1
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
120
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
121 Please consult the Babel documentation for details on configuration.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
122
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
123 If all goes well, running the extraction with Babel should create a POT file
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
124 containing the strings from your Genshi templates and your Python source files.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
125
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
126 .. note:: Genshi currently does not support “translator comments”, i.e. text in
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
127 template comments that would get added to the POT file. This support
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
128 may or may not be added in future versions.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
129
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
130
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
131 ---------------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
132 Configuration Options
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
133 ---------------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
134
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
135 The Genshi extraction plugin for Babel supports the following options:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
136
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
137 ``template_class``
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
138 ------------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
139 The concrete ``Template`` class that the file should be loaded with. Specify
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
140 the package/module name and the class name, separated by a colon.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
141
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
142 The default is to use ``genshi.template:MarkupTemplate``, and you'll want to
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
143 set it to ``genshi.template:TextTemplate`` for `text templates`_.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
144
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
145 .. _`text templates`: text-templates.html
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
146
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
147 ``encoding``
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
148 ------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
149 The encoding of the template file. This is only used for text templates. The
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
150 default is to assume “utf-8”.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
151
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
152 ``include_attrs``
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
153 -----------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
154 Comma-separated list of attribute names that should be considered to have
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
155 localizable values. Only used for markup templates.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
156
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
157 ``ignore_tags``
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
158 ---------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
159 Comma-separated list of tag names that should be ignored. Only used for markup
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
160 templates.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
161
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
162 ``extract_text``
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
163 ----------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
164 Whether text outside explicit ``gettext`` function calls should be extracted.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
165 By default, any text nodes not inside ignored tags, and values of attribute in
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
166 the ``include_attrs`` list are extracted. If this option is disabled, only
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
167 strings in ``gettext`` function calls are extracted.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
168
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
169 .. note:: If you disable this option, it's not necessary to add the translation
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
170 filter as described above. You only need to make sure that the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
171 template has access to the ``gettext`` functions it uses.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
172
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
173
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
174 Translation
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
175 ===========
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
176
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
177 If you have prepared MO files for use with Genshi using the appropriate tools,
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
178 you can access the message catalogs with the `gettext Python module`_. You'll
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
179 probably want to create a ``gettext.GNUTranslations`` instance, and make the
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
180 translation functions it provides available to your templates by putting them
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
181 in the template context.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
182
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
183 The ``Translator`` filter needs to be added to the filters of the template
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
184 (applying it as a stream filter will likely not have the desired effect).
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
185 Furthermore it needs to be the first filter in the list, including the internal
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
186 filters that Genshi adds itself:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
187
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
188 .. code-block:: python
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
189
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
190 from genshi.filters import Translator
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
191 from genshi.template import MarkupTemplate
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
192
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
193 template = MarkupTemplate("...")
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
194 template.filters.insert(0, Translator(translations.ugettext))
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
195
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
196 If you're using `TemplateLoader`, you should specify a callback function in
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
197 which you add the filter:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
198
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
199 .. code-block:: python
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
200
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
201 from genshi.filters import Translator
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
202 from genshi.template import TemplateLoader
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
203
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
204 def template_loaded(template):
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
205 template.filters.insert(0, Translator(translations.ugettext))
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
206
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
207 loader = TemplateLoader('templates', callback=template_loaded)
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
208 template = loader.load("...")
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
209
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
210 This approach ensures that the filter is not added everytime the template is
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
211 loaded, and thus being applied multiple times.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
212
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
213
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
214 Related Considerations
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
215 ======================
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
216
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
217 If you intend to produce an application that is fully prepared for an
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
218 international audience, there are a couple of other things to keep in mind:
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
219
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
220 -------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
221 Unicode
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
222 -------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
223
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
224 Use ``unicode`` internally, not encoded bytestrings. Only encode/decode where
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
225 data enters or exits the system. This means that your code works with characters
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
226 and not just with bytes, which is an important distinction for example when
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
227 calculating the length of a piece of text. When you need to decode/encode, it's
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
228 probably a good idea to use UTF-8.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
229
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
230 -------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
231 Date and Time
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
232 -------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
233
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
234 If your application uses datetime information that should be displayed to users
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
235 in different timezones, you should try to work with UTC (universal time)
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
236 internally. Do the conversion from and to "local time" when the data enters or
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
237 exits the system. Make use the Python `datetime`_ module and the third-party
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
238 `pytz`_ package.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
239
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
240 --------------------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
241 Formatting and Locale Data
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
242 --------------------------
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
243
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
244 Make sure you check out the functionality provided by the `Babel`_ project for
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
245 things like number and date formatting, locale display strings, etc.
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
246
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
247 .. _`datetime`: http://docs.python.org/lib/module-datetime.html
9755836bb396 Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
diff changeset
248 .. _`pytz`: http://pytz.sourceforge.net/
Copyright (C) 2012-2017 Edgewall Software