message ("Process dtk translation")
message ("Add DTK_NO_TRANSLATION=YES to qmake arguments skip this")
message ("Add DTK_STATIC_TRANSLATION=YES to add support static translations")

# TODO: check lrelease/lupdate exist

ROOT_DIR = $$_PRO_FILE_PWD_

BASENAME = $$basename(_PRO_FILE_)
BASENAME = $$split(BASENAME, .)
BASENAME = $$first(BASENAME)

isEmpty(DTK_NO_TRANSLATION) {
#    system(lupdate $$_PRO_FILE_) | error("Failed to lupdate")

    TRANSLATIONS = $$files($$ROOT_DIR/translations/*.ts)
    for(tsfile, TRANSLATIONS) {
        qmfile = $$replace(tsfile, .ts$, .qm)
        system(lrelease $$tsfile -qm $$qmfile) | error("Failed to lrelease")
    }

    # create static translations qrc file
    !isEmpty(DTK_STATIC_TRANSLATION) {
        # qrc template
        #<RCC>
        #    <qresource prefix="/dtk/translations">
        #        <file>dtkwidget_am_ET.qm</file>
        #        <file>dtkwidget_ar.qm</file>
        #        <file>dtkwidget_ast.qm</file>
        #    </qresource>
        #</RCC>
        QRC_PATH = $$ROOT_DIR/translations/$${BASENAME}_translations.qrc
        message ("Create static translations file $${QRC_PATH}")

        QRC_CONTENT = "<RCC>"
        QRC_CONTENT += "    <qresource prefix=\"/dtk/translations\">"

        QM_FILE_LIST = $$files($$ROOT_DIR/translations/*.qm)
        for(qmfile, QM_FILE_LIST) {
            QRC_CONTENT += "        <file>$$basename(qmfile)</file>"
        }
        QRC_CONTENT += "    </qresource>"
        QRC_CONTENT += "</RCC>"
        write_file($$QRC_PATH, QRC_CONTENT) | error("Aborting.")
    }
}

!isEmpty(DTK_STATIC_TRANSLATION) {
    QRC_PATH = $$ROOT_DIR/translations/$${TARGET}_translations.qrc
    RESOURCES += $$QRC_PATH
}

TRANSLATIONS += $$ROOT_DIR/translations/*

TRANSLATIONS_DIR = $$LIB_VERSION_NAME/$$DMODULE_NAME/translations
TRANSLATIONS_PATH = $$PREFIX/share/$$TRANSLATIONS_DIR
DEFINES += $$upper($$DMODULE_NAME)_TRANSLATIONS_PATH=\\\"$$TRANSLATIONS_PATH\\\"
DEFINES += $$upper($$DMODULE_NAME)_TRANSLATIONS_DIR=\\\"$$TRANSLATIONS_DIR\\\"

dtk_translations.path = $$TRANSLATIONS_PATH
dtk_translations.files = $$ROOT_DIR/translations/*.qm

INSTALLS += dtk_translations
