gen_translations.py 2.27 KB
Newer Older
1
import sys, os, multiprocessing, subprocess, time, shutil
2

3
gen_resources = os.path.abspath(os.path.join(os.path.dirname(__file__), 'src/sakia'))
4 5
ts = os.path.abspath(os.path.join(os.path.dirname(__file__), 'res', 'i18n', 'ts'))
qm = os.path.abspath(os.path.join(os.path.dirname(__file__), 'res', 'i18n', 'qm'))
6 7
if not os.path.exists(qm):
    os.mkdir(qm)
8 9 10 11 12

translations = []
qm_files = []
qm_shortnames = []

13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
def prepare_qm():
    for root, dirs, files in os.walk(ts):
        for f in files:
            if f.endswith('.ts'):
                tsfilename = os.path.join(root, f)
                qmshort = "{0}qm".format(f[:-2])
                qmfilename = os.path.join(qm, qmshort)
                srcdest = (tsfilename, qmfilename)
                translations.append(srcdest)
                qm_shortnames.append(qmshort)
            else:
                continue
            print(os.path.join(root, f))

    for (ts_file, qm_file) in translations:
29
        # avoid conflict with qt4 lrelease by running qtchooser directly
inso's avatar
inso committed
30
        if sys.platform.startswith('win') or shutil.which("qtchooser") == None or "--lrelease" in sys.argv:
inso's avatar
inso committed
31 32 33
            subprocess.call(["lrelease", ts_file, "-qm", qm_file])
        else:
            subprocess.call(["qtchooser", "-run-tool=lrelease", "-qt=5", ts_file, "-qm", qm_file])
34 35
        print(ts_file + " >> " + qm_file)

36

37 38 39 40 41 42 43 44 45 46
def build_resources():
    files = ""
    for file in qm_shortnames:
        files += """
<file alias="{0}">qm/{0}.qm</file>""".format(file[:-3])
    rccfile = """<RCC>
      <qresource prefix="i18n">{0}
      </qresource>
    </RCC>
    """.format(files)
inso's avatar
inso committed
47
    print(rccfile)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    qrc_filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                'res',
                                                'i18n',
                                                'langs-{0}.qrc'.format(int(time.time()))
                                                ))

    pyc_filename = os.path.abspath(os.path.join(gen_resources, 'i18n_rc.py'))
    with open(qrc_filename, 'w') as outfile:
        outfile.write(rccfile)

    try:
        subprocess.call(["pyrcc5", "-o", pyc_filename, qrc_filename])
        print(qrc_filename + " >> " + pyc_filename)
    finally:
        os.remove(qrc_filename)


prepare_qm()
build_resources()