annotate genshi/filters/i18n.py @ 1039:51515d7ffbe4 stable-0.6.x

Merge r1263 from trunk (add 'placeholder' to list of translatable attributes).
author hodgestar
date Wed, 19 Mar 2014 14:35:27 +0000
parents fa0e84724fee
children
rev   line source
531
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
1 # -*- coding: utf-8 -*-
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
2 #
897
64f04a2c5e66 Update changelog and copyright years.
cmlenz
parents: 895
diff changeset
3 # Copyright (C) 2007-2010 Edgewall Software
531
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
4 # All rights reserved.
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
5 #
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
6 # This software is licensed as described in the file COPYING, which
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
7 # you should have received as part of this distribution. The terms
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
9 #
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
10 # This software consists of voluntary contributions made by many
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
11 # individuals. For the exact contribution history, see the revision
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
9ce91447fc6b Add missing copyright header to i18n.py.
cmlenz
parents: 528
diff changeset
13
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
14 """Directives and utilities for internationalization and localization of
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
15 templates.
576
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
16
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
17 :since: version 0.4
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
18 :note: Directives support added since version 0.6
576
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
19 """
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
20
856
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
21 try:
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
22 any
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
23 except NameError:
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
24 from genshi.util import any
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
25 from gettext import NullTranslations
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
26 import os
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
27 import re
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
28 from types import FunctionType
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
29
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
30 from genshi.core import Attrs, Namespace, QName, START, END, TEXT, \
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
31 XML_NAMESPACE, _ensure, StreamEventKind
794
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
32 from genshi.template.eval import _ast
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
33 from genshi.template.base import DirectiveFactory, EXPR, SUB, _apply_directives
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
34 from genshi.template.directives import Directive, StripDirective
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
35 from genshi.template.markup import MarkupTemplate, EXEC
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
36
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
37 __all__ = ['Translator', 'extract']
501
5e7604c2d60d Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents: 493
diff changeset
38 __docformat__ = 'restructuredtext en'
5e7604c2d60d Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents: 493
diff changeset
39
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
40
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
41 I18N_NAMESPACE = Namespace('http://genshi.edgewall.org/i18n')
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
42
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
43 MSGBUF = StreamEventKind('MSGBUF')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
44 SUB_START = StreamEventKind('SUB_START')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
45 SUB_END = StreamEventKind('SUB_END')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
46
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
47 GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext', 'dgettext', 'dngettext',
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
48 'ugettext', 'ungettext')
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
49
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
50
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
51 class I18NDirective(Directive):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
52 """Simple interface for i18n directives to support messages extraction."""
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
53
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
54 def __call__(self, stream, directives, ctxt, **vars):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
55 return _apply_directives(stream, directives, ctxt, vars)
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
56
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
57
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
58 class ExtractableI18NDirective(I18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
59 """Simple interface for directives to support messages extraction."""
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
60
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
61 def extract(self, translator, stream, gettext_functions=GETTEXT_FUNCTIONS,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
62 search_text=True, comment_stack=None):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
63 raise NotImplementedError
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
64
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
65
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
66 class CommentDirective(I18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
67 """Implementation of the ``i18n:comment`` template directive which adds
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
68 translation comments.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
69
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
70 >>> tmpl = MarkupTemplate('''<html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
71 ... <p i18n:comment="As in Foo Bar">Foo</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
72 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
73 >>> translator = Translator()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
74 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
75 >>> list(translator.extract(tmpl.stream))
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
76 [(2, None, u'Foo', [u'As in Foo Bar'])]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
77 """
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
78 __slots__ = ['comment']
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
79
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
80 def __init__(self, value, template=None, namespaces=None, lineno=-1,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
81 offset=-1):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
82 Directive.__init__(self, None, template, namespaces, lineno, offset)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
83 self.comment = value
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
84
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
85
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
86 class MsgDirective(ExtractableI18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
87 r"""Implementation of the ``i18n:msg`` directive which marks inner content
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
88 as translatable. Consider the following examples:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
89
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
90 >>> tmpl = MarkupTemplate('''<html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
91 ... <div i18n:msg="">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
92 ... <p>Foo</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
93 ... <p>Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
94 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
95 ... <p i18n:msg="">Foo <em>bar</em>!</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
96 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
97
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
98 >>> translator = Translator()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
99 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
100 >>> list(translator.extract(tmpl.stream))
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
101 [(2, None, u'[1:Foo]\n [2:Bar]', []), (6, None, u'Foo [1:bar]!', [])]
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
102 >>> print(tmpl.generate().render())
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
103 <html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
104 <div><p>Foo</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
105 <p>Bar</p></div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
106 <p>Foo <em>bar</em>!</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
107 </html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
108
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
109 >>> tmpl = MarkupTemplate('''<html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
110 ... <div i18n:msg="fname, lname">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
111 ... <p>First Name: ${fname}</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
112 ... <p>Last Name: ${lname}</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
113 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
114 ... <p i18n:msg="">Foo <em>bar</em>!</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
115 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
116 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
117 >>> list(translator.extract(tmpl.stream)) #doctest: +NORMALIZE_WHITESPACE
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
118 [(2, None, u'[1:First Name: %(fname)s]\n [2:Last Name: %(lname)s]', []),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
119 (6, None, u'Foo [1:bar]!', [])]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
120
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
121 >>> tmpl = MarkupTemplate('''<html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
122 ... <div i18n:msg="fname, lname">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
123 ... <p>First Name: ${fname}</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
124 ... <p>Last Name: ${lname}</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
125 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
126 ... <p i18n:msg="">Foo <em>bar</em>!</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
127 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
128 >>> translator.setup(tmpl)
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
129 >>> print(tmpl.generate(fname='John', lname='Doe').render())
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
130 <html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
131 <div><p>First Name: John</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
132 <p>Last Name: Doe</p></div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
133 <p>Foo <em>bar</em>!</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
134 </html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
135
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
136 Starting and ending white-space is stripped of to make it simpler for
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
137 translators. Stripping it is not that important since it's on the html
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
138 source, the rendered output will remain the same.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
139 """
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
140 __slots__ = ['params', 'lineno']
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
141
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
142 def __init__(self, value, template=None, namespaces=None, lineno=-1,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
143 offset=-1):
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
144 Directive.__init__(self, None, template, namespaces, lineno, offset)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
145 self.params = [param.strip() for param in value.split(',') if param]
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
146 self.lineno = lineno
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
147
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
148 @classmethod
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
149 def attach(cls, template, stream, value, namespaces, pos):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
150 if type(value) is dict:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
151 value = value.get('params', '').strip()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
152 return super(MsgDirective, cls).attach(template, stream, value.strip(),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
153 namespaces, pos)
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
154
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
155 def __call__(self, stream, directives, ctxt, **vars):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
156 gettext = ctxt.get('_i18n.gettext')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
157 if ctxt.get('_i18n.domain'):
891
f8bcd76729a1 Removed some obsolete/unused code from the i18n filter.
cmlenz
parents: 888
diff changeset
158 dgettext = ctxt.get('_i18n.dgettext')
854
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 853
diff changeset
159 assert hasattr(dgettext, '__call__'), \
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 853
diff changeset
160 'No domain gettext function passed'
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
161 gettext = lambda msg: dgettext(ctxt.get('_i18n.domain'), msg)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
162
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
163 def _generate():
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
164 msgbuf = MessageBuffer(self)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
165 previous = stream.next()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
166 if previous[0] is START:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
167 yield previous
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
168 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
169 msgbuf.append(*previous)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
170 previous = stream.next()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
171 for kind, data, pos in stream:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
172 msgbuf.append(*previous)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
173 previous = kind, data, pos
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
174 if previous[0] is not END:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
175 msgbuf.append(*previous)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
176 previous = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
177 for event in msgbuf.translate(gettext(msgbuf.format())):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
178 yield event
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
179 if previous:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
180 yield previous
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
181
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
182 return _apply_directives(_generate(), directives, ctxt, vars)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
183
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
184 def extract(self, translator, stream, gettext_functions=GETTEXT_FUNCTIONS,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
185 search_text=True, comment_stack=None):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
186 msgbuf = MessageBuffer(self)
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
187 strip = False
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
188
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
189 stream = iter(stream)
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
190 previous = stream.next()
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
191 if previous[0] is START:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
192 for message in translator._extract_attrs(previous,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
193 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
194 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
195 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
196 previous = stream.next()
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
197 strip = True
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
198 for event in stream:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
199 if event[0] is START:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
200 for message in translator._extract_attrs(event,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
201 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
202 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
203 yield message
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
204 msgbuf.append(*previous)
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
205 previous = event
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
206 if not strip:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
207 msgbuf.append(*previous)
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
208
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
209 yield self.lineno, None, msgbuf.format(), comment_stack[-1:]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
210
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
211
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
212 class ChooseBranchDirective(I18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
213 __slots__ = ['params']
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
214
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
215 def __call__(self, stream, directives, ctxt, **vars):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
216 self.params = ctxt.get('_i18n.choose.params', [])[:]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
217 msgbuf = MessageBuffer(self)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
218 stream = _apply_directives(stream, directives, ctxt, vars)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
219
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
220 previous = stream.next()
871
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
221 if previous[0] is START:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
222 yield previous
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
223 else:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
224 msgbuf.append(*previous)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
225
871
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
226 try:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
227 previous = stream.next()
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
228 except StopIteration:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
229 # For example <i18n:singular> or <i18n:plural> directives
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
230 yield MSGBUF, (), -1 # the place holder for msgbuf output
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
231 ctxt['_i18n.choose.%s' % self.tagname] = msgbuf
871
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
232 return
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
233
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
234 for event in stream:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
235 msgbuf.append(*previous)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
236 previous = event
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
237 yield MSGBUF, (), -1 # the place holder for msgbuf output
871
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
238
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
239 if previous[0] is END:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
240 yield previous # the outer end tag
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
241 else:
b6deffd8ecce Allow the use of `i18n:singular` and `i18n:plural` as directives and not just as attributes.
palgarvio
parents: 869
diff changeset
242 msgbuf.append(*previous)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
243 ctxt['_i18n.choose.%s' % self.tagname] = msgbuf
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
244
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
245 def extract(self, translator, stream, gettext_functions=GETTEXT_FUNCTIONS,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
246 search_text=True, comment_stack=None, msgbuf=None):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
247 stream = iter(stream)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
248 previous = stream.next()
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
249
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
250 if previous[0] is START:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
251 # skip the enclosing element
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
252 for message in translator._extract_attrs(previous,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
253 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
254 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
255 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
256 previous = stream.next()
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
257
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
258 for event in stream:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
259 if previous[0] is START:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
260 for message in translator._extract_attrs(previous,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
261 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
262 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
263 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
264 msgbuf.append(*previous)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
265 previous = event
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
266
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
267 if previous[0] is not END:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
268 msgbuf.append(*previous)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
269
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
270
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
271 class SingularDirective(ChooseBranchDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
272 """Implementation of the ``i18n:singular`` directive to be used with the
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
273 ``i18n:choose`` directive."""
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
274
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
275
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
276 class PluralDirective(ChooseBranchDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
277 """Implementation of the ``i18n:plural`` directive to be used with the
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
278 ``i18n:choose`` directive."""
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
279
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
280
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
281 class ChooseDirective(ExtractableI18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
282 """Implementation of the ``i18n:choose`` directive which provides plural
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
283 internationalisation of strings.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
284
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
285 This directive requires at least one parameter, the one which evaluates to
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
286 an integer which will allow to choose the plural/singular form. If you also
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
287 have expressions inside the singular and plural version of the string you
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
288 also need to pass a name for those parameters. Consider the following
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
289 examples:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
290
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
291 >>> tmpl = MarkupTemplate('''\
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
292 <html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
293 ... <div i18n:choose="num; num">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
294 ... <p i18n:singular="">There is $num coin</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
295 ... <p i18n:plural="">There are $num coins</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
296 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
297 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
298 >>> translator = Translator()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
299 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
300 >>> list(translator.extract(tmpl.stream)) #doctest: +NORMALIZE_WHITESPACE
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
301 [(2, 'ngettext', (u'There is %(num)s coin',
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
302 u'There are %(num)s coins'), [])]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
303
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
304 >>> tmpl = MarkupTemplate('''\
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
305 <html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
306 ... <div i18n:choose="num; num">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
307 ... <p i18n:singular="">There is $num coin</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
308 ... <p i18n:plural="">There are $num coins</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
309 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
310 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
311 >>> translator.setup(tmpl)
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
312 >>> print(tmpl.generate(num=1).render())
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
313 <html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
314 <div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
315 <p>There is 1 coin</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
316 </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
317 </html>
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
318 >>> print(tmpl.generate(num=2).render())
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
319 <html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
320 <div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
321 <p>There are 2 coins</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
322 </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
323 </html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
324
888
7e85b17cc317 More i18n doc improvements.
cmlenz
parents: 882
diff changeset
325 When used as a element and not as an attribute:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
326
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
327 >>> tmpl = MarkupTemplate('''\
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
328 <html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
329 ... <i18n:choose numeral="num" params="num">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
330 ... <p i18n:singular="">There is $num coin</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
331 ... <p i18n:plural="">There are $num coins</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
332 ... </i18n:choose>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
333 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
334 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
335 >>> list(translator.extract(tmpl.stream)) #doctest: +NORMALIZE_WHITESPACE
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
336 [(2, 'ngettext', (u'There is %(num)s coin',
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
337 u'There are %(num)s coins'), [])]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
338 """
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
339 __slots__ = ['numeral', 'params', 'lineno']
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
340
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
341 def __init__(self, value, template=None, namespaces=None, lineno=-1,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
342 offset=-1):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
343 Directive.__init__(self, None, template, namespaces, lineno, offset)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
344 params = [v.strip() for v in value.split(';')]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
345 self.numeral = self._parse_expr(params.pop(0), template, lineno, offset)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
346 self.params = params and [name.strip() for name in
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
347 params[0].split(',') if name] or []
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
348 self.lineno = lineno
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
349
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
350 @classmethod
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
351 def attach(cls, template, stream, value, namespaces, pos):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
352 if type(value) is dict:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
353 numeral = value.get('numeral', '').strip()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
354 assert numeral is not '', "at least pass the numeral param"
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
355 params = [v.strip() for v in value.get('params', '').split(',')]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
356 value = '%s; ' % numeral + ', '.join(params)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
357 return super(ChooseDirective, cls).attach(template, stream, value,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
358 namespaces, pos)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
359
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
360 def __call__(self, stream, directives, ctxt, **vars):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
361 ctxt.push({'_i18n.choose.params': self.params,
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
362 '_i18n.choose.singular': None,
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
363 '_i18n.choose.plural': None})
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
364
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
365 ngettext = ctxt.get('_i18n.ngettext')
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
366 assert hasattr(ngettext, '__call__'), 'No ngettext function available'
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
367 dngettext = ctxt.get('_i18n.dngettext')
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
368 if not dngettext:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
369 dngettext = lambda d, s, p, n: ngettext(s, p, n)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
370
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
371 new_stream = []
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
372 singular_stream = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
373 singular_msgbuf = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
374 plural_stream = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
375 plural_msgbuf = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
376
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
377 numeral = self.numeral.evaluate(ctxt)
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
378 is_plural = self._is_plural(numeral, ngettext)
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
379
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
380 for event in stream:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
381 if event[0] is SUB and any(isinstance(d, ChooseBranchDirective)
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
382 for d in event[1][0]):
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
383 subdirectives, substream = event[1]
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
384
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
385 if isinstance(subdirectives[0], SingularDirective):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
386 singular_stream = list(_apply_directives(substream,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
387 subdirectives,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
388 ctxt, vars))
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
389 new_stream.append((MSGBUF, None, (None, -1, -1)))
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
390
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
391 elif isinstance(subdirectives[0], PluralDirective):
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
392 if is_plural:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
393 plural_stream = list(_apply_directives(substream,
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
394 subdirectives,
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
395 ctxt, vars))
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
396
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
397 else:
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
398 new_stream.append(event)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
399
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
400 if ctxt.get('_i18n.domain'):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
401 ngettext = lambda s, p, n: dngettext(ctxt.get('_i18n.domain'),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
402 s, p, n)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
403
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
404 singular_msgbuf = ctxt.get('_i18n.choose.singular')
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
405 if is_plural:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
406 plural_msgbuf = ctxt.get('_i18n.choose.plural')
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
407 msgbuf, choice = plural_msgbuf, plural_stream
873
4e8100e83bdd Any `py:strip` involved on `i18n:singular` or `i18n:plural` is now handled separately, ie, if one has `py:strip` on a `i18n:singular` element or directive and the plural form is the one chosen, no stripping will be performed. Refs #371.
palgarvio
parents: 872
diff changeset
408 else:
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
409 msgbuf, choice = singular_msgbuf, singular_stream
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
410 plural_msgbuf = MessageBuffer(self)
873
4e8100e83bdd Any `py:strip` involved on `i18n:singular` or `i18n:plural` is now handled separately, ie, if one has `py:strip` on a `i18n:singular` element or directive and the plural form is the one chosen, no stripping will be performed. Refs #371.
palgarvio
parents: 872
diff changeset
411
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
412 for kind, data, pos in new_stream:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
413 if kind is MSGBUF:
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
414 for event in choice:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
415 if event[0] is MSGBUF:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
416 translation = ngettext(singular_msgbuf.format(),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
417 plural_msgbuf.format(),
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
418 numeral)
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
419 for subevent in msgbuf.translate(translation):
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
420 yield subevent
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
421 else:
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
422 yield event
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
423 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
424 yield kind, data, pos
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
425
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
426 ctxt.pop()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
427
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
428 def extract(self, translator, stream, gettext_functions=GETTEXT_FUNCTIONS,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
429 search_text=True, comment_stack=None):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
430 strip = False
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
431 stream = iter(stream)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
432 previous = stream.next()
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
433
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
434 if previous[0] is START:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
435 # skip the enclosing element
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
436 for message in translator._extract_attrs(previous,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
437 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
438 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
439 yield message
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
440 previous = stream.next()
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
441 strip = True
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
442
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
443 singular_msgbuf = MessageBuffer(self)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
444 plural_msgbuf = MessageBuffer(self)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
445
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
446 for event in stream:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
447 if previous[0] is SUB:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
448 directives, substream = previous[1]
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
449 for directive in directives:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
450 if isinstance(directive, SingularDirective):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
451 for message in directive.extract(translator,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
452 substream, gettext_functions, search_text,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
453 comment_stack, msgbuf=singular_msgbuf):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
454 yield message
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
455 elif isinstance(directive, PluralDirective):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
456 for message in directive.extract(translator,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
457 substream, gettext_functions, search_text,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
458 comment_stack, msgbuf=plural_msgbuf):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
459 yield message
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
460 elif not isinstance(directive, StripDirective):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
461 singular_msgbuf.append(*previous)
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
462 plural_msgbuf.append(*previous)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
463 else:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
464 if previous[0] is START:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
465 for message in translator._extract_attrs(previous,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
466 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
467 search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
468 yield message
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
469 singular_msgbuf.append(*previous)
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
470 plural_msgbuf.append(*previous)
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
471 previous = event
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
472
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
473 if not strip:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
474 singular_msgbuf.append(*previous)
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
475 plural_msgbuf.append(*previous)
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
476
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
477 yield self.lineno, 'ngettext', \
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
478 (singular_msgbuf.format(), plural_msgbuf.format()), \
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
479 comment_stack[-1:]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
480
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
481 def _is_plural(self, numeral, ngettext):
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
482 # XXX: should we test which form was chosen like this!?!?!?
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
483 # There should be no match in any catalogue for these singular and
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
484 # plural test strings
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
485 singular = u'O\x85\xbe\xa9\xa8az\xc3?\xe6\xa1\x02n\x84\x93'
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
486 plural = u'\xcc\xfb+\xd3Pn\x9d\tT\xec\x1d\xda\x1a\x88\x00'
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
487 return ngettext(singular, plural, numeral) == plural
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
488
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
489
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
490 class DomainDirective(I18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
491 """Implementation of the ``i18n:domain`` directive which allows choosing
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
492 another i18n domain(catalog) to translate from.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
493
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
494 >>> from genshi.filters.tests.i18n import DummyTranslations
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
495 >>> tmpl = MarkupTemplate('''\
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
496 <html xmlns:i18n="http://genshi.edgewall.org/i18n">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
497 ... <p i18n:msg="">Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
498 ... <div i18n:domain="foo">
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
499 ... <p i18n:msg="">FooBar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
500 ... <p>Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
501 ... <p i18n:domain="bar" i18n:msg="">Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
502 ... <p i18n:domain="">Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
503 ... </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
504 ... <p>Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
505 ... </html>''')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
506
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
507 >>> translations = DummyTranslations({'Bar': 'Voh'})
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
508 >>> translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'})
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
509 >>> translations.add_domain('bar', {'Bar': 'bar_Bar'})
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
510 >>> translator = Translator(translations)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
511 >>> translator.setup(tmpl)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
512
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
513 >>> print(tmpl.generate().render())
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
514 <html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
515 <p>Voh</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
516 <div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
517 <p>BarFoo</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
518 <p>foo_Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
519 <p>bar_Bar</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
520 <p>Voh</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
521 </div>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
522 <p>Voh</p>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
523 </html>
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
524 """
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
525 __slots__ = ['domain']
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
526
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
527 def __init__(self, value, template=None, namespaces=None, lineno=-1,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
528 offset=-1):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
529 Directive.__init__(self, None, template, namespaces, lineno, offset)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
530 self.domain = value and value.strip() or '__DEFAULT__'
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
531
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
532 @classmethod
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
533 def attach(cls, template, stream, value, namespaces, pos):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
534 if type(value) is dict:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
535 value = value.get('name')
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
536 return super(DomainDirective, cls).attach(template, stream, value,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
537 namespaces, pos)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
538
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
539 def __call__(self, stream, directives, ctxt, **vars):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
540 ctxt.push({'_i18n.domain': self.domain})
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
541 for event in _apply_directives(stream, directives, ctxt, vars):
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
542 yield event
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
543 ctxt.pop()
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
544
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
545
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
546 class Translator(DirectiveFactory):
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
547 """Can extract and translate localizable strings from markup streams and
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
548 templates.
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
549
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
550 For example, assume the following template:
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
551
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
552 >>> tmpl = MarkupTemplate('''<html xmlns:py="http://genshi.edgewall.org/">
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
553 ... <head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
554 ... <title>Example</title>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
555 ... </head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
556 ... <body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
557 ... <h1>Example</h1>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
558 ... <p>${_("Hello, %(name)s") % dict(name=username)}</p>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
559 ... </body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
560 ... </html>''', filename='example.html')
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
561
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
562 For demonstration, we define a dummy ``gettext``-style function with a
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
563 hard-coded translation table, and pass that to the `Translator` initializer:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
564
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
565 >>> def pseudo_gettext(string):
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
566 ... return {
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
567 ... 'Example': 'Beispiel',
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
568 ... 'Hello, %(name)s': 'Hallo, %(name)s'
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
569 ... }[string]
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
570 >>> translator = Translator(pseudo_gettext)
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
571
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
572 Next, the translator needs to be prepended to any already defined filters
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
573 on the template:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
574
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
575 >>> tmpl.filters.insert(0, translator)
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
576
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
577 When generating the template output, our hard-coded translations should be
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
578 applied as expected:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
579
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
580 >>> print(tmpl.generate(username='Hans', _=pseudo_gettext))
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
581 <html>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
582 <head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
583 <title>Beispiel</title>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
584 </head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
585 <body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
586 <h1>Beispiel</h1>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
587 <p>Hallo, Hans</p>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
588 </body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
589 </html>
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
590
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
591 Note that elements defining ``xml:lang`` attributes that do not contain
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
592 variable expressions are ignored by this filter. That can be used to
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
593 exclude specific parts of a template from being extracted and translated.
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
594 """
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
595
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
596 directives = [
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
597 ('domain', DomainDirective),
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
598 ('comment', CommentDirective),
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
599 ('msg', MsgDirective),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
600 ('choose', ChooseDirective),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
601 ('singular', SingularDirective),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
602 ('plural', PluralDirective)
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
603 ]
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
604
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
605 IGNORE_TAGS = frozenset([
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
606 QName('script'), QName('http://www.w3.org/1999/xhtml}script'),
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
607 QName('style'), QName('http://www.w3.org/1999/xhtml}style')
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
608 ])
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
609 INCLUDE_ATTRS = frozenset([
1039
51515d7ffbe4 Merge r1263 from trunk (add 'placeholder' to list of translatable attributes).
hodgestar
parents: 1018
diff changeset
610 'abbr', 'alt', 'label', 'prompt', 'standby', 'summary', 'title',
51515d7ffbe4 Merge r1263 from trunk (add 'placeholder' to list of translatable attributes).
hodgestar
parents: 1018
diff changeset
611 'placeholder',
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
612 ])
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
613 NAMESPACE = I18N_NAMESPACE
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
614
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
615 def __init__(self, translate=NullTranslations(), ignore_tags=IGNORE_TAGS,
594
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
616 include_attrs=INCLUDE_ATTRS, extract_text=True):
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
617 """Initialize the translator.
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
618
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
619 :param translate: the translation function, for example ``gettext`` or
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
620 ``ugettext``.
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
621 :param ignore_tags: a set of tag names that should not be localized
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
622 :param include_attrs: a set of attribute names should be localized
594
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
623 :param extract_text: whether the content of text nodes should be
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
624 extracted, or only text in explicit ``gettext``
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
625 function calls
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
626
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
627 :note: Changed in 0.6: the `translate` parameter can now be either
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
628 a ``gettext``-style function, or an object compatible with the
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
629 ``NullTransalations`` or ``GNUTranslations`` interface
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
630 """
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
631 self.translate = translate
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
632 self.ignore_tags = ignore_tags
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
633 self.include_attrs = include_attrs
594
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
634 self.extract_text = extract_text
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
635
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
636 def __call__(self, stream, ctxt=None, translate_text=True,
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
637 translate_attrs=True):
448
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
638 """Translate any localizable strings in the given stream.
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
639
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
640 This function shouldn't be called directly. Instead, an instance of
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
641 the `Translator` class should be registered as a filter with the
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
642 `Template` or the `TemplateLoader`, or applied as a regular stream
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
643 filter. If used as a template filter, it should be inserted in front of
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
644 all the default filters.
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
645
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
646 :param stream: the markup event stream
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
647 :param ctxt: the template context (not used)
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
648 :param translate_text: whether text nodes should be translated (used
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
649 internally)
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
650 :param translate_attrs: whether attribute values should be translated
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
651 (used internally)
448
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
652 :return: the localized stream
1154f2aadb6c Add support for HTML5 doctype.
cmlenz
parents: 446
diff changeset
653 """
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
654 ignore_tags = self.ignore_tags
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
655 include_attrs = self.include_attrs
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
656 skip = 0
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
657 xml_lang = XML_NAMESPACE['lang']
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
658 if not self.extract_text:
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
659 translate_text = False
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
660 translate_attrs = False
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
661
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
662 if type(self.translate) is FunctionType:
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
663 gettext = self.translate
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
664 if ctxt:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
665 ctxt['_i18n.gettext'] = gettext
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
666 else:
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
667 gettext = self.translate.ugettext
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
668 ngettext = self.translate.ungettext
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
669 try:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
670 dgettext = self.translate.dugettext
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
671 dngettext = self.translate.dungettext
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
672 except AttributeError:
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
673 dgettext = lambda _, y: gettext(y)
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
674 dngettext = lambda _, s, p, n: ngettext(s, p, n)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
675 if ctxt:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
676 ctxt['_i18n.gettext'] = gettext
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
677 ctxt['_i18n.ngettext'] = ngettext
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
678 ctxt['_i18n.dgettext'] = dgettext
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
679 ctxt['_i18n.dngettext'] = dngettext
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
680
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
681 if ctxt and ctxt.get('_i18n.domain'):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
682 gettext = lambda msg: dgettext(ctxt.get('_i18n.domain'), msg)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
683
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
684 for kind, data, pos in stream:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
685
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
686 # skip chunks that should not be localized
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
687 if skip:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
688 if kind is START:
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
689 skip += 1
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
690 elif kind is END:
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
691 skip -= 1
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
692 yield kind, data, pos
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
693 continue
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
694
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
695 # handle different events that can be localized
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
696 if kind is START:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
697 tag, attrs = data
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
698 if tag in self.ignore_tags or \
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
699 isinstance(attrs.get(xml_lang), basestring):
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
700 skip += 1
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
701 yield kind, data, pos
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
702 continue
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
703
493
3f6582a5a4a5 Fix another bug in the translation filter: translated attributes were getting added instead of replaced.
cmlenz
parents: 485
diff changeset
704 new_attrs = []
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
705 changed = False
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
706
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
707 for name, value in attrs:
483
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
708 newval = value
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
709 if isinstance(value, basestring):
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
710 if translate_attrs and name in include_attrs:
788
31432f30a6fb Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents: 787
diff changeset
711 newval = gettext(value)
483
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
712 else:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
713 newval = list(
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
714 self(_ensure(value), ctxt, translate_text=False)
483
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
715 )
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
716 if newval != value:
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
717 value = newval
5cc92db755c5 Fix for handling of interpolated attribute values in translation filter.
cmlenz
parents: 481
diff changeset
718 changed = True
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
719 new_attrs.append((name, value))
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
720 if changed:
667
c9a084ffaee6 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
721 attrs = Attrs(new_attrs)
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
722
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
723 yield kind, (tag, attrs), pos
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
724
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
725 elif translate_text and kind is TEXT:
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
726 text = data.strip()
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
727 if text:
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
728 data = data.replace(text, unicode(gettext(text)))
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
729 yield kind, data, pos
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
730
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
731 elif kind is SUB:
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
732 directives, substream = data
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
733 current_domain = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
734 for idx, directive in enumerate(directives):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
735 # Organize directives to make everything work
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
736 # FIXME: There's got to be a better way to do this!
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
737 if isinstance(directive, DomainDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
738 # Grab current domain and update context
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
739 current_domain = directive.domain
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
740 ctxt.push({'_i18n.domain': current_domain})
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
741 # Put domain directive as the first one in order to
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
742 # update context before any other directives evaluation
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
743 directives.insert(0, directives.pop(idx))
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
744
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
745 # If this is an i18n directive, no need to translate text
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
746 # nodes here
856
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
747 is_i18n_directive = any([
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
748 isinstance(d, ExtractableI18NDirective)
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
749 for d in directives
21308bd343b8 Add a couple of fallback imports for Python 3.0.
cmlenz
parents: 854
diff changeset
750 ])
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
751 substream = list(self(substream, ctxt,
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
752 translate_text=not is_i18n_directive,
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
753 translate_attrs=translate_attrs))
790
da90cee22560 Merged the custom-directives branch back into trunk.
cmlenz
parents: 788
diff changeset
754 yield kind, (directives, substream), pos
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
755
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
756 if current_domain:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
757 ctxt.pop()
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
758 else:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
759 yield kind, data, pos
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
760
485
fb66fb3e4b49 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 483
diff changeset
761 def extract(self, stream, gettext_functions=GETTEXT_FUNCTIONS,
891
f8bcd76729a1 Removed some obsolete/unused code from the i18n filter.
cmlenz
parents: 888
diff changeset
762 search_text=True, comment_stack=None):
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
763 """Extract localizable strings from the given template stream.
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
764
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
765 For every string found, this function yields a ``(lineno, function,
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
766 message, comments)`` tuple, where:
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
767
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
768 * ``lineno`` is the number of the line on which the string was found,
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
769 * ``function`` is the name of the ``gettext`` function used (if the
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
770 string was extracted from embedded Python code), and
469
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
771 * ``message`` is the string itself (a ``unicode`` object, or a tuple
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
772 of ``unicode`` objects for functions with multiple string
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
773 arguments).
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
774 * ``comments`` is a list of comments related to the message, extracted
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
775 from ``i18n:comment`` attributes found in the markup
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
776
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
777 >>> tmpl = MarkupTemplate('''<html xmlns:py="http://genshi.edgewall.org/">
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
778 ... <head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
779 ... <title>Example</title>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
780 ... </head>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
781 ... <body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
782 ... <h1>Example</h1>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
783 ... <p>${_("Hello, %(name)s") % dict(name=username)}</p>
469
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
784 ... <p>${ngettext("You have %d item", "You have %d items", num)}</p>
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
785 ... </body>
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
786 ... </html>''', filename='example.html')
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
787 >>> for line, func, msg, comments in Translator().extract(tmpl.stream):
853
f33ecf3c319e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 850
diff changeset
788 ... print('%d, %r, %r' % (line, func, msg))
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
789 3, None, u'Example'
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
790 6, None, u'Example'
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
791 7, '_', u'Hello, %(name)s'
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
792 8, 'ngettext', (u'You have %d item', u'You have %d items', None)
469
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
793
450
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
794 :param stream: the event stream to extract strings from; can be a
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
795 regular stream or a template stream
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
796 :param gettext_functions: a sequence of function names that should be
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
797 treated as gettext-style localization
94601511cd68 Extend the I18n extraction to also yield function names if applicable.
cmlenz
parents: 448
diff changeset
798 functions
485
fb66fb3e4b49 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 483
diff changeset
799 :param search_text: whether the content of text nodes should be
fb66fb3e4b49 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 483
diff changeset
800 extracted (used internally)
469
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
801
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
802 :note: Changed in 0.4.1: For a function with multiple string arguments
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
803 (such as ``ngettext``), a single item with a tuple of strings is
2d3246f9ea54 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions.
cmlenz
parents: 467
diff changeset
804 yielded, instead an item for each string argument.
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
805 :note: Changed in 0.6: The returned tuples now include a fourth
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
806 element, which is a list of comments for the translator.
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
807 """
594
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
808 if not self.extract_text:
2bbaa61b328f Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 576
diff changeset
809 search_text = False
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
810 if comment_stack is None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
811 comment_stack = []
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
812 skip = 0
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
813
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
814 xml_lang = XML_NAMESPACE['lang']
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
815
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
816 for kind, data, pos in stream:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
817 if skip:
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
818 if kind is START:
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
819 skip += 1
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
820 if kind is END:
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
821 skip -= 1
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
822
549
7214c1bdb383 The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
823 if kind is START and not skip:
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
824 tag, attrs = data
522
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
825 if tag in self.ignore_tags or \
082535e5087c The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 501
diff changeset
826 isinstance(attrs.get(xml_lang), basestring):
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
827 skip += 1
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
828 continue
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
829
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
830 for message in self._extract_attrs((kind, data, pos),
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
831 gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
832 search_text=search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
833 yield message
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
834
549
7214c1bdb383 The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
835 elif not skip and search_text and kind is TEXT:
891
f8bcd76729a1 Removed some obsolete/unused code from the i18n filter.
cmlenz
parents: 888
diff changeset
836 text = data.strip()
f8bcd76729a1 Removed some obsolete/unused code from the i18n filter.
cmlenz
parents: 888
diff changeset
837 if text and [ch for ch in text if ch.isalpha()]:
f8bcd76729a1 Removed some obsolete/unused code from the i18n filter.
cmlenz
parents: 888
diff changeset
838 yield pos[1], None, text, comment_stack[-1:]
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
839
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
840 elif kind is EXPR or kind is EXEC:
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
841 for funcname, strings in extract_from_code(data,
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
842 gettext_functions):
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
843 # XXX: Do we need to grab i18n:comment from comment_stack ???
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
844 yield pos[1], funcname, strings, []
446
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
845
fd9c4f7a249a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
846 elif kind is SUB:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
847 directives, substream = data
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
848 in_comment = False
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
850 for idx, directive in enumerate(directives):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
851 # Do a first loop to see if there's a comment directive
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
852 # If there is update context and pop it from directives
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
853 if isinstance(directive, CommentDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
854 in_comment = True
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
855 comment_stack.append(directive.comment)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
856 if len(directives) == 1:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
857 # in case we're in the presence of something like:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
858 # <p i18n:comment="foo">Foo</p>
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
859 for message in self.extract(
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
860 substream, gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
861 search_text=search_text and not skip,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
862 comment_stack=comment_stack):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
863 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
864 directives.pop(idx)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
865 elif not isinstance(directive, I18NDirective):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
866 # Remove all other non i18n directives from the process
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
867 directives.pop(idx)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
868
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
869 if not directives and not in_comment:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
870 # Extract content if there's no directives because
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
871 # strip was pop'ed and not because comment was pop'ed.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
872 # Extraction in this case has been taken care of.
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
873 for message in self.extract(
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
874 substream, gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
875 search_text=search_text and not skip):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
876 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
877
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
878 for directive in directives:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
879 if isinstance(directive, ExtractableI18NDirective):
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
880 for message in directive.extract(self,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
881 substream, gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
882 search_text=search_text and not skip,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
883 comment_stack=comment_stack):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
884 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
885 else:
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
886 for message in self.extract(
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
887 substream, gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
888 search_text=search_text and not skip,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
889 comment_stack=comment_stack):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
890 yield message
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
891
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
892 if in_comment:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
893 comment_stack.pop()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
894
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
895 def get_directive_index(self, dir_cls):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
896 total = len(self._dir_order)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
897 if dir_cls in self._dir_order:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
898 return self._dir_order.index(dir_cls) - total
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
899 return total
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
900
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
901 def setup(self, template):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
902 """Convenience function to register the `Translator` filter and the
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
903 related directives with the given template.
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
904
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
905 :param template: a `Template` instance
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
906 """
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
907 template.filters.insert(0, self)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
908 if hasattr(template, 'add_directives'):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
909 template.add_directives(Translator.NAMESPACE, self)
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
910
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
911 def _extract_attrs(self, event, gettext_functions, search_text):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
912 for name, value in event[1][1]:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
913 if search_text and isinstance(value, basestring):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
914 if name in self.include_attrs:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
915 text = value.strip()
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
916 if text:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
917 yield event[2][1], None, text, []
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
918 else:
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
919 for message in self.extract(_ensure(value), gettext_functions,
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
920 search_text=False):
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
921 yield message
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
922
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
923
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
924 class MessageBuffer(object):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
925 """Helper class for managing internationalized mixed content.
576
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
926
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
927 :since: version 0.5
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
928 """
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
929
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
930 def __init__(self, directive=None):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
931 """Initialize the message buffer.
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
932
882
5ec88105d89c Fix regression in [1099]: templates must not have an implicit loader during message extraction.
cmlenz
parents: 873
diff changeset
933 :param directive: the directive owning the buffer
5ec88105d89c Fix regression in [1099]: templates must not have an implicit loader during message extraction.
cmlenz
parents: 873
diff changeset
934 :type directive: I18NDirective
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
935 """
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
936 # params list needs to be copied so that directives can be evaluated
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
937 # more than once
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
938 self.orig_params = self.params = directive.params[:]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
939 self.directive = directive
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
940 self.string = []
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
941 self.events = {}
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
942 self.values = {}
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
943 self.depth = 1
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
944 self.order = 1
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
945 self._prev_order = None
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
946 self.stack = [0]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
947 self.subdirectives = {}
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
948
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
949 def _add_event(self, order, event):
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
950 if order == self._prev_order:
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
951 self.events[order][-1].append(event)
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
952 else:
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
953 self._prev_order = order
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
954 self.events.setdefault(order, [])
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
955 self.events[order].append([event])
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
956
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
957 def append(self, kind, data, pos):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
958 """Append a stream event to the buffer.
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
959
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
960 :param kind: the stream event kind
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
961 :param data: the event data
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
962 :param pos: the position of the event in the source
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
963 """
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
964 if kind is SUB:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
965 # The order needs to be +1 because a new START kind event will
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
966 # happen and we we need to wrap those events into our custom kind(s)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
967 order = self.stack[-1] + 1
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
968 subdirectives, substream = data
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
969 # Store the directives that should be applied after translation
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
970 self.subdirectives.setdefault(order, []).extend(subdirectives)
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
971 self._add_event(order, (SUB_START, None, pos))
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
972 for skind, sdata, spos in substream:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
973 self.append(skind, sdata, spos)
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
974 self._add_event(order, (SUB_END, None, pos))
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
975 elif kind is TEXT:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
976 if '[' in data or ']' in data:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
977 # Quote [ and ] if it ain't us adding it, ie, if the user is
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
978 # using those chars in his templates, escape them
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
979 data = data.replace('[', '\[').replace(']', '\]')
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
980 self.string.append(data)
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
981 self._add_event(self.stack[-1], (kind, data, pos))
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
982 elif kind is EXPR:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
983 if self.params:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
984 param = self.params.pop(0)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
985 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
986 params = ', '.join(['"%s"' % p for p in self.orig_params if p])
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
987 if params:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
988 params = "(%s)" % params
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
989 raise IndexError("%d parameters%s given to 'i18n:%s' but "
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
990 "%d or more expressions used in '%s', line %s"
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
991 % (len(self.orig_params), params,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
992 self.directive.tagname,
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
993 len(self.orig_params) + 1,
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
994 os.path.basename(pos[0] or
872
21392d1af4d4 Typo correction.
palgarvio
parents: 871
diff changeset
995 'In-memory Template'),
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
996 pos[1]))
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
997 self.string.append('%%(%s)s' % param)
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
998 self._add_event(self.stack[-1], (kind, data, pos))
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
999 self.values[param] = (kind, data, pos)
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1000 else:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1001 if kind is START:
854
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 853
diff changeset
1002 self.string.append('[%d:' % self.order)
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1003 self.stack.append(self.order)
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
1004 self._add_event(self.stack[-1], (kind, data, pos))
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1005 self.depth += 1
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1006 self.order += 1
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1007 elif kind is END:
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1008 self.depth -= 1
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1009 if self.depth:
953
ea40c6ff63da Merge r1177 from trunk (support multiple stream events around tags inside translated messages -- see #404).
hodgestar
parents: 897
diff changeset
1010 self._add_event(self.stack[-1], (kind, data, pos))
854
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 853
diff changeset
1011 self.string.append(']')
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1012 self.stack.pop()
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1013
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1014 def format(self):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1015 """Return a message identifier representing the content in the
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1016 buffer.
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1017 """
854
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 853
diff changeset
1018 return ''.join(self.string).strip()
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1019
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
1020 def translate(self, string, regex=re.compile(r'%\((\w+)\)s')):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1021 """Interpolate the given message translation with the events in the
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1022 buffer and return the translated stream.
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1023
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1024 :param string: the translated message string
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1025 """
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1026 substream = None
895
79fc86fac3aa i18n: some cleanup, especially for the pluralization directives.
cmlenz
parents: 892
diff changeset
1027
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1028 def yield_parts(string):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1029 for idx, part in enumerate(regex.split(string)):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1030 if idx % 2:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1031 yield self.values[part]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1032 elif part:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1033 yield (TEXT,
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1034 part.replace('\[', '[').replace('\]', ']'),
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1035 (None, -1, -1)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1036 )
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1037
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1038 parts = parse_msg(string)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1039 parts_counter = {}
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1040 for order, string in parts:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1041 parts_counter.setdefault(order, []).append(None)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1042
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1043 while parts:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1044 order, string = parts.pop(0)
1018
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1045 events = self.events[order]
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1046 if events:
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1047 events = events.pop(0)
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1048 else:
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1049 # create a dummy empty text event so any remaining
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1050 # part of the translation can be processed.
fa0e84724fee Merge r1242 from trunk (fix handling of case where a translation has text after a closing tag).
hodgestar
parents: 953
diff changeset
1051 events = [(TEXT, "", (None, -1, -1))]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1052 parts_counter[order].pop()
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1053
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1054 for event in events:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1055 if event[0] is SUB_START:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1056 substream = []
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1057 elif event[0] is SUB_END:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1058 # Yield a substream which might have directives to be
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1059 # applied to it (after translation events)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1060 yield SUB, (self.subdirectives[order], substream), event[2]
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1061 substream = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1062 elif event[0] is TEXT:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1063 if string:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1064 for part in yield_parts(string):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1065 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1066 substream.append(part)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1067 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1068 yield part
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1069 # String handled, reset it
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1070 string = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1071 elif event[0] is START:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1072 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1073 substream.append(event)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1074 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1075 yield event
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1076 if string:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1077 for part in yield_parts(string):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1078 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1079 substream.append(part)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1080 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1081 yield part
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1082 # String handled, reset it
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1083 string = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1084 elif event[0] is END:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1085 if string:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1086 for part in yield_parts(string):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1087 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1088 substream.append(part)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1089 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1090 yield part
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1091 # String handled, reset it
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1092 string = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1093 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1094 substream.append(event)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1095 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1096 yield event
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1097 elif event[0] is EXPR:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1098 # These are handled on the strings itself
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1099 continue
775
886934df7fea Support for parameters in internationalized `i18n:msg` content. See #129.
cmlenz
parents: 762
diff changeset
1100 else:
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1101 if string:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1102 for part in yield_parts(string):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1103 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1104 substream.append(part)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1105 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1106 yield part
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1107 # String handled, reset it
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1108 string = None
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1109 if substream is not None:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1110 substream.append(event)
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1111 else:
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1112 yield event
560
7e83be231f96 Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
1113
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
1114
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1115 def parse_msg(string, regex=re.compile(r'(?:\[(\d+)\:)|(?<!\\)\]')):
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1116 """Parse a translated message using Genshi mixed content message
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1117 formatting.
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1118
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1119 >>> parse_msg("See [1:Help].")
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1120 [(0, 'See '), (1, 'Help'), (0, '.')]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1121
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1122 >>> parse_msg("See [1:our [2:Help] page] for details.")
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1123 [(0, 'See '), (1, 'our '), (2, 'Help'), (1, ' page'), (0, ' for details.')]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1124
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1125 >>> parse_msg("[2:Details] finden Sie in [1:Hilfe].")
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1126 [(2, 'Details'), (0, ' finden Sie in '), (1, 'Hilfe'), (0, '.')]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1127
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1128 >>> parse_msg("[1:] Bilder pro Seite anzeigen.")
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1129 [(1, ''), (0, ' Bilder pro Seite anzeigen.')]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1130
738
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1131 :param string: the translated message string
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1132 :return: a list of ``(order, string)`` tuples
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1133 :rtype: `list`
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1134 """
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1135 parts = []
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1136 stack = [0]
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1137 while True:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1138 mo = regex.search(string)
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1139 if not mo:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1140 break
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1141
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1142 if mo.start() or stack[-1]:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1143 parts.append((stack[-1], string[:mo.start()]))
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1144 string = string[mo.end():]
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1145
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1146 orderno = mo.group(1)
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1147 if orderno is not None:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1148 stack.append(int(orderno))
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1149 else:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1150 stack.pop()
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1151 if not stack:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1152 break
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1153
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1154 if string:
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1155 parts.append((stack[-1], string))
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1156
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1157 return parts
3b8a38fcc1ab Minor cleanup in the i18n module.
cmlenz
parents: 667
diff changeset
1158
776
ddb58d74c8ee Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents: 775
diff changeset
1159
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1160 def extract_from_code(code, gettext_functions):
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1161 """Extract strings from Python bytecode.
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1162
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1163 >>> from genshi.template.eval import Expression
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1164 >>> expr = Expression('_("Hello")')
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
1165 >>> list(extract_from_code(expr, GETTEXT_FUNCTIONS))
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1166 [('_', u'Hello')]
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1167
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1168 >>> expr = Expression('ngettext("You have %(num)s item", '
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1169 ... '"You have %(num)s items", num)')
892
a6a23b889708 i18n: Support extraction of attributes in markup embedded in ``i18n:msg`` and ``i18n:choose`` directives. See also #380.
cmlenz
parents: 891
diff changeset
1170 >>> list(extract_from_code(expr, GETTEXT_FUNCTIONS))
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1171 [('ngettext', (u'You have %(num)s item', u'You have %(num)s items', None))]
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1172
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1173 :param code: the `Code` object
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1174 :type code: `genshi.template.eval.Code`
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1175 :param gettext_functions: a sequence of function names
576
b00765a115a5 Improve docs on `Stream.select()` for #135.
cmlenz
parents: 565
diff changeset
1176 :since: version 0.5
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1177 """
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1178 def _walk(node):
794
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1179 if isinstance(node, _ast.Call) and isinstance(node.func, _ast.Name) \
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1180 and node.func.id in gettext_functions:
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1181 strings = []
600
23a4784203ae Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
1182 def _add(arg):
794
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1183 if isinstance(arg, _ast.Str) and isinstance(arg.s, basestring):
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1184 strings.append(unicode(arg.s, 'utf-8'))
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1185 elif arg:
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1186 strings.append(None)
600
23a4784203ae Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
1187 [_add(arg) for arg in node.args]
794
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1188 _add(node.starargs)
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1189 _add(node.kwargs)
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1190 if len(strings) == 1:
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1191 strings = strings[0]
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1192 else:
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1193 strings = tuple(strings)
794
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1194 yield node.func.id, strings
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1195 elif node._fields:
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1196 children = []
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1197 for field in node._fields:
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1198 child = getattr(node, field, None)
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1199 if isinstance(child, list):
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1200 for elem in child:
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1201 children.append(elem)
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1202 elif isinstance(child, _ast.AST):
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1203 children.append(child)
f9e23d472a6e Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
cmlenz
parents: 790
diff changeset
1204 for child in children:
565
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1205 for funcname, strings in _walk(child):
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1206 yield funcname, strings
53b37e4f2921 * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 561
diff changeset
1207 return _walk(code.ast)
561
21b473243e63 Move code for extracting messages from bytecode into a separate function.
cmlenz
parents: 560
diff changeset
1208
776
ddb58d74c8ee Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents: 775
diff changeset
1209
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1210 def extract(fileobj, keywords, comment_tags, options):
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1211 """Babel extraction method for Genshi templates.
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1212
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1213 :param fileobj: the file-like object the messages should be extracted from
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1214 :param keywords: a list of keywords (i.e. function names) that should be
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1215 recognized as translation functions
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1216 :param comment_tags: a list of translator tags to search for and include
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1217 in the results
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1218 :param options: a dictionary of additional options (optional)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1219 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1220 :rtype: ``iterator``
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1221 """
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1222 template_class = options.get('template_class', MarkupTemplate)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1223 if isinstance(template_class, basestring):
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1224 module, clsname = template_class.split(':', 1)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1225 template_class = getattr(__import__(module, {}, {}, [clsname]), clsname)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1226 encoding = options.get('encoding', None)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1227
596
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1228 extract_text = options.get('extract_text', True)
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1229 if isinstance(extract_text, basestring):
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1230 extract_text = extract_text.lower() in ('1', 'on', 'yes', 'true')
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1231
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1232 ignore_tags = options.get('ignore_tags', Translator.IGNORE_TAGS)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1233 if isinstance(ignore_tags, basestring):
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1234 ignore_tags = ignore_tags.split()
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1235 ignore_tags = [QName(tag) for tag in ignore_tags]
596
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1236
528
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1237 include_attrs = options.get('include_attrs', Translator.INCLUDE_ATTRS)
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1238 if isinstance(include_attrs, basestring):
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1239 include_attrs = include_attrs.split()
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1240 include_attrs = [QName(attr) for attr in include_attrs]
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1241
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1242 tmpl = template_class(fileobj, filename=getattr(fileobj, 'name', None),
24df908da22d Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
1243 encoding=encoding)
882
5ec88105d89c Fix regression in [1099]: templates must not have an implicit loader during message extraction.
cmlenz
parents: 873
diff changeset
1244 tmpl.loader = None
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1245
596
badb5e5b7bb9 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
1246 translator = Translator(None, ignore_tags, include_attrs, extract_text)
849
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1247 if hasattr(tmpl, 'add_directives'):
878c4313c7d5 Merged advanced-i18n branch back into trunk.
cmlenz
parents: 794
diff changeset
1248 tmpl.add_directives(Translator.NAMESPACE, translator)
787
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
1249 for message in translator.extract(tmpl.stream, gettext_functions=keywords):
d7366797440f Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents: 785
diff changeset
1250 yield message
Copyright (C) 2012-2017 Edgewall Software