Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork66
Add script for handling translations#195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
d271fac
Add script for handling translations
rffontenelle634a20c
Make ruff tests happy
rffontenellea809821
Rename mapping file to .babel.cfg
rffontenelleadf715c
Forbid initializing existent po
rffontenelle66820a5
Extend instead of append list for locale arg
rffontenelle6b6f0d2
Add translation tests to tests.yml
rffontenelle7878f4e
Use python instead of python3 to run babel_runner.py
rffontenelle1fbd5d0
Replace os with pahlib in babe_runner.py
rffontenellee3e08eb
Add tomli import to support python<3.11
rffontenellea845398
CI test for minimum supported python version
rffontenelle541c7a5
Merge branch 'main' into add-localization
hugovkbd7c059
Apply suggestions from code review
rffontenelle65a0360
Remove possibly existing translation file
rffontenelle69ab917
Add shell bash to success in Windows
rffontenelle2aa3a04
Swap init and extract to be didactic
rffontenelleFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.babel.cfg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[jinja2: **.html] |
38 changes: 38 additions & 0 deletions.github/workflows/tests.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletionsbabel_runner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/usr/bin/venv python3 | ||
"""Script for handling translations with Babel""" | ||
from __future__ import annotations | ||
import argparse | ||
import subprocess | ||
from pathlib import Path | ||
try: | ||
import tomllib | ||
except ImportError: | ||
try: | ||
import tomli as tomllib | ||
except ImportError as ie: | ||
raise ImportError( | ||
"tomli or tomllib is required to parse pyproject.toml" | ||
) from ie | ||
PROJECT_DIR = Path(__file__).resolve().parent | ||
# Global variables used by pybabel below (paths relative to PROJECT_DIR) | ||
DOMAIN = "messages" | ||
COPYRIGHT_HOLDER = "Python Software Foundation" | ||
LOCALES_DIR = "locales" | ||
POT_FILE = Path(LOCALES_DIR, f"{DOMAIN}.pot") | ||
SOURCE_DIR = "python_docs_theme" | ||
MAPPING_FILE = ".babel.cfg" | ||
def get_project_info() -> dict: | ||
"""Retrieve project's info to populate the message catalog template""" | ||
with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f: | ||
data = tomllib.load(f) | ||
return data["project"] | ||
def extract_messages() -> None: | ||
"""Extract messages from all source files into message catalog template""" | ||
Path(PROJECT_DIR, LOCALES_DIR).mkdir(parents=True, exist_ok=True) | ||
project_data = get_project_info() | ||
subprocess.run( | ||
[ | ||
"pybabel", | ||
"extract", | ||
"-F", | ||
MAPPING_FILE, | ||
"--copyright-holder", | ||
COPYRIGHT_HOLDER, | ||
"--project", | ||
project_data["name"], | ||
"--version", | ||
project_data["version"], | ||
"--msgid-bugs-address", | ||
project_data["urls"]["Issue tracker"], | ||
"-o", | ||
POT_FILE, | ||
SOURCE_DIR, | ||
], | ||
cwd=PROJECT_DIR, | ||
check=True, | ||
) | ||
def init_locale(locale: str) -> None: | ||
"""Initialize a new locale based on existing message catalog template""" | ||
pofile = PROJECT_DIR / LOCALES_DIR / locale / "LC_MESSAGES" / f"{DOMAIN}.po" | ||
if pofile.exists(): | ||
print(f"There is already a message catalog for locale {locale}, skipping.") | ||
return | ||
cmd = ["pybabel", "init", "-i", POT_FILE, "-d", LOCALES_DIR, "-l", locale] | ||
subprocess.run(cmd, cwd=PROJECT_DIR, check=True) | ||
def update_catalogs(locale: str) -> None: | ||
"""Update translations from existing message catalogs""" | ||
cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR] | ||
if locale: | ||
cmd.extend(["-l", locale]) | ||
subprocess.run(cmd, cwd=PROJECT_DIR, check=True) | ||
def compile_catalogs(locale: str) -> None: | ||
"""Compile existing message catalogs""" | ||
cmd = ["pybabel", "compile", "-d", LOCALES_DIR] | ||
if locale: | ||
cmd.extend(["-l", locale]) | ||
subprocess.run(cmd, cwd=PROJECT_DIR, check=True) | ||
def main() -> None: | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"command", | ||
choices=["extract", "init", "update", "compile"], | ||
help="command to be executed", | ||
) | ||
parser.add_argument( | ||
"-l", | ||
"--locale", | ||
default="", | ||
help="language code (needed for init, optional for update and compile)", | ||
) | ||
args = parser.parse_args() | ||
locale = args.locale | ||
if args.command == "extract": | ||
extract_messages() | ||
elif args.command == "init": | ||
if not locale: | ||
parser.error("init requires passing the --locale option") | ||
init_locale(locale) | ||
elif args.command == "update": | ||
update_catalogs(locale) | ||
elif args.command == "compile": | ||
compile_catalogs(locale) | ||
if __name__ == "__main__": | ||
main() |
5 changes: 5 additions & 0 deletionsrequirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# for babel_runner.py | ||
setuptools | ||
Babel | ||
Jinja2 | ||
tomli; python_version < "3.10" |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.