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

Commitd271fac

Browse files
committed
Add script for handling translations
1 parentf1ba0f8 commitd271fac

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-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]

‎babel_runner.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/venv python3
2+
"""Script for handling translations with Babel"""
3+
4+
importargparse
5+
importos
6+
importsubprocess
7+
importtomllib
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+
defget_project_info()->dict:
23+
"""Retrieve project's info to populate the message catalog template"""
24+
withopen(os.path.join(PROJECT_DIR,"pyproject.toml"),"rb")asf:
25+
data=tomllib.load(f)
26+
returndata["project"]
27+
28+
29+
defextract_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+
definit_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+
defupdate_catalogs(locale:str):
62+
"""Update translations from existing message catalogs"""
63+
cmd= ["pybabel","update","-i",POT_FILE,"-d",LOCALES_DIR]
64+
iflocale!="":
65+
cmd.append(["-l",locale])
66+
subprocess.run(cmd,check=True)
67+
68+
69+
defcompile_catalogs(locale:str):
70+
"""Compile existing message catalogs"""
71+
cmd= ["pybabel","compile","-d",LOCALES_DIR]
72+
iflocale!="":
73+
cmd.append(["-l",locale])
74+
subprocess.run(cmd,check=True)
75+
76+
77+
defmain():
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.localeifargs.localeelse""
92+
93+
os.chdir(PROJECT_DIR)
94+
95+
ifargs.command=="extract":
96+
extract_messages()
97+
elifargs.command=="init":
98+
iflocale=="":
99+
parser.error("init requires passing the --locale option")
100+
init_locale(locale)
101+
elifargs.command=="update":
102+
update_catalogs(locale)
103+
elifargs.command=="compile":
104+
compile_catalogs(locale)
105+
106+
107+
if__name__=="__main__":
108+
main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp