2 # -*- coding: utf-8 -*-
4 from BeautifulSoup import BeautifulSoup
8 arg 2 : the locale field which you wanna extract
12 DESIGN_ID = "Design ID"
15 def findindexof( word, tds ):
16 for i, td in enumerate(tds):
18 if td.contents[0]==word:
24 if __name__=="__main__":
33 print 'input file :', in_file
34 print 'locale :', locale
35 print 'po file :', po_file
37 f = open( in_file, 'rt' )
38 soup = BeautifulSoup( f.read() )
41 for i, tr in enumerate(soup('tr')):
44 iID = findindexof( DESIGN_ID, tds )
45 iLocale = findindexof( locale, tds )
46 print 'index of design id :',iID, ", index of",locale,":", iLocale
47 if iID<0 or iLocale<0:
53 for j,td in enumerate(tds):
55 if j==iID: ID = td.contents[0]
56 elif j==iLocale: szLocale = td.contents[0]
62 dString[ID] = szLocale
63 #print i, '\t', ID, '\t' ,szLocale
66 f = open( po_file, 'rt' )
67 po = f.read().decode('utf-8')
70 for k, v in dString.items():
72 msg = 'msgid "'+ k + '"'
77 lines = po.split('\n')
78 for i, line in enumerate(lines):
79 if line.find( msg )>-1:
82 v = v.replace('\r', '') #remove linefeed
83 v = v.replace('\n ', '\\n') #remove carriage return
84 lines[i+1] = 'msgstr "' + v + '"'
85 po = "\n".join( lines )
90 # po += 'msgid "' + k + '"\n'
91 # po += 'msgstr "' + v + '"\n\n'
93 newfile = po_file+'.new'
94 f = open( newfile , 'wt' )
95 f.write( po.encode('utf-8') )
97 print newfile, 'is created'