From 9a69adeef001ddd0c55513271cf02eedc0a9aef8 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 18 Dec 2008 16:58:48 -0700 Subject: Added request.create_translation() function and corresponding unit tests --- ipalib/request.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index 3e4b2798..545ebc54 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -28,15 +28,10 @@ import gettext from constants import OVERRIDE_ERROR -# Thread-local storage of most per-request contextrmation +# Thread-local storage of most per-request information context = threading.local() -# Thread-local storage of gettext.Translations instances (one per gettext -# domain): -translations = threading.local() - - def set_languages(*languages): if hasattr(context, 'languages'): raise StandardError( @@ -46,3 +41,17 @@ def set_languages(*languages): languages = locale.getdefaultlocale()[:1] context.languages = languages assert type(context.languages) is tuple + + +def create_translation(domain, localedir, *languages): + if hasattr(context, 'gettext') or hasattr(context, 'ngettext'): + raise StandardError( + 'create_translation() already called in thread %r' % + threading.currentThread().getName() + ) + set_languages(*languages) + translation = gettext.translation(domain, + localedir=localedir, languages=context.languages, fallback=True + ) + context.gettext = translation.ugettext + context.ngettext = translation.ungettext -- cgit