From dc54dee622bf9ff95a59530423ac5caa01868373 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 18 Dec 2008 14:01:59 -0700 Subject: Started work on per-request gettext setup --- ipalib/request.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ipalib/request.py (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py new file mode 100644 index 00000000..3e4b2798 --- /dev/null +++ b/ipalib/request.py @@ -0,0 +1,48 @@ +# Authors: +# Rob Crittenden +# Jason Gerard DeRose +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty contextrmation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2 only +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +Per-request thread-local data. +""" + +import threading +import locale +import gettext +from constants import OVERRIDE_ERROR + + +# Thread-local storage of most per-request contextrmation +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( + OVERRIDE_ERROR % ('context.languages', context.languages, languages) + ) + if len(languages) == 0: + languages = locale.getdefaultlocale()[:1] + context.languages = languages + assert type(context.languages) is tuple -- cgit 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 From 6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 17:29:11 -0700 Subject: Cleaned up Env.__setattr__() and Env.__setitem__() a bit updated their unit tests --- ipalib/request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index 545ebc54..ea028239 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -34,8 +34,8 @@ context = threading.local() def set_languages(*languages): if hasattr(context, 'languages'): - raise StandardError( - OVERRIDE_ERROR % ('context.languages', context.languages, languages) + raise StandardError(OVERRIDE_ERROR % + ('context', 'languages', context.languages, languages) ) if len(languages) == 0: languages = locale.getdefaultlocale()[:1] -- cgit From 0d3ddef93b0a72b824297e5504e435a4427f14bd Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sat, 3 Jan 2009 02:35:36 -0700 Subject: Started fleshing out reoganization of errors in errors.py (with gettext support) --- ipalib/request.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index ea028239..5a07d4db 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -32,6 +32,12 @@ from constants import OVERRIDE_ERROR context = threading.local() +def _(message): + if hasattr(context, 'gettext'): + return context.gettext(message) + return message.decode('UTF-8') + + def set_languages(*languages): if hasattr(context, 'languages'): raise StandardError(OVERRIDE_ERROR % -- cgit From c081ce5460018634fb30249ead2168ebf3a79044 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sat, 3 Jan 2009 22:03:37 -0700 Subject: request.create_translation() now sets context.ugettext and context.ungettext --- ipalib/request.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index 5a07d4db..f5400b75 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -50,7 +50,7 @@ def set_languages(*languages): def create_translation(domain, localedir, *languages): - if hasattr(context, 'gettext') or hasattr(context, 'ngettext'): + if hasattr(context, 'ugettext') or hasattr(context, 'ungettext'): raise StandardError( 'create_translation() already called in thread %r' % threading.currentThread().getName() @@ -59,5 +59,5 @@ def create_translation(domain, localedir, *languages): translation = gettext.translation(domain, localedir=localedir, languages=context.languages, fallback=True ) - context.gettext = translation.ugettext - context.ngettext = translation.ungettext + context.ugettext = translation.ugettext + context.ungettext = translation.ungettext -- cgit From c161784973fdedb146a4087d8692b157214c4db0 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sun, 4 Jan 2009 00:46:21 -0700 Subject: Added request.ugettext() and request.ungettext() functions; added corresponding unit tests --- ipalib/request.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index f5400b75..6ad7ad35 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -32,12 +32,20 @@ from constants import OVERRIDE_ERROR context = threading.local() -def _(message): - if hasattr(context, 'gettext'): - return context.gettext(message) +def ugettext(message): + if hasattr(context, 'ugettext'): + return context.ugettext(message) return message.decode('UTF-8') +def ungettext(singular, plural, n): + if hasattr(context, 'ungettext'): + return context.ungettext(singular, plural, n) + if n == 1: + return singular.decode('UTF-8') + return plural.decode('UTF-8') + + def set_languages(*languages): if hasattr(context, 'languages'): raise StandardError(OVERRIDE_ERROR % -- cgit