diff --git a/scripts/README.md b/scripts/README.md index db0a0cd..ef3ec87 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -1,3 +1,4 @@ # Usage -[build_exiv2.sh](build_exiv2.sh): Used to build thirdparty library. -[get_cache_key.py](get_cache_key.py): Used to get cache's key which used in [action/cache](https://github.com/actions/cache). +`build_*.sh`: Used to build thirdparty library. +[get_cache_key.py](get_cache_key.py): Used to get cache's key which used in [action/cache](https://github.com/actions/cache). +[gen_mo.py](gen_mo.py): Used to generate .mo file. (i18n) diff --git a/scripts/gen_mo.py b/scripts/gen_mo.py new file mode 100644 index 0000000..855a71a --- /dev/null +++ b/scripts/gen_mo.py @@ -0,0 +1,44 @@ +from argparse import ArgumentParser +from os.path import abspath, dirname, exists, join +from os import listdir, makedirs +from sys import exit as _exit +from traceback import print_exc +from subprocess import Popen +from re import compile + + +def call(*cmd): + print(cmd) + p = Popen(cmd) + p.wait() + return p.returncode + + +try: + RE = compile(r'^pixiv_downloader\.(.*).po$') + p = ArgumentParser() + p.add_argument('-p', '--prefix', help='Translation will install at PREFIX/share/locale//LC_MESSAGES/pixiv_downloader.mo') # noqa: E501 + p.add_argument('-o', '--output', help='Output all translations file in a directory.') # noqa: E501 + d = abspath(join(dirname(__file__), "../Language")) + arg = p.parse_intermixed_args() + if arg.prefix is None and arg.output is None: + raise ValueError('--prefix or --output is needed.') + for i in listdir(d): + if not i.endswith('.po'): + continue + fn = join(d, i) + out = None + if arg.output: + out = join(arg.output, i.rstrip(".po") + ".mo") + elif arg.prefix: + out = join(arg.prefix, 'share/locale', RE.search(i).group(1), 'LC_MESSAGES/pixiv_downloader.mo') + if out is None: + raise ValueError('Failed to get output file name.') + out_dir = dirname(out) + if not exists(out_dir): + makedirs(out_dir, 755, exist_ok=True) + if call('msgfmt', fn, '--output', out, '--no-hash') != 0: + raise ValueError('Failed to run msgfmt.') +except Exception: + print_exc() + _exit(1)