Skip to content

Commit 8a3c589

Browse files
committed
Abstracted init.src_msgs()
1 parent 2cd92c2 commit 8a3c589

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

translate-messages/src/translate_messages/__main__.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
def main():
22
import sys
33
from pathlib import Path
4-
from .lib import data, init, language, log, wizard
4+
5+
from .lib import init, language, log, wizard
56

67
cli = init.cli(__file__)
78

@@ -20,21 +21,8 @@ def main():
2021
else:
2122
log.warn('Unable to locate directory.')
2223
sys.exit(1)
23-
24-
cli.msgs_filename = 'messages.json'
25-
cli.locales_path = Path(cli.config.locales_dir)
26-
cli.en_path = cli.locales_path / 'en' / cli.msgs_filename
27-
if not cli.en_path.exists():
28-
log.error(f'English locale not found at {cli.en_path}.')
29-
log.tip(f'Make sure {cli.en_path} exists!')
30-
sys.exit(1)
31-
try:
32-
cli.en_msgs = data.json.read(cli.en_path)
33-
except Exception as err:
34-
log.error(f'Failed to parse {cli.en_path}: {err}')
35-
log.tip('Make sure it contains valid JSON')
36-
sys.exit(1)
37-
24+
25+
init.src_msgs(cli)
3826
init.target_langs(cli)
3927

4028
langs_translated, langs_skipped, langs_added, langs_not_translated = language.write_translations(cli)

translate-messages/src/translate_messages/lib/init.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from pathlib import Path
2+
import sys
3+
24
from . import data, log, settings
35

46
def cli(caller_file):
@@ -29,14 +31,27 @@ def locales_dir(cli):
2931
return
3032
cli.config.locales_dir = None
3133

34+
def src_msgs(cli):
35+
cli.msgs_filename = 'messages.json'
36+
cli.locales_path = Path(cli.config.locales_dir)
37+
cli.en_path = cli.locales_path / 'en' / cli.msgs_filename
38+
if not cli.en_path.exists():
39+
log.error(f'English locale not found at {cli.en_path}.')
40+
log.tip(f'Make sure {cli.en_path} exists!')
41+
sys.exit(1)
42+
try:
43+
cli.en_msgs = data.json.read(cli.en_path)
44+
except Exception as err:
45+
log.error(f'Failed to parse {cli.en_path}: {err}')
46+
log.tip('Make sure it contains valid JSON')
47+
sys.exit(1)
48+
3249
def target_langs(cli):
3350
cli.config.target_langs = list(set(cli.config.target_langs)) # remove dupes
34-
3551
if not cli.config.target_langs:
3652
cli.config.target_langs = cli.supported_locales
3753
for lang_path in cli.locales_path.rglob(f'*/{cli.msgs_filename}'): # merge discovered locales
3854
discovered_lang = lang_path.parent.name.replace('_', '-')
3955
if discovered_lang not in cli.config.target_langs:
4056
cli.config.target_langs.append(discovered_lang)
41-
4257
cli.config.target_langs.sort()

0 commit comments

Comments
 (0)