[Zope-Checkins] CVS: Zope3/lib/python/Zope/TAL - talgettext.py:1.1

Barry Warsaw barry@wooz.org
Mon, 17 Jun 2002 12:53:28 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/TAL
In directory cvs.zope.org:/tmp/cvs-serv28155/lib/python/Zope/TAL

Added Files:
	talgettext.py 
Log Message:
Really simple translations extractor for .pt files.  Needs a lot of
work, but at least it gets you going; po-mode doesn't provide a way to
add new entries. :(


=== Added File Zope3/lib/python/Zope/TAL/talgettext.py ===
#!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################

"""Program to extract internationalization markup from Page Templates.

Once you have marked up a Page Template file with i18n: namespace tags, use
this program to extract GNU gettext .po file entries.

Usage: talgettext.py [options] file
Options:
    -o file
    --output=file
        Send output to the named file, otherwise standard out is used.

    -h / --help
        Print this message and exit.
"""

import sys
import os
import getopt

from Zope.TAL.HTMLTALParser import HTMLTALParser
from Zope.TAL.TALInterpreter import TALInterpreter
from Zope.TAL.DummyEngine import DummyEngine
from Zope.TAL.ITALES import ITALESEngine


def usage(code, msg=''):
    # Python 2.1 required
    print >> sys.stderr, __doc__
    if msg:
        print >> sys.stderr, msg
    sys.exit(code)


def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:],
            'ho:',
            ['help', 'output='])
    except getopt.error, msg:
        usage(1, msg)

    outfile = None
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-o', '--output'):
            outfile = arg

    if len(args) <> 1:
        usage(1)

    # Output goes here
    if outfile:
        outfp = open(outfile, 'w')
    else:
        outfp = sys.stdout

    # We don't care about the rendered output of the .pt file
    class Devnull:
        def write(self, s):
            pass

    file = args[0]
    p = HTMLTALParser()
    p.parseFile(file)
    program, macros = p.getCode()
    engine = POEngine()
    engine.outfp = outfp
    TALInterpreter(program, macros, engine, stream=Devnull())()


class POEngine(DummyEngine):
    __implements__ = ITALESEngine

    def evaluatePathOrVar(self, expr):
        return 'who cares'

    def translate(self, domain, msgid, mapping):
        print >> self.outfp, 'msgid "%s"' % msgid
        print >> self.outfp, 'msgstr ""'
        print >> self.outfp
        

if __name__ == '__main__':
    main()