|
| 1 | +#!/usr/bin/venv python3 |
| 2 | +"""Script for handling translations with Babel""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +try: |
| 10 | + import tomllib |
| 11 | +except ImportError: |
| 12 | + try: |
| 13 | + import tomli as tomllib |
| 14 | + except ImportError as ie: |
| 15 | + raise ImportError( |
| 16 | + "tomli or tomllib is required to parse pyproject.toml" |
| 17 | + ) from ie |
| 18 | + |
| 19 | +PROJECT_DIR = Path(__file__).resolve().parent |
| 20 | + |
| 21 | +# Global variables used by pybabel below (paths relative to PROJECT_DIR) |
| 22 | +DOMAIN = "messages" |
| 23 | +COPYRIGHT_HOLDER = "Python Software Foundation" |
| 24 | +LOCALES_DIR = "locales" |
| 25 | +POT_FILE = Path(LOCALES_DIR, f"{DOMAIN}.pot") |
| 26 | +SOURCE_DIR = "python_docs_theme" |
| 27 | +MAPPING_FILE = ".babel.cfg" |
| 28 | + |
| 29 | + |
| 30 | +def get_project_info() -> dict: |
| 31 | + """Retrieve project's info to populate the message catalog template""" |
| 32 | + with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f: |
| 33 | + data = tomllib.load(f) |
| 34 | + return data["project"] |
| 35 | + |
| 36 | + |
| 37 | +def extract_messages() -> None: |
| 38 | + """Extract messages from all source files into message catalog template""" |
| 39 | + Path(PROJECT_DIR, LOCALES_DIR).mkdir(parents=True, exist_ok=True) |
| 40 | + project_data = get_project_info() |
| 41 | + subprocess.run( |
| 42 | + [ |
| 43 | + "pybabel", |
| 44 | + "extract", |
| 45 | + "-F", |
| 46 | + MAPPING_FILE, |
| 47 | + "--copyright-holder", |
| 48 | + COPYRIGHT_HOLDER, |
| 49 | + "--project", |
| 50 | + project_data["name"], |
| 51 | + "--version", |
| 52 | + project_data["version"], |
| 53 | + "--msgid-bugs-address", |
| 54 | + project_data["urls"]["Issue tracker"], |
| 55 | + "-o", |
| 56 | + POT_FILE, |
| 57 | + SOURCE_DIR, |
| 58 | + ], |
| 59 | + cwd=PROJECT_DIR, |
| 60 | + check=True, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def init_locale(locale: str) -> None: |
| 65 | + """Initialize a new locale based on existing message catalog template""" |
| 66 | + pofile = PROJECT_DIR / LOCALES_DIR / locale / "LC_MESSAGES" / f"{DOMAIN}.po" |
| 67 | + if pofile.exists(): |
| 68 | + print(f"There is already a message catalog for locale {locale}, skipping.") |
| 69 | + return |
| 70 | + cmd = ["pybabel", "init", "-i", POT_FILE, "-d", LOCALES_DIR, "-l", locale] |
| 71 | + subprocess.run(cmd, cwd=PROJECT_DIR, check=True) |
| 72 | + |
| 73 | + |
| 74 | +def update_catalogs(locale: str) -> None: |
| 75 | + """Update translations from existing message catalogs""" |
| 76 | + cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR] |
| 77 | + if locale: |
| 78 | + cmd.extend(["-l", locale]) |
| 79 | + subprocess.run(cmd, cwd=PROJECT_DIR, check=True) |
| 80 | + |
| 81 | + |
| 82 | +def compile_catalogs(locale: str) -> None: |
| 83 | + """Compile existing message catalogs""" |
| 84 | + cmd = ["pybabel", "compile", "-d", LOCALES_DIR] |
| 85 | + if locale: |
| 86 | + cmd.extend(["-l", locale]) |
| 87 | + subprocess.run(cmd, cwd=PROJECT_DIR, check=True) |
| 88 | + |
| 89 | + |
| 90 | +def main() -> None: |
| 91 | + parser = argparse.ArgumentParser(description=__doc__) |
| 92 | + parser.add_argument( |
| 93 | + "command", |
| 94 | + choices=["extract", "init", "update", "compile"], |
| 95 | + help="command to be executed", |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "-l", |
| 99 | + "--locale", |
| 100 | + default="", |
| 101 | + help="language code (needed for init, optional for update and compile)", |
| 102 | + ) |
| 103 | + |
| 104 | + args = parser.parse_args() |
| 105 | + locale = args.locale |
| 106 | + |
| 107 | + if args.command == "extract": |
| 108 | + extract_messages() |
| 109 | + elif args.command == "init": |
| 110 | + if not locale: |
| 111 | + parser.error("init requires passing the --locale option") |
| 112 | + init_locale(locale) |
| 113 | + elif args.command == "update": |
| 114 | + update_catalogs(locale) |
| 115 | + elif args.command == "compile": |
| 116 | + compile_catalogs(locale) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments