|
| 1 | +#!/usr/bin/venv python3 |
| 2 | +"""Script for handling translations with Babel""" |
| 3 | +from __future__importannotations |
| 4 | + |
| 5 | +importargparse |
| 6 | +importsubprocess |
| 7 | +frompathlibimportPath |
| 8 | + |
| 9 | +try: |
| 10 | +importtomllib |
| 11 | +exceptImportError: |
| 12 | +try: |
| 13 | +importtomliastomllib |
| 14 | +exceptImportErrorasie: |
| 15 | +raiseImportError( |
| 16 | +"tomli or tomllib is required to parse pyproject.toml" |
| 17 | + )fromie |
| 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 | +defget_project_info()->dict: |
| 31 | +"""Retrieve project's info to populate the message catalog template""" |
| 32 | +withopen(Path(PROJECT_DIR/"pyproject.toml"),"rb")asf: |
| 33 | +data=tomllib.load(f) |
| 34 | +returndata["project"] |
| 35 | + |
| 36 | + |
| 37 | +defextract_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 | +definit_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 | +ifpofile.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 | +defupdate_catalogs(locale:str)->None: |
| 75 | +"""Update translations from existing message catalogs""" |
| 76 | +cmd= ["pybabel","update","-i",POT_FILE,"-d",LOCALES_DIR] |
| 77 | +iflocale: |
| 78 | +cmd.extend(["-l",locale]) |
| 79 | +subprocess.run(cmd,cwd=PROJECT_DIR,check=True) |
| 80 | + |
| 81 | + |
| 82 | +defcompile_catalogs(locale:str)->None: |
| 83 | +"""Compile existing message catalogs""" |
| 84 | +cmd= ["pybabel","compile","-d",LOCALES_DIR] |
| 85 | +iflocale: |
| 86 | +cmd.extend(["-l",locale]) |
| 87 | +subprocess.run(cmd,cwd=PROJECT_DIR,check=True) |
| 88 | + |
| 89 | + |
| 90 | +defmain()->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 | +ifargs.command=="extract": |
| 108 | +extract_messages() |
| 109 | +elifargs.command=="init": |
| 110 | +ifnotlocale: |
| 111 | +parser.error("init requires passing the --locale option") |
| 112 | +init_locale(locale) |
| 113 | +elifargs.command=="update": |
| 114 | +update_catalogs(locale) |
| 115 | +elifargs.command=="compile": |
| 116 | +compile_catalogs(locale) |
| 117 | + |
| 118 | + |
| 119 | +if__name__=="__main__": |
| 120 | +main() |