Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support %-formatting of translation strings.

  • Loading branch information...
commit 0ca63509b1713b05bb84a01ce33e4a6629a0ddda 1 parent 463e9c9
@wichert wichert authored
View
2  CHANGES.txt
@@ -4,7 +4,7 @@ translationstring
Next release
------------
-- No changes yet.
+- Support formatting of translation strings with %-operator.
0.4 (09-22-2011)
----------------
View
9 docs/tstrings.rst
@@ -59,6 +59,15 @@ replacement marker itself:
from translationstring import TranslationString
ts = TranslationString('Add ${number}', mapping={'number':1})
+You can also create a new translation string instance with a mapping
+using the standard python %-operator:
+
+.. code-block:: python
+ :linenos:
+
+ from translationstring import TranslationString
+ ts = TranslationString('Add ${number}') % {'number': 1}
+
You may interpolate a translation string with a mapping:
.. code-block:: python
View
17 translationstring/__init__.py
@@ -77,6 +77,23 @@ def __new__(self, msgid, domain=None, default=None, mapping=None):
self.mapping = mapping
return self
+ def __mod__(self, options):
+ """Create a new TranslationString instance with an updated mapping.
+ This makes it possible to use the standard python %-style string
+ formatting with translatable strings. Only dictionary
+ arguments are supported.
+ """
+ if not isinstance(options, dict):
+ raise ValueError(
+ 'Can only interpolate translationstring '
+ 'with dictionaries.')
+ if self.mapping:
+ mapping = self.mapping.copy()
+ mapping.update(options)
+ else:
+ mapping = options.copy()
+ return TranslationString(self, mapping=mapping)
+
def interpolate(self, translated=None):
""" Interpolate the value ``translated`` which is assumed to
be a Unicode object containing zero or more *replacement
View
23 translationstring/tests/test__init__.py
@@ -42,6 +42,29 @@ def test_allargs(self):
self.assertEqual(inst.mapping, 'mapping')
self.assertEqual(inst.domain, 'domain')
+ def test_format_no_exising_mapping(self):
+ inst = self._makeOne('msgid', domain='domain', default='default')
+ mapping = {'foo': 'bar'}
+ new_inst = inst % mapping
+ self.assertTrue(new_inst is not inst)
+ self.assertEqual(new_inst, 'msgid')
+ self.assertEqual(new_inst.default, 'default')
+ self.assertEqual(new_inst.domain, 'domain')
+ self.assertEqual(new_inst.mapping, mapping)
+ # Make sure a copy is made to prevent changes in the original
+ # dictionary from modifying our mapping.
+ self.assertTrue(new_inst.mapping is not mapping)
+
+ def test_format_extend_existing_mapping(self):
+ inst = self._makeOne('msgid', domain='domain', default='default',
+ mapping={'one': '1', 'two': '3'})
+ new_inst = inst % {'two': '2', 'three': '3'}
+ self.assertEqual(
+ new_inst.mapping,
+ {'one': '1', 'two': '2', 'three': '3'})
+ # Make sure original is not changed
+ self.assertEqual(inst.mapping, {'one': '1', 'two': '3'})
+
def test_interpolate_substitution(self):
mapping = {"name": "Zope", "version": 3}
inst = self._makeOne('This is $name version ${version}.',
Please sign in to comment.
Something went wrong with that request. Please try again.