-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatch_build.py
51 lines (42 loc) · 1.67 KB
/
hatch_build.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
import json
import os
import subprocess
from contextlib import contextmanager
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
def initialize(self, version, build_data):
if os.environ.get('READTHEDOCS') == 'True' or version == 'editable':
return
if translations_dir := next(Path().glob('indico_*/translations/'), None):
_compile_languages(translations_dir)
_compile_languages_react(translations_dir)
def _compile_languages(translations_dir: Path):
from babel.messages.frontend import CompileCatalog
if not any(translations_dir.glob('**/*.po')):
return
cmd = CompileCatalog()
cmd.directory = translations_dir
cmd.finalize_options()
cmd.use_fuzzy = True
cmd.run()
def _compile_languages_react(translations_dir: Path):
# we assume a ..../indico/{src,plugins/whatever}/ structure for indico and plugin repos
indico_root = Path('../../../src/').absolute().resolve()
for subdir in translations_dir.absolute().iterdir():
po_file = subdir / 'LC_MESSAGES' / 'messages-react.po'
json_file = subdir / 'LC_MESSAGES' / 'messages-react.json'
if not po_file.exists():
continue
with _chdir(indico_root):
output = subprocess.check_output(['npx', 'react-jsx-i18n', 'compile', po_file], stderr=subprocess.DEVNULL)
json.loads(output) # just to be sure the JSON is valid
json_file.write_bytes(output)
@contextmanager
def _chdir(path: Path):
old_path = Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_path)