"""Validate catalog coverage and printf argument compatibility in the source catalogs and optionally the compiled app.""" import argparse import collections import json from pathlib import Path import re import subprocess 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 parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--app', type=Path, help='Also compare every translation with the built app') app = parser.parse_args().app locales = ('zh-Hans', 'en', 'zh-Hant') count = 0 for path in sorted(root.glob('*.xcstrings')): compiled = {} if app: for locale in locales: table = app / 'Contents' / 'Resources' / f'{locale}.lproj' / f'{path.stem}.strings' compiled[locale] = json.loads(subprocess.check_output(['plutil', '-convert', 'json', '-o', '-', table])) for key, entry in json.loads(path.read_text())['strings'].items(): units = {locale: entry['localizations'][locale]['stringUnit'] for locale in locales} expected = arguments(key) or arguments(units['en']['value']) for locale, unit in units.items(): assert unit['state'] == 'translated' and unit['value'].strip(), (path, key, locale) assert arguments(unit['value']) == expected, (path, key, locale) if app: assert compiled[locale].get(key) == unit['value'], (path, key, locale, 'built translation') count += 1 print(f'PASS: {count} catalog entries, three-language coverage and format arguments')