-
Notifications
You must be signed in to change notification settings - Fork 11
/
sort_keys.py
160 lines (121 loc) · 5.6 KB
/
sort_keys.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
#!/usr/bin/python3
import argparse
import os.path
from functions import readTranslations, clearContentsOfFile, writeTranslationToFile, writeCommentToFile
#TODO: Auto remove duplicate key / value pairs and report duplicates where their values don't match
# Sorts keys that don't match with their translations to the top of the file. Most of the times the base strings
# file will have lines such as:
# "done" = "done";
#
# However at times you may have something like:
#
# "Preference.General.Done" = "OK";
#
# This script finds such strings and moves them to the top of the file so these can be grouped separately for better
# discoverability.
parser = argparse.ArgumentParser()
parser.add_argument("-p", default="", help="set the path to the root directory where all the localized translations reside (i.e. directory with `fr.lproj` etc)")
parser.add_argument("-f", default="Localizable.strings", help="set the name of the .strings file to look for")
parser.add_argument("-o", default="en", help="set the origin locale for to extract missing translations from, default is english")
args = parser.parse_args()
# Read and cache origin language once
resourcePath = os.path.expanduser(args.p.strip())
originLangKey = args.o.strip()
stringsFileName = os.path.expanduser(args.f.strip())
originPath = os.path.join(resourcePath, originLangKey + os.path.join(".lproj", stringsFileName))
print("Reading source language: %s" % (originPath))
originLines = readTranslations(originPath)
clearContentsOfFile(stringsFileName, originLangKey)
print("Sorting localizations")
totalLinesWritten = 0
mismatchedTranslationLines = []
normalLines = []
for originLine in originLines:
stringName = originLine['key']
stringVal = originLine['value']
if stringName != stringVal:
mismatchedTranslationLines.append(originLine)
totalLinesWritten += 1
#end if
#end for
for originLine in originLines:
stringName = originLine['key']
stringVal = originLine['value']
if stringName == stringVal:
normalLines.append(originLine)
totalLinesWritten += 1
# end if
# end for
formatMisMatch = 0
for line in sorted(mismatchedTranslationLines, key = lambda i: str(i['key']).lower()):
stringName = line['key']
stringVal = line['value']
stringComment = line['comment']
if not stringComment:
stringComment = ""
#end if
# lint
stringVal = stringVal.replace("(", " (")
stringVal = stringVal.replace(")", ") ")
stringVal = stringVal.replace("%", "%")
stringVal = stringVal.replace("% @", "%@")
stringVal = stringVal.replace("\\ n", "\n")
writeTranslationToFile(stringsFileName, stringName, stringVal, stringComment, originLangKey)
# Some basic validation to confirm translation did not get rid of formatters in source text
totalFormattersInSource = stringName.count('%')
totalFormattersInOutput = stringVal.count('%')
if totalFormattersInSource != totalFormattersInOutput:
formatMisMatch += 1
print("\n ..... !! WARNING !! Formatters don't match in: %s => %s\n" % (
stringName, stringVal))
#end if
formatterIndex = stringVal.find('%')
if formatterIndex != -1 and formatterIndex + 1 >= len(stringVal):
formatMisMatch += 1
print("\n ..... !! WARNING !! Invalid formatter in: %s => %s\n" % (stringName, stringVal))
elif formatterIndex != -1 and len(stringVal) != formatterIndex and stringVal[formatterIndex + 1] not in ['u', 'l', '@', 'f', '1', '2', '3', 'd', '.']:
formatMisMatch += 1
print("\n ..... !! WARNING !! Invalid formatter in: %s => %s\n" % (stringName, stringVal))
# end if
#end for
writeCommentToFile(stringsFileName, "-------", originLangKey)
for line in sorted(normalLines, key = lambda i: str(i['key']).lower()):
stringName = line['key']
stringVal = line['value']
stringComment = line['comment']
if not stringComment:
stringComment = ""
#end if
# lint
stringVal = stringVal.replace("(", " (")
stringVal = stringVal.replace(")", ")")
stringVal = stringVal.replace("%", "%")
stringVal = stringVal.replace("% @", "%@")
stringVal = stringVal.replace("\\ n", "\n")
writeTranslationToFile(stringsFileName, stringName, stringVal, stringComment, originLangKey)
# Some basic validation to confirm translation did not get rid of formatters in source text
totalFormattersInSource = stringName.count('%')
totalFormattersInOutput = stringVal.count('%')
if totalFormattersInSource != totalFormattersInOutput:
formatMisMatch += 1
print("\n ..... !! WARNING !! Formatters don't match in: %s => %s\n" % (
stringName, stringVal))
#end if
formatterIndex = stringVal.find('%')
if formatterIndex != -1 and formatterIndex + 1 >= len(stringVal):
formatMisMatch += 1
print("\n ..... !! WARNING !! Invalid formatter in: %s => %s\n" % (stringName, stringVal))
elif formatterIndex != -1 and len(stringVal) != formatterIndex and stringVal[formatterIndex + 1] not in ['u', 'l', '@', 'f', '1', '2', '3', 'd', '.']:
formatMisMatch += 1
print("\n ..... !! WARNING !! Invalid formatter in: %s => %s\n" % (stringName, stringVal))
# end if
#end for
if formatMisMatch > 0:
# This may be okay since the string name itself may not have any formatters, but worth pointing out
# in case the keys and values are the same
print("WARN: Total mismatched formatters found: %s" % (formatMisMatch))
#endif
if totalLinesWritten != len(originLines):
print("ERROR: Total lines written do NOT match total lines read - %s != %s" % (totalLinesWritten, len(originLines)))
#end if
print("Done")