# usage: call here to bring all translations up to date
# usage forms:
# update.py [--complete] <list of language files>
# updates all language files in the editable form (with comments); untranslated items
# are marked and decorated with the original text and the translations from the other
# languages from the given file list. Example:
# update.py spanish.txt is useful for editing the portugese translation if the s
# panish translation is up to date.
# the --complete switch adds the original texts as comments to all items, even
# those already translated.
# the --scm switch removes all comments added by previous runs so you can
# easily check in partial translations into source control.
#
# update.py --dist
# compactifies translations: strips comments and whitespace
import sys
import os
import string
# parse line for identifier/value pattern
class LanguageUpdater:
def __init__(self):
# maximal assumed length of identifier
self.maxlen = 30
self.commentAll = False
self.commentNone = False
self.autoComment = "#ORIGINAL TEXT:"
self.autoComment2 = "#TRANSLATION "
self.untranslated = "UNTRANSLATED\n"
self.outdated = "# Outdated:\n"
self.translations = {}
def ParseLine( self, line ):
stripped = line.expandtabs(4).lstrip()
if len(stripped) > 0 and stripped[0] != '#':
pair = stripped.split(" ",1)
pair[1] = pair[1].lstrip()
return pair
else:
return False
def Write( self, key, value ):
self.outfile.write( (key + " ").ljust( self.maxlen ) + value )
# fetch, print and delete translation from dictionary
def WriteFromDictionary( self, key ):
try:
# fetch translation...
trans = self.dictionary[ key ]
# print it...
self.Write( key, trans )
# and delete it.
del self.dictionary[ key ]
except KeyError:
# translation not found: note that.
if not self.commentNone:
self.Write( "#" + key, self.untranslated )
# writes dictionary to open outfile
def WriteDictionary0( self ):
for key in self.dictionary:
self.Write( key, self.dictionary[key] )
# opens file, writes dictionary
def WriteDictionary1( self, translation ):
# open outfile
self.outfile = open( translation, "w" )
# write to it in condensed form
self.WriteHeader()
self.outfile.write("# We don't maintain this file in this horrible unsorted format.\n# Execute update.py without arguments to return it into its good form.\n\n")
self.WriteDictionary0()
# close outfile
self.outfile.close()
del self.outfile
def WriteHeader( self ):
# write language the outfile is in
self.outfile.write(self.leadingComment)
self.WriteFromDictionary( "language" )
self.outfile.write("\n")
# read contents of translation into dictionary
def ReadDictionary( self, translation ):
# read translation into dictionary
self.leadingComment = ""
started = False
outfilein = open( translation, "r" )
self.dictionary = {}
self.lostcomments = {}
for line in outfilein.readlines():
# parse line for identifier/value pattern
pair = self.ParseLine( line )
# put pair into dictionary
if pair != False:
started = True
self.dictionary[pair[0]] = pair[1]
else:
if not started:
self.leadingComment += line;
else:
if not line.startswith( self.autoComment ) and not line.startswith( self.autoComment2 ) and not line.endswith( self.untranslated ) and not line == self.outdated:
self.lostcomments[line] = True
outfilein.close()
# write contents of dictionary in the order its items appear in the file base.
def WriteDictionary( self, base, translation ):
# open outfile
self.outfile = open( translation, "w" )
# open infile
infile = open( base, "r" )
self.WriteHeader()
# flag indicating whether the next item needs an extra separation line
separate = True
# flag indicating whether the last language item was translated
lastTranslated = True
# flag indicating whether the last line was empty
lastEmpty = False
# read through base file, rewriting it
for line in infile.readlines():
pair = self.ParseLine( line )
if pair == False:
# delete comment from lost comment archive
try: del self.lostcomments[ line ]
except KeyError: pass
# just print line, it is a comment or whitespace
empty = ( len(line) <= 1 )
if lastTranslated or ( not empty or not lastEmpty ):
self.outfile.write( line )
lastEmpty = empty
separate = False
else:
if not pair[0] == "include":
# write original text as a comment
lastTranslated = pair[0] in self.dictionary
if self.commentAll or ( not lastTranslated and not self.commentNone ):