[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[OpenTAL-checkins] opental/PlacelessTranslationService GettextMess...
From: |
Fernando Lalo Martins |
Subject: |
[OpenTAL-checkins] opental/PlacelessTranslationService GettextMess... |
Date: |
Fri, 21 Feb 2003 18:29:40 -0500 |
CVSROOT: /cvsroot/opental
Module name: opental
Changes by: Fernando Lalo Martins <address@hidden> 03/02/21 18:29:40
Modified files:
PlacelessTranslationService: GettextMessageCatalog.py
Negotiator.py
PlacelessTranslationService.py
__init__.py
Added files:
PlacelessTranslationService: test_PlacelessExports.py
Log message:
exporting hooks for use in TTW code: negotiate(), translate(),
getLanguages(), getLanguageName(). Fixing many miscelany stuff we found while
doing it.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/test_PlacelessExports.py?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/GettextMessageCatalog.py.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/Negotiator.py.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/PlacelessTranslationService.py.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/__init__.py.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
Patches:
Index: opental/PlacelessTranslationService/GettextMessageCatalog.py
diff -u opental/PlacelessTranslationService/GettextMessageCatalog.py:1.4
opental/PlacelessTranslationService/GettextMessageCatalog.py:1.5
--- opental/PlacelessTranslationService/GettextMessageCatalog.py:1.4 Fri Feb
14 11:09:35 2003
+++ opental/PlacelessTranslationService/GettextMessageCatalog.py Fri Feb
21 18:29:40 2003
@@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
"""A simple implementation of a Message Catalog.
-$Id: GettextMessageCatalog.py,v 1.4 2003/02/14 16:09:35 lalo Exp $
+$Id: GettextMessageCatalog.py,v 1.5 2003/02/21 23:29:40 lalo Exp $
"""
from gettext import GNUTranslations
@@ -58,6 +58,7 @@
if self._language is None or self._domain is None:
raise ValueError, 'potfile has no metadata'
self.preferred_encodings = tro._info.get('preferred-encodings',
'').split()
+ self.name = unicode(tro._info.get('language-name', ''),
tro._charset)
self.__translation_object = tro
missing = self._path_to_file[:-1] + 'issing'
if os.access(missing, os.W_OK):
Index: opental/PlacelessTranslationService/Negotiator.py
diff -u opental/PlacelessTranslationService/Negotiator.py:1.6
opental/PlacelessTranslationService/Negotiator.py:1.7
--- opental/PlacelessTranslationService/Negotiator.py:1.6 Sun Jan 26
17:59:30 2003
+++ opental/PlacelessTranslationService/Negotiator.py Fri Feb 21 18:29:40 2003
@@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
"""
-$Id: Negotiator.py,v 1.6 2003/01/26 22:59:30 lalo Exp $
+$Id: Negotiator.py,v 1.7 2003/02/21 23:29:40 lalo Exp $
"""
_langPrefsRegistry = []
@@ -47,15 +47,28 @@
get = context.get
try:
- req_langs = get('user_language', None) or \
- get('HTTP_ACCEPT_LANGUAGE', '')
+ user_langs = get('user_language', '')
+ http_langs = get('HTTP_ACCEPT_LANGUAGE', '')
except:
from traceback import print_exc
print_exc()
self.langs = ()
return
+ if user_langs and http_langs and user_langs ==
context.cookies.get('user_language'):
+ user_langs = user_langs.split(',')
+ http_langs = http_langs.split(',')
+ for l in user_langs:
+ if l not in http_langs:
+ req_langs = user_langs + http_langs
+ break
+ else:
+ # user_langs is a subset of http_langs
+ context.RESPONSE.expireCookie('user_language', path='/')
+ req_langs = http_langs
+ else:
+ req_langs = (user_langs +','+ http_langs).split(',')
langs = []
- for lang in req_langs.split(','):
+ for lang in req_langs:
lang = lang.strip().lower().replace('_', '-')
if lang:
langs.append(lang.split(';')[0])
@@ -72,12 +85,18 @@
def getLanguage(self, langs, env):
+ langs = tuple(langs)
try:
- return env.other['_pts_negotiator_cache']
+ cache = env.other['_pts_negotiator_cache']
except KeyError:
# Store cache in request object
- env.set('_pts_negotiator_cache', self._getLanguage(langs, env))
- return env.other['_pts_negotiator_cache']
+ cache = {}
+ env.set('_pts_negotiator_cache', cache)
+ try:
+ return cache[langs]
+ except KeyError:
+ cache[langs] = self._getLanguage(langs, env)
+ return cache[langs]
def _getLanguage(self, langs, env):
envprefs = getLangPrefsMethod(env)
@@ -98,3 +117,6 @@
negotiator = Negotiator()
+
+def negotiate(langs, request):
+ return negotiator.getLanguage(langs, request)
Index: opental/PlacelessTranslationService/PlacelessTranslationService.py
diff -u opental/PlacelessTranslationService/PlacelessTranslationService.py:1.10
opental/PlacelessTranslationService/PlacelessTranslationService.py:1.11
--- opental/PlacelessTranslationService/PlacelessTranslationService.py:1.10
Fri Feb 14 15:38:55 2003
+++ opental/PlacelessTranslationService/PlacelessTranslationService.py Fri Feb
21 18:29:40 2003
@@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
"""Placeless Translation Service for providing I18n to file-based code.
-$Id: PlacelessTranslationService.py,v 1.10 2003/02/14 20:38:55 lalo Exp $
+$Id: PlacelessTranslationService.py,v 1.11 2003/02/21 23:29:40 lalo Exp $
"""
import re, zLOG
@@ -90,17 +90,29 @@
self._fallbacks = fallbacks
+ def getLanguageName(self, code):
+ for (ccode, cdomain), cnames in self._catalogs.items():
+ if ccode == code:
+ for cname in cnames:
+ cat = self._data[cname]
+ if cat.name:
+ return cat.name
+
+
def getLanguages(self, domain=None):
"""Get available languages"""
- if domain is not None:
- return [m[0] for m in self._catalogs.keys() if m[1] == domain]
- # no domain, so user wants 'em all
- langs = [m[0] for m in self._catalogs.keys()]
- # uniquify
- d = {}
- for l in langs:
- d[l] = 1
- return d.keys()
+ if domain is None:
+ # no domain, so user wants 'em all
+ langs = self._catalogs.keys()
+ # uniquify
+ d = {}
+ for l in langs:
+ d[l[0]] = 1
+ l = d.keys()
+ else:
+ l = [k[0] for k in self._catalogs.keys() if k[1] == domain]
+ l.sort()
+ return l
def translate(self, domain, msgid, mapping=None, context=None,
@@ -123,6 +135,9 @@
raise TypeError, 'No destination language'
else:
langs = [m[0] for m in self._catalogs.keys() if m[1] == domain]
+ for fallback in self._fallbacks:
+ if fallback not in langs:
+ langs.append(fallback)
target_language = negotiator.getLanguage(langs, context)
# Get the translation. Use the specified fallbacks if this fails
Index: opental/PlacelessTranslationService/__init__.py
diff -u opental/PlacelessTranslationService/__init__.py:1.4
opental/PlacelessTranslationService/__init__.py:1.5
--- opental/PlacelessTranslationService/__init__.py:1.4 Tue Feb 18 17:31:56 2003
+++ opental/PlacelessTranslationService/__init__.py Fri Feb 21 18:29:40 2003
@@ -16,10 +16,12 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
__version__ = '''
-$Id: __init__.py,v 1.4 2003/02/18 22:31:56 lalo Exp $
+$Id: __init__.py,v 1.5 2003/02/21 23:29:40 lalo Exp $
'''.strip()
+from AccessControl import ModuleSecurityInfo, allow_module, allow_class,
allow_type
from PlacelessTranslationService import PlacelessTranslationService, log
+from Negotiator import negotiate
from GettextMessageCatalog import GettextMessageCatalog
from Products.PageTemplates.GlobalTranslationService import
setGlobalTranslationService
import os, glob, zLOG, sys
@@ -31,6 +33,16 @@
translation_service = PlacelessTranslationService('default')
# set the translation service
setGlobalTranslationService(translation_service)
+
+translate = translation_service.translate
+getLanguages = translation_service.getLanguages
+getLanguageName = translation_service.getLanguageName
+
+security = ModuleSecurityInfo('Products.PlacelessTranslationService')
+security.declarePublic('negotiate')
+security.declarePublic('translate')
+security.declarePublic('getLanguages')
+security.declarePublic('getLanguageName')
# sweep the i18n directory
basepath = os.path.join(INSTANCE_HOME, 'i18n')