-
-
Notifications
You must be signed in to change notification settings - Fork 66
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d271fac
Add script for handling translations
rffontenelle 634a20c
Make ruff tests happy
rffontenelle a809821
Rename mapping file to .babel.cfg
rffontenelle adf715c
Forbid initializing existent po
rffontenelle 66820a5
Extend instead of append list for locale arg
rffontenelle 6b6f0d2
Add translation tests to tests.yml
rffontenelle 7878f4e
Use python instead of python3 to run babel_runner.py
rffontenelle 1fbd5d0
Replace os with pahlib in babe_runner.py
rffontenelle e3e08eb
Add tomli import to support python<3.11
rffontenelle a845398
CI test for minimum supported python version
rffontenelle 541c7a5
Merge branch 'main' into add-localization
hugovk bd7c059
Apply suggestions from code review
rffontenelle 65a0360
Remove possibly existing translation file
rffontenelle 69ab917
Add shell bash to success in Windows
rffontenelle 2aa3a04
Swap init and extract to be didactic
rffontenelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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] |
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,108 @@ | ||
#!/usr/bin/venv python3 | ||
"""Script for handling translations with Babel""" | ||
|
||
import argparse | ||
import os | ||
import subprocess | ||
import tomllib | ||
|
||
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
# Global variables used by pybabel below | ||
DOMAIN = "messages" | ||
COPYRIGHT_HOLDER = "Python Software Foundation" | ||
LOCALES_DIR = os.path.relpath(os.path.join(PROJECT_DIR, "locales")) | ||
POT_FILE = os.path.relpath(os.path.join(LOCALES_DIR, f"{DOMAIN}.pot"), PROJECT_DIR) | ||
SOURCE_DIR = os.path.relpath( | ||
os.path.join(PROJECT_DIR, "python_docs_theme"), PROJECT_DIR | ||
) | ||
MAPPING_FILE = os.path.relpath(os.path.join(PROJECT_DIR, "babel.cfg"), PROJECT_DIR) | ||
|
||
|
||
def get_project_info() -> dict: | ||
"""Retrieve project's info to populate the message catalog template""" | ||
with open(os.path.join(PROJECT_DIR, "pyproject.toml"), "rb") as f: | ||
data = tomllib.load(f) | ||
return data["project"] | ||
|
||
|
||
def extract_messages(): | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Extract messages from all source files into template file""" | ||
os.makedirs(LOCALES_DIR, 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, | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
def init_locale(locale: str): | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Initialize a new locale based on existing""" | ||
cmd = ["pybabel", "init", "-i", POT_FILE, "-d", LOCALES_DIR, "-l", locale] | ||
subprocess.run(cmd, check=True) | ||
|
||
|
||
def update_catalogs(locale: str): | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Update translations from existing message catalogs""" | ||
cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR] | ||
if locale != "": | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.append(["-l", locale]) | ||
subprocess.run(cmd, check=True) | ||
|
||
|
||
def compile_catalogs(locale: str): | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Compile existing message catalogs""" | ||
cmd = ["pybabel", "compile", "-d", LOCALES_DIR] | ||
if locale != "": | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.append(["-l", locale]) | ||
subprocess.run(cmd, check=True) | ||
|
||
|
||
def main(): | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"command", | ||
choices=["init", "extract", "update", "compile"], | ||
help="command to be executed", | ||
) | ||
parser.add_argument( | ||
"-l", | ||
"--locale", | ||
help="language code (needed for init, optional for update and compile)", | ||
) | ||
|
||
args = parser.parse_args() | ||
locale = args.locale if args.locale else "" | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
os.chdir(PROJECT_DIR) | ||
|
||
if args.command == "extract": | ||
extract_messages() | ||
elif args.command == "init": | ||
if locale == "": | ||
rffontenelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.