1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#!/usr/bin/python
# 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 ):
if separate:
self.outfile.write("\n")
self.Write( self.autoComment, pair[1] )
for otherTranslation in self.translations:
try:
self.Write( self.autoComment2 + otherTranslation, self.translations[otherTranslation][pair[0]] )
except KeyError: pass
# write translation
self.WriteFromDictionary( pair[0] )
separate = True
# write translated items that don't appear in the original
if len( self.dictionary ) > 0:
self.outfile.write( "\n" + self.outdated )
self.WriteDictionary0()
# write old comments
for line in self.lostcomments:
self.outfile.write( line )
self.outfile.close()
del self.outfile
# update translation files with new items to translate
def Translate( self, base, translation ):
# read dictionary
self.ReadDictionary( translation )
# rewrite into different file
self.WriteDictionary( base, translation + ".out" )
# rename files if no error happened
os.rename( translation, translation + ".bak" )
os.rename( translation + ".out", translation )
# update translation files for distribution
def Distribute( self, translation ):
# read dictionary
self.ReadDictionary( translation )
# rewrite into different file
self.WriteDictionary1( translation + ".out" )
# rename files if no error happened
os.rename( translation, translation + ".bak" )
os.rename( translation + ".out", translation )
# add a file to the list of translations to print along with the english original
def AddTranslation( self, file ):
# read dictionary
self.ReadDictionary( file )
# store it
self.translations[string.split(file,".")[0]] = self.dictionary
del self.dictionary
if __name__ == "__main__":
# determine languages to update: everything included from
# languages.txt.in except am/eng and custom.
files = []
listfile = open("languages.txt.in")
for line in listfile.readlines():
if line.startswith("include"):
file =line.split()[1]
if file != "american.txt" and file != "english.txt" and file != "british.txt" and file != "custom.txt":
files += [file]
lu = LanguageUpdater()
if len( sys.argv ) >= 2 and sys.argv[1] == "--dist":
# strip comments from translations
lu.maxlen = 0
for file in files:
lu.Distribute( file );
else:
if len( sys.argv ) >= 2 and sys.argv[1] == "--complete":
lu.commentAll = True
if len( sys.argv ) >= 2 and sys.argv[1] == "--scm":
lu.commentNone = True
# read in all translations
for file in sys.argv[1:]:
if file[0] != "-":
lu.AddTranslation(file);
# update translations: mark untranslated items and add original text as comment
for file in files:
lu.Translate( "english_base.txt", file );
# if len( sys.argv ) >=3:
|