[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[OpenTAL-checkins] opental/PlacelessTranslationService PlacelessTr...
From: |
Fernando Lalo Martins |
Subject: |
[OpenTAL-checkins] opental/PlacelessTranslationService PlacelessTr... |
Date: |
Sun, 26 Jan 2003 18:17:08 -0500 |
CVSROOT: /cvsroot/opental
Module name: opental
Changes by: Fernando Lalo Martins <address@hidden> 03/01/26 18:17:08
Modified files:
PlacelessTranslationService: PlacelessTranslationService.py
__init__.py
Removed files:
PlacelessTranslationService: SimpleTranslationService.py
Log message:
simplify vigorously: removing unused SimpleTranslationService class
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/PlacelessTranslationService.py.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/opental/opental/PlacelessTranslationService/__init__.py.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
Patches:
Index: opental/PlacelessTranslationService/PlacelessTranslationService.py
diff -u opental/PlacelessTranslationService/PlacelessTranslationService.py:1.6
opental/PlacelessTranslationService/PlacelessTranslationService.py:1.7
--- opental/PlacelessTranslationService/PlacelessTranslationService.py:1.6
Sun Jan 26 17:59:30 2003
+++ opental/PlacelessTranslationService/PlacelessTranslationService.py Sun Jan
26 18:17:07 2003
@@ -17,11 +17,27 @@
# 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.6 2003/01/26 22:59:30 lalo Exp $
+$Id: PlacelessTranslationService.py,v 1.7 2003/01/26 23:17:07 lalo Exp $
"""
+import re, zLOG
+from types import DictType, StringType, UnicodeType
from Negotiator import negotiator
-from SimpleTranslationService import SimpleTranslationService
+from Domain import Domain
+from pax import XML
+
+def log(msg, severity=zLOG.INFO, detail='', error=None):
+ zLOG.LOG('PlacelessTranslationService', severity, msg, detail, error)
+
+def map_get(map, name):
+ return map.get(name)
+
+# Setting up some regular expressions for finding interpolation variables in
+# the text.
+NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*"
+_interp_regex = re.compile(r'(?<!\$)(\$(?:%(n)s|{%(n)s}))' %({'n': NAME_RE}))
+_get_var_regex = re.compile(r'%(n)s' %({'n': NAME_RE}))
+
# The configure.zcml file should specify a list of fallback languages for the
# site. If a particular catalog for a negotiated language is not available,
@@ -34,7 +50,7 @@
LANGUAGE_FALLBACKS = ['en']
-class PlacelessTranslationService(SimpleTranslationService):
+class PlacelessTranslationService:
def __init__(self, default_domain='global', fallbacks=None):
# XXX We haven't specified that ITranslationServices have a default
@@ -55,7 +71,7 @@
key = (http_language, domain)
mc = self._catalogs.setdefault(key, [])
mc.append(catalog_name)
- print 'adding catalog for domain %s, language %s' % (domain, language)
+ log('adding catalog for domain %s, language %s' % (domain, language))
def addCatalog(self, catalog):
self._data[catalog.getIdentifier()] = catalog
@@ -130,6 +146,50 @@
# Now we need to do the interpolation
text = self.interpolate(text, mapping)
return text
+
+ def getDomain(self, domain):
+ """
+ """
+ return Domain(domain, self)
+
+ def interpolate(self, text, mapping):
+ try:
+ """Insert the data passed from mapping into the text"""
+
+ # If the mapping does not exist, make a "raw translation" without
+ # interpolation.
+ if mapping is None or type(text) not in (StringType, UnicodeType):
+ # silly wabbit!
+ return text
+
+ get = map_get
+ try:
+ mapping.get('')
+ except AttributeError:
+ get = getattr
+
+ # Find all the spots we want to substitute
+ to_replace = _interp_regex.findall(text)
+
+ # Now substitute with the variables in mapping
+ for string in to_replace:
+ var = _get_var_regex.findall(string)[0]
+ value = get(mapping, var)
+ if callable(value):
+ value = value()
+ if value is None:
+ value = string
+ if type(value) not in (StringType, UnicodeType):
+ # FIXME: we shouldn't do this. We should instead
+ # return a list. But i'm not sure about how to use
+ # the regex to split the text.
+ value = XML(value)
+ text = text.replace(string, value)
+
+ return text
+ except:
+ import traceback
+ traceback.print_exc()
#
############################################################
Index: opental/PlacelessTranslationService/__init__.py
diff -u opental/PlacelessTranslationService/__init__.py:1.2
opental/PlacelessTranslationService/__init__.py:1.3
--- opental/PlacelessTranslationService/__init__.py:1.2 Thu Jan 16 18:15:43 2003
+++ opental/PlacelessTranslationService/__init__.py Sun Jan 26 18:17:07 2003
@@ -16,10 +16,10 @@
# 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.2 2003/01/16 23:15:43 lalo Exp $
+$Id: __init__.py,v 1.3 2003/01/26 23:17:07 lalo Exp $
'''.strip()
-from PlacelessTranslationService import PlacelessTranslationService
+from PlacelessTranslationService import PlacelessTranslationService, log
from GettextMessageCatalog import GettextMessageCatalog
from Products.PageTemplates.GlobalTranslationService import
setGlobalTranslationService
import os, glob, zLOG, sys
@@ -35,16 +35,14 @@
if os.path.isdir(basepath):
names = glob.glob(os.path.join(basepath, '*.mo'))
if not names:
- print 'no translations found!'
+ log('no translations found!', zLOG.PROBLEM)
for name in names:
try:
translation_service.addCatalog(GettextMessageCatalog(name))
except ValueError:
- zLOG.LOG('AltPTi18n', zLOG.PROBLEM, 'Message Catalog has no
metadata',
- name, sys.exc_info())
+ log('Message Catalog has no metadata', zLOG.PROBLEM, name,
sys.exc_info())
except:
- zLOG.LOG('AltPTi18n', zLOG.PROBLEM, 'Message Catalog has errors',
- name, sys.exc_info())
+ log('Message Catalog has errors', zLOG.PROBLEM, name,
sys.exc_info())
def initialize(context):
pass