forked from hongquan/CoBang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devtool.py
executable file
·75 lines (60 loc) · 2.12 KB
/
devtool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import subprocess
from pathlib import Path
import click
from cobang.consts import SHORT_NAME
ROOT = Path(__file__).parent
@click.group()
def cli():
pass
@cli.command()
def extract_translation():
filepath = ROOT / 'po' / f'{SHORT_NAME}.pot'
cmd = ('pybabel', 'extract', '-F', 'babel.cfg', '-o', filepath, '.')
subprocess.run(cmd)
@cli.command()
@click.option('--locale', '-l', 'locales', multiple=True)
def update_translation(locales: str):
valid_locales = {}
if not locales:
existing_files = ROOT.joinpath('po').glob('*.po')
for filepath in existing_files:
lo = filepath.with_suffix('').name
valid_locales[lo] = filepath
else:
for lo in locales:
filepath = ROOT / 'po' / f'{lo}.po'
if filepath.exists():
valid_locales[lo] = filepath
if not valid_locales:
click.echo('Locale files do not exist', err=True)
return
pot_path = ROOT / 'po' / f'{SHORT_NAME}.pot'
for lo, filepath in valid_locales.items():
cmd = ('pybabel', 'update', '-D', SHORT_NAME, '-l', lo, '-i', pot_path, '-o', filepath)
subprocess.run(cmd)
@cli.command()
@click.option('--locale', '-l', 'locales', multiple=True)
def compile_translation(locales: str):
valid_locales = {}
if not locales:
existing_files = ROOT.joinpath('po').glob('*.po')
for filepath in existing_files:
lo = filepath.with_suffix('').name
valid_locales[lo] = filepath
else:
for lo in locales:
filepath = ROOT / 'po' / f'{lo}.po'
if filepath.exists():
valid_locales[lo] = filepath
if not valid_locales:
click.echo('Locale files do not exist', err=True)
return
for lo, filepath in valid_locales.items():
mo_folder: Path = ROOT / 'po' / lo / 'LC_MESSAGES'
if not mo_folder.exists():
mo_folder.mkdir(parents=True, exist_ok=True)
cmd = ('pybabel', 'compile', '-D', SHORT_NAME, '-l', lo, '-i', filepath, '-d', 'po')
subprocess.run(cmd)
if __name__ == '__main__':
cli()