Permalink
Browse files

Do not modify mapping in tstring if (partially) updating mapping in t…

…ranslator call.
  • Loading branch information...
1 parent 3afa341 commit db732442f1bc543c033358fecf9dcd6d4a4bf0f1 @wichert wichert committed Feb 3, 2012
Showing with 10 additions and 4 deletions.
  1. +7 −4 translationstring/__init__.py
  2. +3 −0 translationstring/tests/test_integration.py
View
11 translationstring/__init__.py
@@ -270,15 +270,18 @@ def Translator(translations=None, policy=None):
def translator(tstring, domain=None, mapping=None):
if not hasattr(tstring, 'interpolate'):
tstring = TranslationString(tstring, domain=domain, mapping=mapping)
+ elif mapping:
+ if tstring.mapping:
+ new_mapping = tstring.mapping.copy()
+ new_mapping.update(mapping)
+ else:
+ new_mapping = mapping
+ tstring = TranslationString(tstring, domain=domain, mapping=new_mapping)
translated = tstring
if translations is not None:
translated = policy(translations, tstring, domain)
if translated == tstring:
translated = tstring.default
- if tstring.mapping is None:
- tstring.mapping = mapping
- elif mapping is not None:
- tstring.mapping.update(mapping)
if translated and '$' in translated and tstring.mapping:
translated = tstring.interpolate(translated)
return translated
View
3 translationstring/tests/test_integration.py
@@ -91,6 +91,9 @@ def test_translator_with_interpolation_partially_overridden_in_translate(self):
result = translator(tstring, mapping={'two': 'kakkonen'})
self.assertEqual(result, 'yksi kakkonen ${three}')
+ # Make sure original mapping is not touched
+ self.assertEqual(tstring.mapping, {'one': 'yksi', 'two': 'kaksi'})
+
class PluralizerIntegrationTests(unittest.TestCase):
def _makeTranslations(self):

0 comments on commit db73244

Please sign in to comment.