forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_bcp47.py
More file actions
58 lines (43 loc) · 1.84 KB
/
Copy pathcheck_bcp47.py
File metadata and controls
58 lines (43 loc) · 1.84 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
#!/usr/bin/env python3
"""G3 BCP 47 / RFC 5646 language-tag validity gate (merge-blocking).
Every language tag nearmiss authors must be well-formed *and* resolvable against
the IANA/CLDR registry, so a typo'd or invented locale ("sp", "en_US" with the
wrong separator, "esp") never ships. We validate:
* the locale directory names under ``src/nearmiss/locales`` (each is a real
gettext catalog we compile and load), and
* the ``SUPPORTED_LANGUAGES`` tuple and ``DEFAULT_LANGUAGE`` declared in
``nearmiss.i18n``,
by parsing each through ``babel.Locale.parse`` (CLDR-backed). Anything Babel
cannot resolve fails the build.
"""
from __future__ import annotations
import sys
from babel import Locale, UnknownLocaleError
# nearmiss is installed (editable) in the environment `make verify`/CI runs in.
from nearmiss.i18n import DEFAULT_LANGUAGE, LOCALEDIR, SUPPORTED_LANGUAGES
def _catalog_dirs() -> list[str]:
if not LOCALEDIR.is_dir():
return []
return sorted(p.name for p in LOCALEDIR.iterdir() if (p / "LC_MESSAGES").is_dir())
def main() -> int:
tags = set(SUPPORTED_LANGUAGES) | {DEFAULT_LANGUAGE} | set(_catalog_dirs())
errors: list[str] = []
for tag in sorted(tags):
try:
Locale.parse(tag)
except (UnknownLocaleError, ValueError) as exc:
errors.append(f"{tag!r}: {exc}")
if DEFAULT_LANGUAGE not in SUPPORTED_LANGUAGES:
errors.append(
f"DEFAULT_LANGUAGE {DEFAULT_LANGUAGE!r} is not in SUPPORTED_LANGUAGES "
f"{SUPPORTED_LANGUAGES!r}"
)
if errors:
print("BCP 47 tag validation FAILED:", file=sys.stderr)
for err in errors:
print(f" - {err}", file=sys.stderr)
return 1
print(f"BCP 47 OK: {sorted(tags)} are well-formed, registry-valid tags.")
return 0
if __name__ == "__main__":
raise SystemExit(main())