3 from xml.etree import ElementTree as ET
4 from xml.etree.ElementTree import *
5 from xml.dom import minidom
7 def get_file_name( theScript, theFileName, theExtension ):
8 anExt = '.'+theExtension
10 return theScript+anExt
11 if theFileName.lower().endswith( anExt ):
14 return theFileName + anExt
19 self.check_object = None # the function to check if the object is suitable for translation
20 self.translation = None # the function to translate the object's name
21 self.sub_objects = None # the function to get sub-objects of the given object
22 self.not_translated = {}
24 def add( self, theContext, theSource, theTranslation ):
25 if len(theSource)==0 or len(theTranslation)==0:
27 if not theContext in self.data:
28 self.data[theContext] = {}
29 self.data[theContext][theSource] = theTranslation
31 def saveXML( self, theFileName, theEncoding, theDocType, theRootElem ):
32 aRoughRepr = ET.tostring( theRootElem, theEncoding )
33 aDoc = minidom.parseString( aRoughRepr )
34 aDocType = minidom.getDOMImplementation( '' ).createDocumentType( theDocType, '', '' )
35 aDoc.insertBefore( aDocType, aDoc.documentElement )
36 anXmlRepr = aDoc.toprettyxml( indent=' ' )
37 anXmlLines = anXmlRepr.split( '\n' )[1:]
38 anXmlRepr = '\n'.join( anXmlLines )
40 aFile = open( theFileName, 'w' )
41 aFile.write( anXmlRepr )
44 def load( self, theFileName ):
45 if not os.path.isfile( theFileName ):
48 aTree = ET.parse( theFileName )
49 aRoot = aTree.getroot()
50 for aContextElem in aRoot:
51 aContext = aContextElem[0].text;
55 for aMessageElem in aContextElem[1:]:
56 aSource = aMessageElem[0].text.strip()
57 aTranslation = aMessageElem[1].text.strip()
60 self.add( aContext, aSource, aTranslation )
62 def save( self, theFileName ):
63 aRoot = Element( 'TS' )
64 for aContext, aData in self.data.iteritems():
65 aContextElem = SubElement( aRoot, 'context' )
66 aName = SubElement( aContextElem, 'name' )
68 for aSource, aTranslation in aData.iteritems():
69 aMessage = SubElement( aContextElem, 'message' )
70 aSourceElem = SubElement( aMessage, 'source' )
71 aSourceElem.text = aSource
72 aTranslationElem = SubElement( aMessage, 'translation' )
73 aTranslationElem.text = aTranslation
75 self.saveXML( theFileName, 'utf-8', 'TS', aRoot )
77 def isOK( self, theName, theObject ):
78 if theName.startswith( '__' ):
81 return self.check_object( theName, theObject )
84 def parse( self, theScript, theFileName = '' ):
86 aFileName = get_file_name( theScript, theFileName, 'ts' )
87 self.load( aFileName )
88 aModule = __import__( theScript )
89 self.parseObjects( aModule.__dict__, None, '' )
90 self.save( aFileName )
92 def parseObjects( self, theDict, theParentObj, theParentName ):
94 for aName, anObject in theDict.iteritems():
96 if not self.isOK( aName, anObject ):
100 aContext, aTranslation = self.translation( anObject, aName, theParentObj, theParentName )
105 #print "Found:", aName
106 if len( aTranslation ) > 0:
107 self.add( aContext, aName, aTranslation )
108 elif self.isOK( '', anObject ) and len( aName ) > 0:
109 #print anObject, aName
110 self.not_translated[aName] = ''
114 aSubObjects = self.sub_objects( anObject )
115 self.parseObjects( aSubObjects, anObject, aName )
117 def saveNotTranslated( self, theFileName ):
118 aFile = open( theFileName, 'w' )
119 aNotTranslated = self.not_translated.keys()
120 aNotTranslated.sort()
121 for aKey in aNotTranslated:
122 aFile.write( aKey + '\n' )