forked from cateye/android-missing-strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathams
More file actions
executable file
·168 lines (127 loc) · 5.01 KB
/
ams
File metadata and controls
executable file
·168 lines (127 loc) · 5.01 KB
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
#!/usr/bin/env python
import re
import os
import sys
import getopt
from fpdf import FPDF
PATTERN=re.compile(r'<string name="(.*?)".*>(.*?)</string>', re.I | re.MULTILINE)
class MissingStringsReport:
filePath = None
originalStringsDict = None
myStringsDict = None
missingKeys = None
def __init__(self, stringsFilePath, originalStringsDict):
'''stringsFilePath - The path to the strings.xml file we want to check.
originalStringsDict - A dictionary containing all the string-keys and original language values (usually inside res/values/strings.xml)
'''
self.stringsFilePath = stringsFilePath
self.originalStringsDict = originalStringsDict
self.myStringsDict = MissingStringsReport.readXML(self.stringsFilePath)
self.missingKeys = MissingStringsReport.findMissingKeys(self.originalStringsDict, self.myStringsDict)
def getMissingKeys(self):
'''Returns a list with the missing keys.'''
return self.missingKeys
def getMyStringsDict(self):
return self.myStringsDict
def getCompletePercentage(self):
return (len(self.myStringsDict)/float(len(self.originalStringsDict))) * 100
@staticmethod
def findMissingKeys(fullDict, incompleteDict):
'''Given the original (fullDict) strings dictionary, this returns a list of the missing keys on incompleteDict.
If the file is fully translated it will return an empty list.'''
missingKeys = []
for k in fullDict:
if k not in incompleteDict:
missingKeys.append(k)
return missingKeys
@staticmethod
def readXML(fileName):
result={}
global PATTERN
f=open(fileName,'r')
line = f.readline()
while line!="":
matcher = PATTERN.search(line)
if matcher != None:
result[matcher.group(1)]=matcher.group(2)
line = f.readline()
f.close()
return result
def getStringsFilePaths():
'''Gets all the relative paths to strings.xml files in a list.'''
p = os.popen("find . | grep strings.xml")
filesPath = p.readlines()
p.close()
return filesPath
def usage():
usage = """
ams - Android Missing Strings reporting tool.
Usage:
ams [-l xx[,yy,zz...]] -o <output_file>
Options:
-h --help Print this help
-l --lang <xx> Specify a language or many with comma separated 2-char language codes.
e.g: -l cn (creates report for Chinese strings.xml)
-l cn,it,fr (creates report for Chinese, Italian and French strings.xml files)
If this parameter is ommited, a report with every language file found will be created.
-o --oFile Specify the output file name for the PDF report
Copyright (c) 2014 - The Mit License (MIT)
Authors:
Angel Leon <gubatron@gmail.com>
Katay Santos <kataysantos@gmail.com>
"""
print usage
def main(argv):
xmlFilePaths=[]
outputFile = ''
try:
opts, args = getopt.getopt(argv, "ho:l:",["ofile=","lang="])
except getopt.GetoptError:
print "unhandled option"
usage()
sys.exit(2)
if len(opts) == 0:
usage()
print "Error: missing parameters."
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit(2)
if opt in ("-o", "--ofile"):
outputFile = arg
if opt in ("-l", "--lang"):
if "," in arg:
languages=arg.split(",")
for l in languages:
xmlFilePaths.append("./res/values-" + l + "/strings.xml")
else:
xmlFilePaths=["./res/values-" + arg + "/strings.xml"]
if outputFile == '':
usage()
print "Error: missing -o (output file) parameter."
sys.exit(2)
if len(xmlFilePaths) == 0:
xmlFilePaths=getStringsFilePaths()
xmlOriginalFile="./res/values/strings.xml"
originalDict=MissingStringsReport.readXML(xmlOriginalFile)
pdf=FPDF()
pdf.add_page()
for xmlFilePath in xmlFilePaths:
filePath = xmlFilePath.strip()
if filePath != xmlOriginalFile:
report = MissingStringsReport(filePath, originalDict)
print "Generating report for " + filePath + "..."
pdf.set_font('Arial','B',12)
pdf.cell(100,10,"Language file: " + filePath,0,0)
pdf.cell(40,10,"%d%% completed " % (report.getCompletePercentage()),0,1,'R')
pdf.cell(40,10,"Missing elements: ",0,1)
for missingKey in report.getMissingKeys():
pdf.set_font('Arial','I',8)
tag = "<string name=\"%s\">%s</string>" % (missingKey, originalDict[missingKey])
pdf.multi_cell(190,10,tag,1,1)
pdf.add_page()
pdf.output(outputFile,'F')
print "\nSuccess: Android Missing String report ready at " + outputFile
if __name__ == '__main__':
main(sys.argv[1:])