pytho****@googl*****
pytho****@googl*****
2012年 10月 13日 (土) 23:35:18 JST
Revision: 7f72f47c1639 Branch: default Author: Arihiro TAKASE <hinac****@gmail*****> Date: Sat Oct 13 07:32:07 2012 Log: ファイル追加: tools/i18nutil.py http://code.google.com/p/python-doc-ja/source/detail?r=7f72f47c1639&repo=py33 Added: /tools/i18nutil.py ======================================= --- /dev/null +++ /tools/i18nutil.py Sat Oct 13 07:32:07 2012 @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- + +import os +import os.path +import itertools +import sys +import argparse + +import polib + +join = os.path.join + +maindir = join(os.path.abspath(os.path.dirname(__file__)), os.pardir) + +txpydir = join(maindir, 'locale') +localedir = join(maindir, 'locale', 'ja', 'LC_MESSAGES') +htmldir = join(maindir, 'build', 'html') +i18ndir = join(maindir, 'build', 'en') + +# 一つの po ファイルからなるパート +SINGLE = ['about', 'bugs', 'contents', 'copyright', 'glossary', 'license'] +# 複数の po ファイルからなるパート +MULTI = ['c-api', 'distutils', 'extending', 'faq', + 'howto', 'install', 'library', 'reference', 'tutorial', + 'using', 'whatsnew'] + +HEAD = '''[main] +host = https://www.transifex.net +''' + +FORM = ''' +[python-doc-ja.33-{0}] +file_filter = <lang>/{1}.po +source_file = en/{1}.po +type = PO +source_lang = en +''' + +def pofilename(entry): + return entry.occurrences[0][0].split('/')[-1].split('.rst')[0] + +def make_html(build=False, tx=False): + if tx: + os.system('tx -r {0} pull'.format(txpydir)) + + for part in SINGLE: + os.system('msgfmt -o {0}/{1}.mo {0}/{1}.po'.format(localedir, part)) + + for part in MULTI: + os.system('msgcat -o {0}/{1}.po --use-first {0}/{1}/*.po'.format(localedir, part)) + os.system('msgfmt -o {0}/{1}.mo {0}/{1}.po'.format(localedir, part)) + + if build: + os.system('sphinx-build -E {0} {1}'.format(maindir, htmldir)) + +def make_gettext(build=False, tx=False): + if build: + os.system('sphinx-build -b gettext -E {0} {1}'.format(maindir, i18ndir)) + + for part in SINGLE + MULTI: + os.system('msginit -i {0}/{1}.pot -o {0}/{1}.po -l ja --no-translator'.format(i18ndir, part)) + + if tx: + try: + os.makedirs('{}/build/.tx'.format(maindir)) + except OSError: + pass + + with open('{}/build/.tx/config'.format(maindir), 'w') as config: + config.write(HEAD) + for part in SINGLE: + config.write(FORM.format(part, part)) + for part in MULTI: + os.rename('{}/{}.po'.format(i18ndir, part), '{}/_{}.po'.format(i18ndir, part)) + try: + os.makedirs('{}/{}'.format(i18ndir, part)) + except OSError: + pass + orig = polib.pofile('{}/_{}.po'.format(i18ndir, part)) + meta = orig.metadata + for piece, entries in itertools.groupby(orig, key=pofilename): + new = polib.POFile() + new.metadata = meta + new.extend(entries) + new.save('{}/{}/{}.po'.format(i18ndir, part, piece)) + config.write(FORM.format( + '{}-{}'.format(part, piece).replace('.', '_'), + '{}/{}'.format(part, piece) + )) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('-w', '--work', default='html') + parser.add_argument('-b', '--build', action='store_true') + parser.add_argument('-t', '--tx', action='store_true') + args = parser.parse_args() + if args.work == 'html': + make_html(args.build, args.tx) + elif args.work == 'i18n': + make_gettext(args.build, args.tx)