26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
"""Validate catalog coverage and printf argument compatibility against zh-Hans."""
|
|
import collections
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
|
|
root = Path(__file__).resolve().parents[1] / 'kymo/Resources'
|
|
pattern = re.compile(r'%(?:(\d+)\$)?[-+#0 ]*(?:\d+)?(?:\.\d+)?(ll|l|h)?([@diuoxXfFeEgGcCsSp])')
|
|
|
|
def arguments(text):
|
|
result = collections.Counter()
|
|
for index, match in enumerate(pattern.finditer(text.replace('%%', '')), 1):
|
|
result[(int(match[1] or index), (match[2] or '') + match[3])] += 1
|
|
return result
|
|
|
|
count = 0
|
|
for path in sorted(root.glob('*.xcstrings')):
|
|
for key, entry in json.loads(path.read_text())['strings'].items():
|
|
units = {locale: entry['localizations'][locale]['stringUnit']
|
|
for locale in ['zh-Hans', 'en', 'zh-Hant']}
|
|
for locale, unit in units.items():
|
|
assert unit['state'] == 'translated' and unit['value'].strip(), (path, key, locale)
|
|
assert arguments(unit['value']) == arguments(units['zh-Hans']['value']), (path, key, locale)
|
|
count += 1
|
|
print(f'PASS: {count} catalog entries, three-language coverage and format arguments')
|