[Zope3-checkins] CVS: Zope3/src/zope/tal - talgettext.py:1.6 talinterpreter.py:1.8
Jim Fulton
jim@zope.com
Thu, 3 Apr 2003 11:19:08 -0500
Update of /cvs-repository/Zope3/src/zope/tal
In directory cvs.zope.org:/tmp/cvs-serv26474/src/zope/tal
Modified Files:
talgettext.py talinterpreter.py
Log Message:
Changed the handling of program source translations
- The translation files for the application server (zope.app) are now
al in one place, src/zope/app/translation_files.
- Added an extraction tool, extract.py that extracts all translatable
strings from Python and zpt source files into a translation template
file, zope.pot. This template file should then be merged into
individual translation files.
To do:
- zcml extraction
- I don't think that encodings are handled correctly. The template
file certainly doesn't have the encoding set correctly.
=== Zope3/src/zope/tal/talgettext.py 1.5 => 1.6 ===
--- Zope3/src/zope/tal/talgettext.py:1.5 Wed Mar 26 14:11:30 2003
+++ Zope3/src/zope/tal/talgettext.py Thu Apr 3 11:18:38 2003
@@ -48,18 +48,52 @@
sys.exit(code)
+
+class POTALInterpreter(TALInterpreter):
+ def translate(self, msgid, i18ndict=None, obj=None):
+ # XXX is this right?
+ if i18ndict is None:
+ i18ndict = {}
+ if obj:
+ i18ndict.update(obj)
+ # XXX Mmmh, it seems that sometimes the msgid is None; is that really
+ # possible?
+ if msgid is None:
+ return None
+ # XXX We need to pass in one of context or target_language
+ return self.engine.translate(self.i18nContext.domain, msgid, i18ndict,
+ position=self.position)
+
+
class POEngine(DummyEngine):
__implements__ = ITALESEngine
- catalog = {}
- def evaluatePathOrVar(self, expr):
- return 'who cares'
+ def __init__(self, macros=None):
+ self.catalog = {}
+ DummyEngine.__init__(self, macros)
+
+ def evalaluate(*args):
+ return '' # who cares
- def translate(self, domain, msgid, mapping):
+ def evaluatePathOrVar(*args):
+ return '' # who cares
+
+ def evaluateSequence(self, expr):
+ return (0,) # dummy
+
+ def evaluateBoolean(self, expr):
+ return 1 # dummy
+
+ def translate(self, domain, msgid, mapping, position):
# assume domain and mapping are ignored; if they are not,
# unit test must be updated.
- self.catalog[msgid] = ''
+ if msgid not in self.catalog:
+ self.catalog[msgid] = []
+
+ self.catalog[msgid].append((self.file, position))
+
+ return 'x'
class UpdatePOEngine(POEngine):
"""A slightly-less braindead POEngine which supports loading an existing
@@ -70,6 +104,8 @@
self._filename = filename
self._loadFile()
+ self.base = self.catalog
+ self.catalog = {}
def __add(self, id, str, fuzzy):
"Add a non-fuzzy translation to the dictionary."
@@ -150,9 +186,11 @@
def evaluatePathOrVar(self, expr):
return 'who cares'
- def translate (self, domain, msgid, mapping):
- if (msgid not in self.catalog.keys()):
- self.catalog[msgid] = ''
+ def translate(self, domain, msgid, mapping, position):
+ if msgid not in self.base:
+ POEngine.translate(self, domain, msgid, mapping, position)
+
+ return 'x'
def main():
try:
@@ -165,12 +203,16 @@
outfile = None
engine = None
+ update_mode = False
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-o', '--output'):
outfile = arg
elif opt in ('-u', '--update'):
+ update_mode = True
+ if outfile is None:
+ outfile = arg
engine = UpdatePOEngine(filename=arg)
if not args:
@@ -189,24 +231,33 @@
# process each file specified
for filename in args:
- p = HTMLTALParser()
- p.parseFile(filename)
- program, macros = p.getCode()
- TALInterpreter(program, macros, engine, stream=Devnull())()
+ try:
+ engine.file = filename
+ p = HTMLTALParser()
+ p.parseFile(filename)
+ program, macros = p.getCode()
+ POTALInterpreter(program, macros, engine, stream=Devnull(),
+ metal=0)()
+ except: # Hee hee, I love bare excepts!
+ print 'There was an error processing', filename
+ print sys.exc_info()[1]
# Now output the keys in the engine
# write them to a file if --output is specified; otherwise use standard out
if (outfile is None):
outfile = sys.stdout
else:
- outfile = file(outfile, "w")
+ outfile = file(outfile, update_mode and "a" or "w")
msgids = engine.catalog.keys()
msgids.sort()
for msgid in msgids:
- msgstr = engine.catalog[msgid]
+ positions = engine.catalog[msgid]
+ for filename, position in positions:
+ outfile.write('#: %s:%s\n' % (filename, position[0]))
+
outfile.write('msgid "%s"\n' % msgid)
- outfile.write('msgstr "%s"\n' % msgstr)
+ outfile.write('msgstr ""\n')
outfile.write('\n')
if __name__ == '__main__':
=== Zope3/src/zope/tal/talinterpreter.py 1.7 => 1.8 ===
--- Zope3/src/zope/tal/talinterpreter.py:1.7 Mon Mar 31 14:15:55 2003
+++ Zope3/src/zope/tal/talinterpreter.py Thu Apr 3 11:18:38 2003
@@ -622,9 +622,6 @@
i18ndict = {}
if obj:
i18ndict.update(obj)
- # XXX need to fill this in with TranslationService calls. For now,
- # we'll just do simple interpolation based on a $-strings to %-strings
- # algorithm in Mailman.
if not self.i18nInterpolate:
return msgid
# XXX Mmmh, it seems that sometimes the msgid is None; is that really