Skip to content

Commit

Permalink
1.7.1dev: merge [17794] from 1.6-stable (fix for #13745)
Browse files Browse the repository at this point in the history
git-svn-id: http://trac.edgewall.org/intertrac/log:/trunk@17795 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed May 5, 2024
2 parents fcbc20e + 41ebf3e commit da2db36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions trac/util/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ def domain_functions(domain, *symbols):
def wrapdomain(symbol):
if symbol == 'N_':
return _noop
if symbol not in _functions:
raise KeyError(symbol)
return lambda *args, **kw: _functions[symbol](domain, *args, **kw)
return [wrapdomain(s) for s in symbols]

Expand Down Expand Up @@ -455,4 +457,7 @@ def s_gettext(msgid, **kwargs):
'gettext': s_gettext,
'ngettext': ngettext,
'tag_': tag_,
'tagn_': tagn_,
'dtgettext': dtgettext,
'dtngettext': dtngettext,
}
5 changes: 4 additions & 1 deletion trac/web/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,13 +1477,16 @@ def prepare_template(self, req, filename, data, text=False, domain=None):
(see `populate_data`)
:param text: in text mode (``True``) XML/HTML auto-escape of
variable expansion is disabled.
:param domain: domain to be adapted to the translation helper functions
:rtype: a pair of Jinja2 `Template` and a `dict`.
"""
template = self.load_template(filename, text)
if domain:
symbols = list(translation.functions)
symbols = [s for s in translation.functions
if s not in ('dgettext', 'dngettext', 'dtgettext',
'dtngettext')]
domain_functions = translation.domain_functions(domain, symbols)
data.update(dict(zip(symbols, domain_functions)))
data = self.populate_data(req, data)
Expand Down
39 changes: 39 additions & 0 deletions trac/web/tests/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,45 @@ def fn():
b'common/css/blahblah.css", "text/css");', content)
self.assertIn(b' var blahblah=42;', content)

def test_render_template_i18n_domain(self):
"""Regression test for #13745"""

template = textwrap.dedent("""\
${_("anonymous")}
${gettext("Admin")}
${ngettext("%(num)d byte", "%(num)d bytes", num=num)}
${tag_("You may use %(wikiformatting)s here.",
wikiformatting=tag.b('WikiFormatting'))}
${tagn_("%(value)s added", "%(value)s added", len(values),
value=values|join(' '))}
${dgettext("messages", "Timeline")}
${dngettext("messages", "%(num)d day", "%(num)d days", num)}
${dtgettext("messages", "Quickjump to %(name)s",
name=tag.b('WikiStart'))}
${dtngettext("messages", "%(value)s removed", "%(value)s removed",
len(values), value=values|join(' '))}
""")
filename = 'ticket13745.html'
filepath = os.path.join(self.env.templates_dir, filename)
create_file(filepath, template)

def test(domain=None):
req = MockRequest(self.env)
data = {'num': 42, 'values': ['foo', 'bar']}
metadata = {'iterable': False}
if domain:
metadata['domain'] = domain
content = str(self.chrome.render_template(req, filename, data,
metadata), 'utf-8')
self.assertEqual([
'anonymous', 'Admin', '42 bytes',
'You may use <b>WikiFormatting</b> here.', 'foo bar added',
'Timeline', '42 days', 'Quickjump to <b>WikiStart</b>',
'foo bar removed'], content.splitlines())

test(domain=None)
test(domain='messages')


def test_suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit da2db36

Please sign in to comment.