From 3f6d53dabf6b377e5a8e9ec8e037d3668c8c2901 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Thu, 5 Nov 2015 08:56:13 +0100 Subject: [PATCH] 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. --- CHANGES.rst | 8 ++++---- setup.py | 2 +- src/zope/i18n/__init__.py | 13 ++++++++++++- 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