Commit 9d7f2574 authored by Jérome Perrin's avatar Jérome Perrin

Localizer: add a context manager to temporarily change language

parent 0f5ff0f6
......@@ -127,6 +127,64 @@ class TestLocalizer(ERP5TypeTestCase):
self.portal.portal_activities.process_timer(None, None)
self.assertEquals(tmp_obj.getComment(), "C'est 1€.")
def test_get_selected_language(self):
# default selected language is en
self.assertEquals('en', self.portal.Localizer.get_selected_language())
def test_translationContext(self):
self.message_catalog._messages['This is 1€.'] = PersistentMapping(
{'fr':"C'est 1€.", 'note':'',})
localizer = self.portal.Localizer
with localizer.translationContext('fr'):
self.assertEquals('fr', localizer.get_selected_language())
self.assertEquals("C'est 1€.",
self.portal.Base_translateString("This is 1€."))
# outside of this context manager we are back to english
self.assertEquals('en', localizer.get_selected_language())
self.assertEquals("This is 1€.",
self.portal.Base_translateString("This is 1€."))
def test_translationContextActivity(self):
portal = self.portal
self.message_catalog._messages['This is 1€.'] = PersistentMapping(
{'fr':"C'est 1€.", 'note':'',})
localizer = portal.Localizer
test_script = createZODBPythonScript(portal.portal_skins.custom,
'test_script', '', """
def assertEquals(a, b):
if a != b:
raise AssertionError("%r != %r" % (a, b))
localizer = context.getPortalObject().Localizer
with localizer.translationContext('fr'):
assertEquals('fr', localizer.get_selected_language())
assertEquals("C'est 1€.", context.Base_translateString("This is 1€."))
# outside of this context manager we are back to english
assertEquals('en', localizer.get_selected_language())
assertEquals("This is 1€.", context.Base_translateString("This is 1€."))
""")
# normal activity
portal.portal_activities.activate().test_script()
self.tic()
# after activity execution we are still in english
self.assertEquals('en', localizer.get_selected_language())
self.assertEquals("This is 1€.",
self.portal.Base_translateString("This is 1€."))
# execute activity with group_method
portal.portal_activities.activate(group_method_id=None).test_script()
self.tic()
# after activity execution we are still in english
self.assertEquals('en', localizer.get_selected_language())
self.assertEquals("This is 1€.",
self.portal.Base_translateString("This is 1€."))
def test_get_request(self):
# check that Localizer's get_request hack works as expected
from Products.Localizer.utils import get_request
self.assertEquals(get_request(), self.portal.REQUEST)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestLocalizer))
......
......@@ -16,6 +16,7 @@
# Import from the Standard Library
from urllib import unquote
from contextlib import contextmanager
# Import from Zope
from AccessControl import ClassSecurityInfo
......@@ -216,7 +217,10 @@ class Localizer(LanguageManager, Folder):
security.declarePublic('changeLanguage')
changeLanguageForm = LocalDTMLFile('ui/changeLanguageForm', globals())
def changeLanguage(self, lang, goto=None, expires=None):
""" """
"""Change the user language to `lang`.
This method will set a cookie and redirect to `goto` URL.
"""
request = self.REQUEST
response = request.RESPONSE
......@@ -234,6 +238,35 @@ class Localizer(LanguageManager, Folder):
response.redirect(goto)
security.declarePublic('translationContext')
@contextmanager
def translationContext(self, lang):
"""Context manager to temporarily change the current language.
"""
class ForcedLanguage:
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, lang):
self.lang = lang
def select_language(self, available_languages):
return self.lang
def set(self, lang, priority):
if lang != self.lang:
LOG('Localizer', PROBLEM,
'Cannot change language inside a translationContext', error=1)
MARKER = []
from patches import get_request # late import, as this is patched by
# unit tests
request = get_request() # Localizer always use this request internally
old_accept_language = request.get('AcceptLanguage', MARKER)
request.set('AcceptLanguage', ForcedLanguage(lang))
try:
assert self.get_selected_language() == lang
yield
finally:
request.other.pop('AcceptLanguage')
if old_accept_language is not MARKER:
request.set('AcceptLanguage', old_accept_language)
security.declarePublic('translate')
def translate(self, domain, msgid, lang=None, *args, **kw):
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment