Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopped the TranslationStringFactory from steamrolling the domain of an ... #12

Merged
merged 1 commit into from Mar 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion translationstring/__init__.py
Expand Up @@ -128,7 +128,7 @@ def __reduce__(self):
def __getstate__(self):
return text_type(self), self.domain, self.default, self.mapping

def TranslationStringFactory(domain):
def TranslationStringFactory(factory_domain):
""" Create a factory which will generate translation strings
without requiring that each call to the factory be passed a
``domain`` value. A single argument is passed to this class'
Expand All @@ -143,6 +143,14 @@ def create(msgid, mapping=None, default=None):
""" Provided a msgid (Unicode object or :term:`translation
string`) and optionally a mapping object, and a *default
value*, return a :term:`translation string` object."""

# if we are passing in a TranslationString as the msgid, then
# use its domain
if isinstance(msgid, TranslationString):
domain = msgid.domain or factory_domain
else:
domain = factory_domain

return TranslationString(msgid, domain=domain, default=default,
mapping=mapping)
return create
Expand Down
21 changes: 21 additions & 0 deletions translationstring/tests/test__init__.py
Expand Up @@ -140,6 +140,27 @@ def test_allargs(self):
self.assertEqual(inst.mapping, 'mapping')
self.assertEqual(inst.default, 'default')

def test_msgid_is_translation_string_override_domain(self):
user_factory = self._makeOne('user')
factory = self._makeOne('budge')

wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
wrapper_inst = factory(wrapped_inst)

self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
self.assertEqual(wrapper_inst.domain, 'user')

def test_msgid_is_translation_string_override_kwarg(self):
user_factory = self._makeOne('user')
factory = self._makeOne('budge')

wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
wrapper_inst = factory(wrapped_inst, mapping={'b':1}, default='other_default')

self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
self.assertEqual(wrapper_inst.mapping, {'b':1})
self.assertEqual(wrapper_inst.default, 'other_default')


class TestChameleonTranslate(unittest.TestCase):
def _makeOne(self, translator):
Expand Down