diff options
author | Maurits van Rees <maurits@vanrees.org> | 2013-09-21 22:23:53 +0200 |
---|---|---|
committer | Maurits van Rees <maurits@vanrees.org> | 2013-09-21 23:41:29 +0200 |
commit | 98cd8bbd446b209367f8353b9c7a90216e173a12 (patch) | |
tree | 7bf8fd335b82fd79246f0e977f765fd435d58cd5 | |
parent | a7cd41e87cdb6c811208f4954e9e438d718f61ad (diff) | |
download | zope-tal-98cd8bbd446b209367f8353b9c7a90216e173a12.tar.gz |
Fix possible UnicodeDecodeError in warning when msgid already exists.
This happens when comparing 'Read more…' with 'Read moreā¦'.
That should not give a warning, because they are really the same, but that would need a fix somewhere else,
in the code that gets the value of an i18n:attribute in a different way than an i18n:translate.
Added test for the proper printing of the warning.
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | src/zope/tal/talgettext.py | 4 | ||||
-rw-r--r-- | src/zope/tal/tests/test_talgettext.py | 29 |
3 files changed, 32 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 6d53682..17af10e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,7 @@ CHANGES 4.0.0a2 (unreleased) -------------------- -- Nothing changed yet. +- Fix possible UnicodeDecodeError in warning when msgid already exists. 4.0.0a1 (2013-02-15) diff --git a/src/zope/tal/talgettext.py b/src/zope/tal/talgettext.py index 80859e8..233751e 100644 --- a/src/zope/tal/talgettext.py +++ b/src/zope/tal/talgettext.py @@ -136,8 +136,8 @@ class POEngine(DummyEngine): references = '\n'.join([location[0]+':'+str(location[1]) for location in domain[msgid]]) print(("Warning: msgid '%s' in %s already exists " "with a different default (bad: %s, should be: %s)\n" - "The references for the existent value are:\n%s\n" % - (msgid, self.file+':'+str(position), msgid.default.encode('utf-8'), existing_msgid.default.encode('utf-8'), references)).encode('utf-8'), file=sys.stderr) + "The references for the existent value are:\n%s\n".encode('utf-8') % + (msgid.encode('utf-8'), self.file+':'+str(position), msgid.default.encode('utf-8'), existing_msgid.default.encode('utf-8'), references)), file=sys.stderr) domain[msgid].append((self.file, position)) return 'x' diff --git a/src/zope/tal/tests/test_talgettext.py b/src/zope/tal/tests/test_talgettext.py index d4761ff..3799297 100644 --- a/src/zope/tal/tests/test_talgettext.py +++ b/src/zope/tal/tests/test_talgettext.py @@ -46,6 +46,35 @@ class test_POEngine(unittest.TestCase): "POEngine catalog does not properly store message ids" ) + def test_translate_existing(self): + engine = POEngine() + # This tries to reproduce a big surfacing in a template of + # PloneSoftwareCenter when using the i18ndude package to + # extract translatable strings, which uses zope.tal. The + # relevant html snippet is this: + # + # <a href="#" title="Read more…" + # i18n:attributes="title label_read_more" + # tal:attributes="href release/absolute_url"> + # <span i18n:translate="label_read_more">Read more…</span> + # </a> + # + # Due to the different ways that i18n:attributes and + # i18n:translate are handled, the attribute gets passed to the + # translate method with the html entity interpreted as a + # unicode, and the i18n:translate gets passed as a simple + # string with the html entity intact. That may need a fix + # elsewhere, but at the moment it gives a warning. The very + # least we can do is make sure that this does not give a + # UnicodeDecodeError, which is what we test here. + engine.file = 'psc_release_listing.pt' + # position is position in file. + engine.translate('foo', 'domain', default=u'Read more\u2026', position=7) + # Adding the same key with the same default is fine. + engine.translate('foo', 'domain', default=u'Read more\u2026', position=13) + # Adding the same key with a different default is bad and triggers a warning. + engine.translate('foo', 'domain', default='Read still more…', position=42) + def test_dynamic_msgids(self): sample_source = """ <p i18n:translate=""> |