summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Howitz <mh@gocept.com>2015-11-05 08:56:13 +0100
committerMichael Howitz <mh@gocept.com>2015-11-05 08:56:13 +0100
commit3f6d53dabf6b377e5a8e9ec8e037d3668c8c2901 (patch)
treef9beaf51d65a0b535c21f4daf8ac83139ca76a01
parentc97f56dced81f5b003eb38f848eb922d619af604 (diff)
downloadzope-i18n-3f6d53dabf6b377e5a8e9ec8e037d3668c8c2901.tar.gz
Make interpolate working recursively:
If the mapping has a value which is a zope.i18nmessageid.Message itself, it gets interpolated, too. Version 3.5.0 introduced this behaviour for translations, porting it to interpolation here.
-rw-r--r--CHANGES.rst8
-rw-r--r--setup.py2
-rw-r--r--src/zope/i18n/__init__.py13
3 files changed, 17 insertions, 6 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index efa2018..54ae92e 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -2,11 +2,11 @@
CHANGES
=======
-4.0.2 (unreleased)
---------------------
-
-- TBD
+4.1.0 (unreleased)
+------------------
+- ``interpolate()`` now works recursively, if the mapping has a value which is
+ a ``zope.i18nmessageid.Message`` itself.
4.0.1 (2015-06-05)
--------------------
diff --git a/setup.py b/setup.py
index 80f34db..973c0da 100644
--- a/setup.py
+++ b/setup.py
@@ -42,7 +42,7 @@ def alltests():
setup(
name='zope.i18n',
- version='4.0.2.dev0',
+ version='4.1.0.dev0',
author='Zope Foundation and Contributors',
author_email='zope-dev@zope.org',
description='Zope Internationalization Support',
diff --git a/src/zope/i18n/__init__.py b/src/zope/i18n/__init__.py
index 6cb8443..7377ecc 100644
--- a/src/zope/i18n/__init__.py
+++ b/src/zope/i18n/__init__.py
@@ -160,11 +160,22 @@ def interpolate(text, mapping=None):
>>> interpolate(_u("This is ${name}"))
u'This is ${name}'
+
+ If a mapping value is a message id itself it is interpolated, too:
+
+ >>> from zope.i18nmessageid import Message
+ >>> interpolate(_u("This is $meta."),
+ ... mapping={'meta': Message(_u("$name $version"),
+ ... mapping=mapping)})
+ u'This is Zope 3.'
"""
def replace(match):
whole, param1, param2 = match.groups()
- return unicode(mapping.get(param1 or param2, whole))
+ value = mapping.get(param1 or param2, whole)
+ if isinstance(value, Message):
+ value = interpolate(value, value.mapping)
+ return unicode(value)
if not text or not mapping:
return text