3 from xml.etree import ElementTree as ET
4 from xml.etree.ElementTree import *
5 from xml.dom import minidom
8 def get_file_name( theScript, theFileName, theExtension ):
9 anExt = '.'+theExtension
11 return theScript+anExt
12 if theFileName.lower().endswith( anExt ):
15 return theFileName + anExt
20 self.check_object = None # the function to check if the object is suitable for translation
21 self.translation = None # the function to translate the object's name
22 self.sub_objects = None # the function to get sub-objects of the given object
23 self.not_translated = {}
25 def add( self, theContext, theSource, theTranslation ):
26 if len(theSource)==0 or len(theTranslation)==0:
28 if not theContext in self.data:
29 self.data[theContext] = {}
30 self.data[theContext][theSource] = theTranslation
32 def saveXML( self, theFileName, theEncoding, theDocType, theRootElem ):
33 aRoughRepr = ET.tostring( theRootElem, theEncoding )
34 aDoc = minidom.parseString( aRoughRepr )
35 aDocType = minidom.getDOMImplementation( '' ).createDocumentType( theDocType, '', '' )
36 aDoc.insertBefore( aDocType, aDoc.documentElement )
37 anXmlRepr = aDoc.toprettyxml( indent=' ' )
38 anXmlLines = anXmlRepr.split( '\n' )[1:]
39 anXmlRepr = '\n'.join( anXmlLines )
40 anExpr = re.compile( '>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL )
41 anXmlRepr = anExpr.sub( '>\g<1></', anXmlRepr )
44 aFile = open( theFileName, 'w' )
45 aFile.write( anXmlRepr )
48 def load( self, theFileName ):
49 if not os.path.isfile( theFileName ):
52 aTree = ET.parse( theFileName )
53 aRoot = aTree.getroot()
54 for aContextElem in aRoot:
55 aContext = aContextElem[0].text;
59 for aMessageElem in aContextElem[1:]:
60 aSource = aMessageElem[0].text.strip()
61 aTranslation = aMessageElem[1].text.strip()
64 self.add( aContext, aSource, aTranslation )
66 def save( self, theFileName ):
67 aRoot = Element( 'TS' )
68 for aContext, aData in self.data.iteritems():
69 aContextElem = SubElement( aRoot, 'context' )
70 aName = SubElement( aContextElem, 'name' )
72 for aSource, aTranslation in aData.iteritems():
73 aMessage = SubElement( aContextElem, 'message' )
74 aSourceElem = SubElement( aMessage, 'source' )
75 aSourceElem.text = aSource
76 aTranslationElem = SubElement( aMessage, 'translation' )
77 aTranslationElem.text = aTranslation
79 self.saveXML( theFileName, 'utf-8', 'TS', aRoot )
81 def isOK( self, theName, theObject ):
82 if theName.startswith( '__' ):
85 return self.check_object( theName, theObject )
88 def parse( self, theScript, theFileName = '' ):
90 aFileName = get_file_name( theScript, theFileName, 'ts' )
91 self.load( aFileName )
92 aModule = __import__( theScript )
93 self.parseObjects( aModule.__dict__, None, '' )
94 self.save( aFileName )
96 def parseObjects( self, theDict, theParentObj, theParentName ):
98 for aName, anObject in theDict.iteritems():
100 if not self.isOK( aName, anObject ):
104 aContext, aTranslation = self.translation( anObject, aName, theParentObj, theParentName )
109 #print "Found:", aName
110 if len( aTranslation ) > 0:
111 self.add( aContext, aName, aTranslation )
112 elif self.isOK( '', anObject ) and len( aName ) > 0:
113 #print anObject, aName
114 self.not_translated[aName] = ''
118 aSubObjects = self.sub_objects( anObject )
119 self.parseObjects( aSubObjects, anObject, aName )
121 def saveNotTranslated( self, theFileName ):
122 aFile = open( theFileName, 'w' )
123 aNotTranslated = self.not_translated.keys()
124 aNotTranslated.sort()
125 for aKey in aNotTranslated:
126 aFile.write( aKey + '\n' )