Exemplo n.º 1
0
def convertAll(folder, dont_use_default=False, always_ask=False):
    converter = i18nUtil()
    currentfile = None
    filein = None
    fileout = None
    try:
        for root, dirs, files in os.walk(folder):
            if 'CVS' in dirs:
                dirs.remove('CVS')
            if '.svn' in dirs:
                dirs.remove('.svn')
            for file in files:
                currentfile = join(root, file)
                if re.search(re_python_file, file):
                    print ">> Internationalization of '%s' <<" % (currentfile)
                    tmpfile = currentfile + '.bak'
                    if exists(tmpfile):
                        print "\tBackup file found, skipping!"
                    else:
                        copyfile(currentfile, tmpfile)
                        filein = open(tmpfile, 'r')
                        fileout = open(currentfile, 'w')
                        converter.convertPY(filein, fileout, True, False,
                                            always_ask, dont_use_default)
                        fileout.close()
                        filein.close()
                elif re.search(re_tpl_file, file):
                    print ">> Internationalization of '%s' <<" % (currentfile)
                    tmpfile = currentfile + '.bak'
                    if exists(tmpfile):
                        print "\tBackup file found, skipping"
                    else:
                        copyfile(currentfile, tmpfile)
                        filein = open(tmpfile, 'r')
                        fileout = open(currentfile, 'w')
                        converter.convertTPL(filein, fileout, True, False,
                                             always_ask, dont_use_default)
                        fileout.close()
                        filein.close()
    except KeyboardInterrupt, e:
        if filein:
            filein.close()
        if fileout:
            fileout.close()
        if currentfile != None:
            tmpname = currentfile + '.bak'
            if exists(tmpname):
                tmpfile = open(tmpname, 'r')
                torestore = open(currentfile, 'w')
                copyfileobj(tmpfile, torestore)
                tmpfile.close()
                torestore.close()
                os.remove(tmpname)
Exemplo n.º 2
0
def convertAll(folder, dont_use_default=False, always_ask=False):
    converter = i18nUtil()
    currentfile = None
    filein = None
    fileout = None
    try:
        for root, dirs, files in os.walk(folder):
            if 'CVS' in dirs:
                dirs.remove('CVS')
            if '.svn' in dirs:
                dirs.remove('.svn')
            for file in files:
                currentfile = join(root, file)
                if re.search(re_python_file,file):
                    print ">> Internationalization of '%s' <<" % (currentfile)
                    tmpfile = currentfile + '.bak'
                    if exists(tmpfile):
                        print "\tBackup file found, skipping!"
                    else:
                        copyfile(currentfile, tmpfile)
                        filein = open(tmpfile, 'r')
                        fileout = open(currentfile, 'w')
                        converter.convertPY(filein, fileout, True,False,always_ask,dont_use_default)
                        fileout.close()
                        filein.close()
                elif re.search(re_tpl_file, file):
                    print ">> Internationalization of '%s' <<" % (currentfile)
                    tmpfile = currentfile + '.bak'
                    if exists(tmpfile):
                        print "\tBackup file found, skipping"
                    else:
                        copyfile(currentfile, tmpfile)
                        filein = open(tmpfile, 'r')
                        fileout = open(currentfile, 'w')
                        converter.convertTPL(filein, fileout, True,False, always_ask, dont_use_default)
                        fileout.close()
                        filein.close()
    except KeyboardInterrupt, e:
        if filein:
            filein.close()
        if fileout:
            fileout.close()
        if currentfile != None:
            tmpname = currentfile + '.bak'
            if exists(tmpname):
                tmpfile = open(tmpname,'r')
                torestore = open(currentfile, 'w')
                copyfileobj(tmpfile, torestore)
                tmpfile.close()
                torestore.close()
                os.remove(tmpname)
def extract_from_files():
    """Extract translatable strings from the list of files read from
    potfiles_filename.  The files specified there are relative to
    dirname.
    """

    potfilepath = getPotFilePath()
    if os.path.exists(potfilepath):
        choice = chooseOneOf("File: '%s' exists do you want it to be overwritten?" % os.path.basename(potfilepath), ['y','Y','n','N'],False)
        if choice in ['n', 'N']:
            return

    # This list will contain all code files to examine
    file_list=[]
    for root_folder in list_folders_to_translate:
        root_folder.strip()
        if not root_folder or root_folder.startswith('#'):
            continue
        root_folder = root_folder.rstrip('\n')

        #Lists all Python and TPL files in MaKaC directory (given by MaKaCConfig)
        folder=root_folder
        for root, dirs, files, in os.walk(folder):
            # Don't recurse into CVS dirs (nor .svn dirs later perhaps?)
            if 'CVS' in dirs:
                dirs.remove('CVS')
            if '.svn' in dirs:
                dirs.remove('.svn')
            for file in files:
                if file[0] == '#':
                    pass
                elif file[-4:] == '.tpl' or file[-3:] == '.py':
                    file_list.append(os.path.join(root,file))

    # Extract messages from all files and fill db:
    db = {}
    util = i18nUtil()
    cwd = os.getcwd()
    for filename in file_list:
        if len(cwd) < len(filename) and os.path.normcase(cwd) == os.path.normcase(filename[:len(cwd)]):
            shortfilename = filename[len(cwd) + 1:]
        else:
            shortfilename = filename
        print ">> Processing '%s' <<" % (shortfilename)
        file = open(filename, 'r')
        strAndNb = []
        if filename[-3:] == '.py':
            strAndNb = util.getI18nStringsPY(file)
        else:
            strAndNb = util.getI18nStringsTPL(file)
    
        for (string, linenumber) in strAndNb:
            ref = '%s:%d' % (shortfilename, linenumber)

            # Discard withespace, as it is not taken into account later in the po file
            string = _ws_re.sub(' ', string.strip())

            # Format of db: ['word1':[ref1, ref2, ...],'word2':[ref1, ref2, ...],...]
            references = db.setdefault(string, [])
            references.append(ref)

    # Print .po header:
    potfile = open(potfilepath, 'w')
    potfile.write(r'''# $Id: i18n_extract_from_source.py,v 1.10 2008/08/04 13:13:56 pvogt Exp $
#
# This file is part of Indico.
# Copyright (C) 2007 CERN.
#
# Indico 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; either version 2 of the
# License, or (at your option) any later version.
#
# Indico 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.
msgid ""
msgstr ""
"Project-Id-Version: Indico 0.7\n"
"POT-Creation-Date: 2005-11-22 11:20+0100\n"
"PO-Revision-Date: 2005-11-22 11:20+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <*****@*****.**>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
''')    

    # Print db content in .po file:
    for original, refs in db.items():
        # Print all references to occurences of this string
        for ref in refs:
            occurence="#: %s" % ref
            potfile.write("\n" + occurence)

        original_message='msgid "%s"' % quote(original)
        potfile.write("\n" + original_message)
        translated_message = 'msgstr ""'
        potfile.write("\n" + translated_message + "\n")

    print "Wrote '%s'" % potfilepath
Exemplo n.º 4
0
#python -u

import sys
from i18nlib import i18nUtil

i = i18nUtil()
for arg in sys.argv[1:]:
    f = open(arg,'r')
    print i.getI18nStringsPY(f)
    f.close()

if len(sys.argv) == 1:
    print "Please specify at least one file where to look for i18n'ed strings"
Exemplo n.º 5
0
# python -u

import sys
from i18nlib import i18nUtil

i = i18nUtil()
for arg in sys.argv[1:]:
    f = open(arg, "r")
    print i.getI18nStringsPY(f)
    f.close()

if len(sys.argv) == 1:
    print "Please specify at least one file where to look for i18n'ed strings"