-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverify_localization.py
More file actions
113 lines (93 loc) · 3.58 KB
/
Copy pathverify_localization.py
File metadata and controls
113 lines (93 loc) · 3.58 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
import json
import re
import sys
import os
def extract_keys_from_swift(file_path):
keys = set()
if not os.path.exists(file_path):
print(f"Error: Swift file not found at {file_path}")
return keys
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match tr("key") or fmt("key", ...)
tr_matches = re.findall(r'tr\("([^"]+)"\)', content)
fmt_matches = re.findall(r'fmt\("([^"]+)"', content)
keys.update(tr_matches)
keys.update(fmt_matches)
except Exception as e:
print(f"Error reading {file_path}: {e}")
return keys
def load_xcstrings(file_path):
if not os.path.exists(file_path):
print(f"Error: .xcstrings file not found at {file_path}")
return {}
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return data.get('strings', {})
except Exception as e:
print(f"Error reading {file_path}: {e}")
return {}
def main():
# Use relative paths from project root
swift_file = 'Modules/Core/Localization/L10n.swift'
xcstrings_file = 'Resources/Localization/Localizable.xcstrings'
print(f"Checking keys from {swift_file} against {xcstrings_file}...")
swift_keys = extract_keys_from_swift(swift_file)
xcstrings_data = load_xcstrings(xcstrings_file)
if not swift_keys:
print("No keys found in Swift file (or file not found).")
return
missing_keys = []
missing_en = []
missing_es = []
empty_translations = []
for key in sorted(swift_keys):
if key not in xcstrings_data:
missing_keys.append(key)
continue
localizations = xcstrings_data[key].get('localizations', {})
# Check English (en)
if 'en' not in localizations:
missing_en.append(key)
else:
en_val = localizations['en'].get('stringUnit', {}).get('value', '').strip()
if not en_val:
empty_translations.append(f"{key} (en)")
# Check Spanish (es-419 and es)
for es_locale in ['es-419', 'es']:
if es_locale not in localizations:
missing_es.append(f"{key} ({es_locale})")
else:
es_val = localizations[es_locale].get('stringUnit', {}).get('value', '').strip()
if not es_val:
empty_translations.append(f"{key} ({es_locale})")
errors_found = False
if missing_keys:
print(f"\n❌ Found {len(missing_keys)} missing keys in {xcstrings_file}:")
for key in missing_keys:
print(f" - {key}")
errors_found = True
if missing_en:
print(f"\n❌ Found {len(missing_en)} keys missing English ('en') translation:")
for key in missing_en:
print(f" - {key}")
errors_found = True
if missing_es:
print(f"\n❌ Found {len(missing_es)} keys missing Spanish ('es-419') translation:")
for key in missing_es:
print(f" - {key}")
errors_found = True
if empty_translations:
print(f"\n❌ Found {len(empty_translations)} empty translation values:")
for entry in empty_translations:
print(f" - {entry}")
errors_found = True
if errors_found:
sys.exit(1)
else:
print(f"\n✅ All {len(swift_keys)} keys exist in English ('en'), Latin American Spanish ('es-419'), and generic Spanish ('es') with non-empty translations!")
sys.exit(0)
if __name__ == "__main__":
main()