calligra_xgettext.sh 3.57 KB
Newer Older
1
#
2 3 4 5 6 7
# Helper function for extracting translatable messages from Calligra source code.
# Usage: calligra_xgettext <pot-filename-without-path> <source-files-list>
# If there are no messages or the <source-files-list> is empty, the pot file is deleted.
#
# Example usage that creates $podir/myapp.pot file:
#     calligra_xgettext myapp.pot `find . -name \*.cpp -o -name \*.h`
8 9
#
function calligra_xgettext() {
10 11 12 13 14 15 16 17 18
    POTFILE="$podir/$1"
    shift
    if test -n "$*"; then
        calligra_xgettext_internal $* | tee "${POTFILE}" | tail -n1 | grep "^msgstr \"\"\$" > /dev/null \
            || rm -f "${POTFILE}" 2> /dev/null
    fi
}

function calligra_xgettext_internal() {
19 20 21
    SRC_FILES="$*"
    POT_PART_NORMAL="`mktemp $podir/_normal_XXXXXXXX.pot`"
    POT_PART_QUNDOFORMAT="`mktemp $podir/_qundoformat_XXXXXXXX.pot`"
22
    POT_PART_QUNDOFORMAT2="`mktemp $podir/_qundoformat2_XXXXXXXX.pot`"
23 24
    POT_MERGED="`mktemp $podir/_merged_XXXXXXXX.pot`"

25
    $XGETTEXT ${CXG_EXTRA_ARGS} ${SRC_FILES} -o "${POT_PART_NORMAL}" --force-po
26 27 28 29 30 31 32
    $XGETTEXT_PROGRAM --from-code=UTF-8 -C --kde -kkundo2_i18n:1 -kkundo2_i18np:1,2 -kkundo2_i18nc:1c,2 -kkundo2_i18ncp:1c,2,3 ${CXG_EXTRA_ARGS} ${SRC_FILES} -o "${POT_PART_QUNDOFORMAT}"

    if [ $(cat ${POT_PART_NORMAL} ${POT_PART_QUNDOFORMAT} | grep -c \(qtundo-format\)) != 0 ]; then
        echo "ERROR: Context '(qtundo-format)' should not be added manually. Use kundo2_i18n*() calls instead." 1>&2
        exit 17
    fi

33
    if [ -s "${POT_PART_QUNDOFORMAT}" ]; then
34 35 36
        # Prepend "(qtundo-format)" to existing msgctxt properties of messages
        sed -i -e 's/^msgctxt "/msgctxt "(qtundo-format) /' "${POT_PART_QUNDOFORMAT}"

37
        # Add msgctxt "(qtundo-format)" to messages not having msgctxt yet
38 39 40 41 42 43 44 45 46 47
        #
        # lastLine != "#, fuzzy" is the check for the .pot header.
        mv "${POT_PART_QUNDOFORMAT}" "${POT_PART_QUNDOFORMAT2}"
        cat "${POT_PART_QUNDOFORMAT2}" | awk '
            /^msgid "/ {
                if (lastLine !~ /^msgctxt/ && lastLine != "#, fuzzy") {
                    print "msgctxt \"(qtundo-format)\""
                }
            }
            { print ; lastLine = $0 }' > "${POT_PART_QUNDOFORMAT}"
48 49
    fi

50
    if [ -s "${POT_PART_NORMAL}" -a -s "${POT_PART_QUNDOFORMAT}" ]; then
51 52 53 54 55 56 57 58 59
        ${MSGCAT} -F "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" > ${POT_MERGED}
        MERGED_HEADER_LINE_COUNT=$(cat ${POT_MERGED} | grep "^$" -B 100000 --max-count=1 | wc -l)

        KDE_HEADER="$(cat ${POT_PART_NORMAL} | grep "^$" -B 100000 --max-count=1)"
        MERGED_TAIL="$(cat ${POT_MERGED} | tail -n +$MERGED_HEADER_LINE_COUNT)"

        # Print out the resulting .pot
        echo "$KDE_HEADER"
        echo "$MERGED_TAIL"
60
    elif [ -s "${POT_PART_NORMAL}" ]; then
61
        cat "${POT_PART_NORMAL}"
62
    elif [ -s "${POT_PART_QUNDOFORMAT}" ]; then
63 64 65
        cat "${POT_PART_QUNDOFORMAT}"
    fi

66
    rm -f "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" "${POT_PART_QUNDOFORMAT2}" "${POT_MERGED}"
67
}
68 69 70 71 72 73 74 75 76 77 78 79 80 81

# Sets EXCLUDE variable to excludes compatible with the find(1) command, e.g. '-path a -o -path b'.
# To unconditionally exclude dir (with subdirs) just put an empty file .i18n in it.
# To exclude dir for all translations but one, e.g. foo.pot, put a single "foo" line into the .i18n file.
function find_exclude() {
    EXCLUDE=""
    for f in `find . -name .i18n | sed 's/\/\.i18n$//g' | sort`; do
        if ! grep -q "^${1}$" "$f/.i18n" ; then
            if [ -n "$EXCLUDE" ] ; then EXCLUDE="$EXCLUDE -o " ; fi
            EXCLUDE="$EXCLUDE -path $f"
        fi
    done
    if [ -z "$EXCLUDE" ] ; then EXCLUDE="-path __dummy__" ; fi # needed because -prune in find needs args
}