annotate doc/i18n.txt @ 583:61e072802b62 stable-0.4.x

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