Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c53d33

Browse files
authored
Merge pull request#195 from rffontenelle/add-localization
2 parents3e81cef +2aa3a04 commit3c53d33

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

‎.babel.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[jinja2: **.html]

‎.github/workflows/tests.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,41 @@ jobs:
4848
with:
4949
name:doc-html-${{ matrix.branch }}
5050
path:www/
51+
52+
translations:
53+
runs-on:${{ matrix.os }}
54+
strategy:
55+
fail-fast:false
56+
matrix:
57+
os:["ubuntu-latest", "windows-latest"]
58+
# Test minimum supported and latest stable from 3.x series
59+
python-version:["3.9", "3"]
60+
steps:
61+
-uses:actions/checkout@v4
62+
-uses:actions/setup-python@v5
63+
with:
64+
python-version:${{ matrix.python-version }}
65+
allow-prereleases:true
66+
cache:pip
67+
-name:Install dependencies
68+
run:|
69+
pip install --upgrade pip
70+
pip install -r requirements.txt
71+
-name:Remove locale file for testing
72+
shell:bash
73+
run:rm -rf locales/pt_BR/
74+
-run:python babel_runner.py extract
75+
-run:python babel_runner.py init -l pt_BR
76+
-run:python babel_runner.py update
77+
-run:python babel_runner.py update -l pt_BR
78+
-run:python babel_runner.py compile
79+
-run:python babel_runner.py compile -l pt_BR
80+
-name:Print .pot file
81+
shell:bash
82+
run:cat locales/messages.pot
83+
-name:Print .po file
84+
shell:bash
85+
run:cat locales/pt_BR/LC_MESSAGES/messages.po
86+
-name:list files in locales dir
87+
shell:bash
88+
run:ls -R locales/

‎babel_runner.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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()

‎requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# for babel_runner.py
2+
setuptools
3+
Babel
4+
Jinja2
5+
tomli;python_version<"3.10"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp