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