-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract_localizable_strings.py
executable file
·67 lines (55 loc) · 1.83 KB
/
extract_localizable_strings.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
#!/usr/bin/env python3
#
# Find all localizable strings, used in code as indicated by PATTERN
import io
import re
import sys
import glob
# the reference language - 1st argument, defaults to "en"
REFERENCE = sys.argv[1] if len(sys.argv) > 1 else 'en'
REFERENCE_FILE = '{}.lproj/C3PRO.strings'.format(REFERENCE)
# the 1st group must capture the actual string
#PATTERN = re.compile(r'"((([^"])|(\\"))+?)".c3_localized')
# TODO: how to zip past '\"'?
PATTERN = re.compile(r'"([^"]+?)".c3_localized')
# TODO: extract the comments if this form is used: .c3_localized("Start Consent button title")
def parse_into(filepath, into):
with open(filepath, 'r') as handle:
text = handle.read()
for res in re.finditer(PATTERN, text):
if res is not None:
into.add(res.group(1))
if '__main__' == __name__:
found = set()
# tests
if False:
tests = [
'let str = "1 Hello".c3_localized',
'let str = "2 Hello (World)".c3_localized',
'let str = "3 Hello \"World\"".c3_localized',
'let str = "The \("4 Hello".c3_localized) and \("5 World".c3_localized)"'
'let bar = "Hello"\nfunction(is: true)\nlet two = "6 World".c3_localized'
]
for test in tests:
for res in re.finditer(PATTERN, test):
print(res.group(1) if res else None)
sys.exit(0)
# loop all implementation files
for filepath in glob.glob("Sources/*/*.swift"):
parse_into(filepath, found)
# look at Localizable.strings
existing = {}
with io.open(REFERENCE_FILE, 'r', encoding='utf-8') as handle:
for line in handle:
line = line.strip()
if len(line) > 0:
parts = line.split('" = "')
if 2 == len(parts):
key = parts[0][1:]
existing[key] = line
# show all, alphabetically ordered
for string in sorted(found, key=lambda s: s.lower()):
if string in existing:
print(existing[string])
else:
print('"{}" = "{}";'.format(string, string))