forked from facebookarchive/three20
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffstrings.py
executable file
·649 lines (510 loc) · 24.3 KB
/
diffstrings.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env python
usage = """usage: %prog [options] path1 path2 ...
diffstrings compares your primary locale with all your other locales to help you determine which new strings need to be translated. It outputs XML files which can be translated, and then merged back into your strings files.
The path arguments supplied to this script should be the path containing your source files. The file system will be searched to find the .lproj directories containing the Localizable.strings files that the script will read and write.
"""
import os.path, codecs, optparse, re, datetime
from xml.sax.saxutils import escape
from xml.dom import minidom
###################################################################################################
global xmlFormat
xmlFormat = {}
reLprojFileName = re.compile(r'(.+?)\.lproj')
reStringsFileName = re.compile(r'(.+?)\.strings')
reComment = re.compile(r'/\*(.*?)\*/')
reString = re.compile(r'\s*"((\\.|.)+?)"\s*=\s*"(.+?)";')
reVariable = re.compile(r'%(@|d|f|lld|\d+\.?f|\d+\.\d+f|\d+d)')
reEnumeratedVariable = re.compile('(%?\d+\$)')
defaultComment = "No comment provided by engineer."
###################################################################################################
def generateProjects(projects):
for project in projects:
project.generateStrings()
def diffProjects(projects, sourceLocaleName, focusedLocaleName=None, verbose=False, dryRun=False):
compiledStrings = {}
sourceMap = {}
for project in projects:
project.compileStrings(compiledStrings, focusedLocaleName)
project.compileSourceMap(sourceMap)
if sourceLocaleName not in compiledStrings:
print "WARNING: No '%s' locale found!" % sourceLocaleName
else:
sourceStrings = compiledStrings[sourceLocaleName]
if verbose:
print "\n", "=" * 80
print "* %s has %d total strings." \
% (sourceLocaleName, len(sourceStrings.strings))
print "=" * 80
for localeName, localizedStrings in compiledStrings.iteritems():
if localizedStrings != sourceStrings:
sourceStrings.diffReport(localizedStrings)
for localeName, localizedStrings in compiledStrings.iteritems():
if localizedStrings != sourceStrings:
translation = sourceStrings.diff(localizedStrings)
if not dryRun:
translation.save(".", sourceMap=sourceMap)
def mergeProjects(projects, sourceLocaleName, focusedLocaleName=None, verbose=False, dryRun=False):
translations = {}
for project in projects:
sourceStrings = project.condenseStringSourceFiles()
sourceStrings.save()
# for localeName, localizedStrings in project.locales.iteritems():
# if not focusedLocaleName or focusedLocaleName == localeName:
# if localizedStrings.name in translations:
# translation = translations[localizedStrings.name]
# else:
# translation = Translation(localizedStrings.name)
# translation.open(".", "2")
# translations[localizedStrings.name] = translation
#
# if translation.strings:
# if verbose:
# localizedStrings.mergeReport(sourceStrings, translation)
#
# localizedStrings.mergeTranslation(sourceStrings, translation)
# if not dryRun:
# localizedStrings.save()
###################################################################################################
class XcodeProject(object):
def __init__(self, path, sourceLocaleName):
self.path = path
self.sourceLocaleName = sourceLocaleName
self.sourceLocalePath = self.__findStringsFile(sourceLocaleName, self.path)
self.stringSourceFiles = list(self.__iterateSourceStrings())
self.locales = {}
for localeName, localizedStrings in self.__iterateLocalizableStrings():
self.locales[localeName] = localizedStrings
def condenseStringSourceFiles(self):
""" Copies all strings from all sources files into a single file."""
sourceStrings = LocalizableStrings(self.sourceLocaleName)
sourceStrings.path = self.__findSourceStringsPath()
if not sourceStrings.path:
sourceStrings.path = os.path.join(self.sourceLocalePath, "Localizable.strings")
for sourceFile in self.stringSourceFiles:
sourceStrings.update(sourceFile)
return sourceStrings
def compileStrings(self, compiledStrings, focusedLocaleName=None):
""" Copies all strings in a dictionary for each locale."""
if not self.sourceLocaleName in compiledStrings:
compiledStrings[self.sourceLocaleName] = LocalizableStrings(self.sourceLocaleName)
compiledStringsFile = compiledStrings[self.sourceLocaleName]
for sourceStrings in self.stringSourceFiles:
compiledStringsFile.update(sourceStrings)
if focusedLocaleName:
locales = {focusedLocaleName: self.locales[focusedLocaleName]}
else:
locales = self.locales
for localeName, sourceStrings in locales.iteritems():
if not localeName in compiledStrings:
compiledStringsFile = LocalizableStrings(localeName)
compiledStrings[localeName] = compiledStringsFile
else:
compiledStringsFile = compiledStrings[localeName]
compiledStringsFile.update(sourceStrings)
def compileSourceMap(self, sourceMap):
for sourceStrings in self.stringSourceFiles:
for source in sourceStrings.strings:
if not source in sourceMap:
sourceMap[source] = []
name,ext = os.path.splitext(os.path.basename(sourceStrings.path))
sourceMap[source].append(name)
def generateStrings(self):
buildPath = None
cwd = os.getcwd()
os.chdir(self.path)
extras = ""
if os.path.isdir(os.path.join(self.path, "Three20.xcodeproj")):
extras = " -s TTLocalizedString"
for fileName in os.listdir(self.path):
name,ext = os.path.splitext(fileName)
if ext == ".m":
if not buildPath:
buildPath = self.__makeBuildPath()
if not os.path.isdir(buildPath):
os.makedirs(buildPath)
command = "genstrings %s -o %s%s" % (fileName, buildPath, extras)
print " %s" % command
os.system(command)
resultPath = os.path.join(buildPath, "Localizable.strings")
if os.path.isfile(resultPath):
renamedPath = os.path.join(buildPath, "%s.strings" % name)
os.rename(resultPath, renamedPath)
os.chdir(cwd)
def __findStringsFile(self, localeName, searchPath):
dirName = "%s.lproj" % localeName
localeDirPath = os.path.join(searchPath, dirName)
if os.path.isdir(localeDirPath):
return localeDirPath
for name in os.listdir(searchPath):
path = os.path.join(searchPath, name)
if os.path.isdir(path):
localeDirPath = self.__findStringsFile(localeName, path)
if localeDirPath:
return localeDirPath
return None
def __iterateSourceStrings(self):
buildPath = self.__makeBuildPath()
if not os.path.exists(buildPath):
for path in self.__findSourceStrings():
yield path
else:
for fileName in os.listdir(buildPath):
name,ext = os.path.splitext(fileName)
if ext == ".strings":
strings = LocalizableStrings(self.sourceLocaleName)
filePath = os.path.join(buildPath, fileName)
strings.open(filePath)
yield strings
def __findSourceStringsPath(self):
for name in os.listdir(self.sourceLocalePath):
m = reStringsFileName.match(name)
if m:
return os.path.join(self.sourceLocalePath, name)
def __findSourceStrings(self):
for name in os.listdir(self.sourceLocalePath):
m = reStringsFileName.match(name)
if m:
strings = LocalizableStrings(self.sourceLocaleName)
filePath = os.path.join(self.sourceLocalePath, name)
strings.open(filePath)
yield strings
def __iterateLocalizableStrings(self):
dirPath = os.path.dirname(self.sourceLocalePath)
for dirName in os.listdir(dirPath):
m = reLprojFileName.match(dirName)
if m:
localeName = m.groups()[0]
if localeName != self.sourceLocaleName:
strings = LocalizableStrings(localeName)
localeDirPath = os.path.join(dirPath, dirName)
for name in os.listdir(localeDirPath):
m = reStringsFileName.match(name)
if m:
filePath = os.path.abspath(os.path.join(localeDirPath, name))
strings.open(filePath)
break
yield localeName, strings
def __makeBuildPath(self):
return os.path.join(self.path, "build", "i18n")
###################################################################################################
class LocalizableStrings(object):
def __init__(self, name):
self.name = name
self.path = None
self.strings = {}
self.comments = {}
def open(self, path):
if os.path.isfile(path):
self.path = path
self.__parse()
def save(self, path=None, suffix=""):
text = self.generate()
if text:
if path:
filePath = self.__makePath(path, suffix)
else:
filePath = self.path
print "***** Saving %s" % filePath
f = codecs.open(filePath, 'w', 'utf-16')
f.write(text)
f.close()
def generate(self):
lines = []
for source, target in self.strings.iteritems():
if source in self.comments:
comment = self.comments[source]
lines.append("/* %s */" % comment)
lines.append('"%s" = "%s";\n' % (source, target))
return "\n".join(lines)
def mergeTranslation(self, sourceStrings, translation):
for source in sourceStrings.strings:
sourceEnum = enumerateStringVariables(source)
if sourceEnum in translation.strings:
targetEnum = translation.strings[sourceEnum]
target = denumerateStringVariables(targetEnum)
self.strings[source] = target
def update(self, other):
self.strings.update(other.strings)
self.comments.update(other.comments)
def diff(self, localizedStrings):
translation = Translation(localizedStrings.name)
for source, target in self.strings.iteritems():
sourceEnum = enumerateStringVariables(source)
if source in localizedStrings.strings:
target = localizedStrings.strings[source]
translation.translated[sourceEnum] = True
targetEnum = enumerateStringVariables(target)
translation.strings[sourceEnum] = targetEnum
if source in self.comments:
translation.comments[sourceEnum] = self.comments[source]
return translation
def diffReport(self, localizedStrings):
name = localizedStrings.name
newStrings = list(self.__compare(localizedStrings))
obsoleteStrings = list(localizedStrings.__compare(self))
troubleStrings = list(self.__compareSizes(localizedStrings))
print "\n", "=" * 80
if not len(newStrings):
if len(obsoleteStrings):
print "%s is fully translated, but has %s obsolete strings" \
% (name, len(obsoleteStrings))
else:
print "%s is fully translated" % name
else:
existingCount = len(self.strings) - len(newStrings)
if len(obsoleteStrings):
print "%s has %s new strings, %s translated, and %s obsolete."\
% (name, len(newStrings), existingCount, len(obsoleteStrings))
else:
print "%s has %s new strings, with %s already translated."\
% (name, len(newStrings), existingCount)
print "=" * 80
if len(newStrings):
print "\n---- %s NEW STRINGS ---\n" % name
print "\n".join(newStrings)
if len(obsoleteStrings):
print "\n---- %s OBSOLETE STRINGS ---\n" % name
print "\n".join(obsoleteStrings)
if len(troubleStrings):
print "\n---- %s TROUBLE STRINGS ---\n" % name
for source, diff in sorted(troubleStrings, lambda a,b: cmp(b[1], a[1])):
print "%3d. %s " % (diff, codecs.encode(source, 'utf-8'))
print " %s " % codecs.encode(localizedStrings.strings[source], 'utf-8')
print "\n"
def mergeReport(self, sourceStrings, translation):
name = self.name
updatedStrings = []
ignoredStrings = []
for source in sourceStrings.strings:
sourceEnum = enumerateStringVariables(source)
if sourceEnum in translation.strings:
targetEnum = translation.strings[sourceEnum]
target = denumerateStringVariables(targetEnum)
if source not in self.strings or target != self.strings[source]:
updatedStrings.append(source)
else:
ignoredStrings.append(source)
print "\n", "=" * 80
print self.path
print "%d newly translated strings and %d untranslated strings" \
% (len(updatedStrings), len(ignoredStrings))
print "=" * 80
if len(updatedStrings):
print "\n---- %s NEWLY TRANSLATED STRINGS ---\n" % name
print "\n".join(updatedStrings)
if len(ignoredStrings):
print "\n---- %s UNTRANSLATED STRINGS ---\n" % name
print "\n".join(ignoredStrings)
def __makePath(self, path=".", suffix=""):
fileName = "Localizable%s.strings" % (suffix)
return os.path.abspath(os.path.join(path, fileName))
def __parse(self):
lastIdentical = False
lastComment = None
for line in openWithProperEncoding(self.path):
m = reString.search(line)
if m:
source = m.groups()[0]
target = m.groups()[2]
self.strings[source] = target
if lastComment:
self.comments[source] = lastComment
lastComment = None
if lastIdentical:
lastIdentical = False
else:
m = reComment.search(line)
if m:
comment = m.groups()[0].strip()
if comment != defaultComment:
lastComment = comment
def __compare(self, other, compareStrings=False):
for source, target in self.strings.iteritems():
if source in other.strings:
target = other.strings[source]
if compareStrings and target == source:
yield source
else:
yield source
def __compareSizes(self, other):
for source, target in self.strings.iteritems():
if source in other.strings:
target = other.strings[source]
ratio = float(len(target)) / len(source)
diff = len(target) - len(source)
if ratio > 1.3 and diff > 5:
yield (source, diff)
###################################################################################################
class Translation(object):
def __init__(self, name):
self.name = name
self.path = None
self.strings = {}
self.translated = {}
self.comments = {}
def open(self, path=".", suffix=""):
filePath = self.__makePath(path, suffix)
if os.path.isfile(filePath):
self.__parse(filePath)
def save(self, path=None, suffix="", sourceMap=None):
text = self.generate(sourceMap)
if text:
if path:
filePath = self.__makePath(path, suffix)
else:
filePath = self.path
print "***** Saving %s" % filePath
#print codecs.encode(text, 'utf-8')
f = codecs.open(filePath, 'w', 'utf-16')
f.write(text)
f.close()
def generate(self, sourceMap=None):
lines = []
global xmlFormat
prefix = xmlFormat['prefix']
lines.append('<?xml version="1.0" encoding="utf-16"?>')
lines.append('<%sexternal>' % prefix)
lines.append(' <meta>')
if xmlFormat['appName']:
lines.append(' <appName>%s</appName>' % xmlFormat['appName'])
lines.append(' <date>%s</date>' % datetime.datetime.now().strftime('%Y%m%d'))
lines.append(' <locale>%s</locale>' % self.name)
lines.append(' </meta>')
for sourceFileName, sourceFileStrings in self.__invertSourceMap(sourceMap):
lines.append(" <!-- %s -->" % sourceFileName)
for source in sourceFileStrings:
target = self.strings[source]
lines.append(" <entry>")
lines.append(" <%ssource>%s</%ssource>" % (prefix, escape(source), prefix))
if source in self.translated:
lines.append(" <%sxtarget>%s</%sxtarget>" % (prefix, escape(target), prefix))
else:
lines.append(" <%starget>%s</%starget>" % (prefix, escape(target), prefix))
if source in self.comments:
lines.append(" <%sdescription>%s</%sdescription>"
% (prefix, escape(self.comments[source]), prefix))
lines.append(" </entry>")
lines.append('</%sexternal>' % prefix)
return "\n".join(lines)
def __makePath(self, path=".", suffix=""):
fileName = "%s%s.xml" % (self.name, suffix)
return os.path.abspath(os.path.join(path, fileName))
def __parse(self, filePath):
self.path = filePath
global xmlFormat
prefix = xmlFormat['prefix']
document = minidom.parse(filePath)
for entry in document.documentElement.childNodes:
if entry.nodeType == 1:
source = None
target = None
translated = False
sources = entry.getElementsByTagName("%ssource" % prefix)
if len(sources):
source = sources[0]
source = source.childNodes[0].data
targets = entry.getElementsByTagName("%sxtarget" % prefix)
if not len(targets):
targets = entry.getElementsByTagName("%starget" % prefix)
translated = True
if len(targets):
target = targets[0]
target = target.childNodes[0].data
if source and target:
self.strings[source] = target
if translated:
self.translated[source] = True
def __invertSourceMap(self, sourceMap):
sourceFileMap = {}
for sourceEnum in self.strings:
source = denumerateStringVariables(sourceEnum)
if source in sourceMap:
sourcePaths = sourceMap[source]
for sourcePath in sourcePaths:
if sourcePath not in sourceFileMap:
sourceFileMap[sourcePath] = []
sourceFileMap[sourcePath].append(sourceEnum)
break
for sourceName, sourceFileStrings in sourceFileMap.iteritems():
sourceFileStrings.sort()
keys = sourceFileMap.keys()
keys.sort()
for key in keys:
yield key, sourceFileMap[key]
###################################################################################################
## Helpers
def openProjects(projectDirPaths, sourceLocaleName):
for projectDirPath in projectDirPaths:
yield XcodeProject(projectDirPath, sourceLocaleName)
def openWithProperEncoding(path):
if not os.path.isfile(path):
return []
try:
f = codecs.open(path, 'r', 'utf-16')
lines = f.read().splitlines()
f.close()
except UnicodeError,exc:
f = codecs.open(path, 'r', 'utf-8')
lines = f.read().splitlines()
f.close()
return lines
def enumerateStringVariables(s):
i = 1
for var in reVariable.findall(s):
s = s.replace("%%%s" % var, "%%%d$%s" % (i, var), 1)
i += 1
return s
def denumerateStringVariables(s):
for var in reEnumeratedVariable.findall(s):
s = s.replace(var, "%")
return s
###################################################################################################
## Main
def parseOptions():
parser = optparse.OptionParser(usage)
parser.set_defaults(locale="en", focus=None, build=False, merge=False, diff=False,
verbose=False, dryrun=False, appName="", prefix="")
parser.add_option("-l", "--locale", dest="locale", type="str",
help = "The name of your source locale. The default is 'en'.")
parser.add_option("-f", "--focus", dest="focus", type="str",
help = "The name of the locale to operate on, excluding all others.")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help = "Verbose reporting of activity.")
parser.add_option("-r", "--dryrun", dest="dryrun", action="store_true",
help = "Print the output of files instead of saving them.")
parser.add_option("-b", "--build", dest="build", action="store_true",
help = "Runs genstrings on each source file in each project.")
parser.add_option("-d", "--diff", dest="diff", action="store_true",
help="Generates a diff of each locale against the source locale. Each locale's diff will be stored in a file in the working directory named <locale>.xml.")
parser.add_option("-m", "--merge", dest="merge", action="store_true",
help="Merges strings from the <locale>.xml file in the working directory back into the Localized.strings files in each locale.")
parser.add_option("-p", "--prefix", dest="prefix", type="str",
help="The prefix to use on the xml tags.")
parser.add_option("-a", "--appname", dest="appName", type="str",
help="The name of the application to include in the xml metadata.")
options, arguments = parser.parse_args()
paths = ["."] if not len(arguments) else arguments
if not options.merge:
options.diff = True
return options, paths
def main():
options, projectPaths = parseOptions()
projectPaths = [os.path.abspath(os.path.expanduser(path)) for path in projectPaths]
global xmlFormat
xmlFormat['prefix'] = options.prefix
xmlFormat['appName'] = options.appName
projects = list(openProjects(projectPaths, options.locale))
if options.build:
print "******* Generating strings *******"
generateProjects(projects)
print ""
if options.merge:
print "******* Merging *******"
mergeProjects(projects, options.locale, options.focus, options.verbose, options.dryrun)
print ""
if options.diff:
print "******* Diffing *******"
diffProjects(projects, options.locale, options.focus, options.verbose, options.dryrun)
print ""
if __name__ == "__main__":
main()