Attachment 'xrc_i18n.py'

Download

   1 #! /usr/bin/env python
   2 import codecs
   3 import os
   4 import shutil
   5 import sys
   6 from xml.etree import ElementTree
   7 
   8 import msgfmt
   9 
  10 __version__ = '0.1'
  11 
  12 header = """
  13 msgid ""
  14 msgstr ""
  15 "Project-Id-Version: PACKAGE VERSION\\n"
  16 "POT-Creation-Date: Fri Feb 18 16:51:55 2005\\n"
  17 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
  18 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
  19 "Language-Team: LANGUAGE <LL@li.org>\\n"
  20 "MIME-Version: 1.0\\n"
  21 "Content-Type: text/plain; charset=CHARSET\\n"
  22 "Content-Transfer-Encoding: ENCODING\\n"
  23 "Generated-By: xrc_i18n.py\\n"
  24 
  25 """
  26 
  27 def extractIds(filename):
  28     result = []
  29     
  30     tree = ElementTree.parse(filename).getroot()
  31     
  32     tags = [
  33         './/{http://www.wxwidgets.org/wxxrc}label',
  34         './/{http://www.wxwidgets.org/wxxrc}title'
  35     ]
  36     
  37     for tag in tags:
  38         for elm in tree.findall(tag):
  39             if elm.text not in result:
  40                 result.append(elm.text)
  41     return result
  42 
  43 def makePOT(msgIds, filename):
  44     makeBackup(filename)
  45     
  46     of = file(filename, 'w')
  47     of.write(header)
  48     for id in msgIds:
  49         of.write ('msgid "%s"\n' % id)
  50         of.write ('msgstr ""\n\n')
  51 
  52     of.close()
  53     
  54     
  55 def syncPO(msgIds, filename):
  56     content = {}
  57     ifn = codecs.open(filename, 'r')
  58     for line in ifn:
  59         if line[0:5] == 'msgid':
  60             kstr = line.find('"')
  61             kend = line.rfind('"')
  62             key = line[ kstr + 1 : kend]
  63         elif line [0:5] == 'msgst':
  64             mstr = line.find('"')
  65             mend = line.rfind('"')
  66             msg = line[ mstr + 1 : mend]
  67             
  68             content[key] = msg
  69             
  70     ifn.close()
  71     
  72     makeBackup(filename)
  73     
  74     of = codecs.open(filename, 'w')
  75     of.write(header)
  76     
  77     for id in msgIds:
  78         of.write ('msgid "%s"\n' % id)
  79         if content.has_key(id):
  80             of.write ('msgstr "%s"\n\n' % content[id])
  81         else:
  82             of.write ('msgstr ""\n\n')
  83     of.close()
  84     
  85 def makeMO(poPath):
  86     moPath = poPath[ 0 : poPath.rfind('.')] + '.mo'
  87     msgfmt.make(poPath, moPath)
  88                 
  89 def getPOs(path):
  90     out = []
  91     for item in os.listdir(path):
  92         fullpath = os.path.join(path, item)
  93         if os.path.isdir(fullpath):
  94             res = getPOs(fullpath)
  95             out += res
  96         elif os.path.isfile(fullpath) and item[item.rfind('.') + 1 :] == 'po':    
  97             out.append(fullpath)
  98     return out
  99             
 100 def makeBackup(filename):             
 101     id = 1
 102     while True:
 103         nfilename = '%s.%d.bak' % (filename, id)
 104         if not os.path.exists(nfilename):
 105             shutil.copy(filename, nfilename)
 106             break;
 107         id += 1
 108 
 109 def usage():
 110     print 'python xrc_i18n.py xrc_filename localepath'
 111 
 112 def main():
 113     if len (sys.argv) < 3:
 114         usage()
 115         sys.exit(1)
 116     
 117     xrcpath = sys.argv[1]
 118     localepath = sys.argv[2]
 119     
 120     if not os.path.isfile(xrcpath):
 121         print 'File %s does not exist' % xrcpath
 122         usage()
 123         sys.exit(1)
 124     
 125     if not os.path.isdir(localepath):
 126         print 'Problem with directory %s' % localepath
 127         usage()
 128         sys.exit(1)
 129     
 130     potpath = os.path.join(localepath, ".".join( xrcpath.split('.')[:-1] + ['pot'] ))
 131     
 132     print 'Extracting messages from XRC file'
 133     msgIds = extractIds(xrcpath)
 134     print ' - Done'
 135     print 'Creating POT file'
 136     makePOT(msgIds, potpath)
 137     print ' - Done'
 138     pos = getPOs(localepath)
 139     print 'Syncing PO files'
 140     for fn in pos:
 141         syncPO(msgIds, fn)
 142     print ' - Done'
 143     print 'Compiling MO files'
 144     for fn in pos:
 145         makeMO(fn)
 146     print ' - Done'
 147 
 148 if __name__ == '__main__':
 149     main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:03, 3.6 KB) [[attachment:xrc_i18n.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.