Changeset View
Changeset View
Standalone View
Standalone View
kundo2_aware_xgettext.sh
- This file was moved from krita_xgettext.sh.
1 | # | 1 | # | ||
---|---|---|---|---|---|
2 | # Helper function for extracting translatable messages from krita source code. | 2 | # Helper function for extracting translatable messages from Calligra/Krita/Kexi source code. | ||
3 | # Usage: krita_xgettext <pot-filename-without-path> <source-files-list> | 3 | # Usage: kundo2_aware_xgettext <pot-filename-without-path> <source-files-list> | ||
4 | # If there are no messages or the <source-files-list> is empty, the pot file is deleted. | 4 | # If there are no messages or the <source-files-list> is empty, the pot file is deleted. | ||
5 | # | 5 | # | ||
6 | # Example usage that creates $podir/myapp.pot file: | 6 | # Example usage that creates $podir/myapp.pot file: | ||
7 | # krita_xgettext myapp.pot `find . -name \*.cpp -o -name \*.h` | 7 | # kundo2_aware_xgettext myapp.pot `find . -name \*.cpp -o -name \*.h` | ||
8 | # | 8 | # | ||
9 | function krita_xgettext() { | 9 | function kundo2_aware_xgettext() { | ||
10 | POTFILE="$podir/$1" | 10 | POTFILE="$podir/$1" | ||
11 | shift | 11 | shift | ||
12 | if test -n "$*"; then | 12 | if test -n "$*"; then | ||
13 | krita_xgettext_internal $* | tee "${POTFILE}" | tail -n1 | grep "^msgstr \"\"\$" > /dev/null \ | 13 | # we rely on last line being a 'msgstr' signaling that strings has been extracted (a header is always present) | ||
14 | # normally it ends with 'msgstr ""' but if plural it can end with eg 'msgstr[1] ""' | ||||
15 | kundo2_aware_xgettext_internal $* | tee "${POTFILE}" | tail -n1 | grep "^msgstr" > /dev/null \ | ||||
14 | || rm -f "${POTFILE}" 2> /dev/null | 16 | || rm -f "${POTFILE}" 2> /dev/null | ||
15 | fi | 17 | fi | ||
16 | } | 18 | } | ||
17 | 19 | | |||
18 | # How to unit test: | 20 | # How to unit test: | ||
19 | # export podir=. | 21 | # export podir=. | ||
20 | # cp init-sample.pot sample.pot | 22 | # cp init-sample.pot sample.pot | ||
21 | # source krita_xgettext.sh | 23 | # source krita_xgettext.sh | ||
22 | # add_ctxt_qtundo sample.pot | 24 | # add_ctxt_qtundo sample.pot | ||
23 | # | 25 | # | ||
24 | # Then check that all messages in sample.pot have "(qtundo-format)" in msgctxt. | 26 | # Then check that all messages in sample.pot have "(qtundo-format)" in msgctxt. | ||
25 | function add_ctxt_qtundo() { | 27 | function add_ctxt_qtundo() { | ||
26 | POT_PART_QUNDOFORMAT="$1" | 28 | POT_PART_QUNDOFORMAT="$1" | ||
27 | POT_PART_QUNDOFORMAT2="`mktemp $podir/_qundoformat2_XXXXXXXX.pot`" | 29 | POT_PART_QUNDOFORMAT2="`mktemp $podir/_qundoformat2_XXXXXXXX.pot`" | ||
28 | 30 | | |||
29 | # Prepend "(qtundo-format)" to existing msgctxt properties of messages | 31 | # Prepend "(qtundo-format)" to existing msgctxt properties of messages | ||
30 | sed -i -e 's/^msgctxt "/msgctxt "(qtundo-format) /' "${POT_PART_QUNDOFORMAT}" | 32 | sed -i -e 's/^msgctxt "/msgctxt "(qtundo-format) /' "${POT_PART_QUNDOFORMAT}" | ||
31 | 33 | | |||
32 | # Add msgctxt "(qtundo-format)" to messages not having msgctxt yet | 34 | # Add msgctxt "(qtundo-format)" to messages not having msgctxt yet | ||
33 | # | 35 | # | ||
34 | # lastLine != "#, fuzzy" is the check for the .pot header. | 36 | # lastLine != "#, fuzzy" is the check for the .pot header. | ||
37 | # If lastLine starts with '"' the msgctxt has been split on several lines and is treated by sed above, so skip it | ||||
35 | mv "${POT_PART_QUNDOFORMAT}" "${POT_PART_QUNDOFORMAT2}" | 38 | mv "${POT_PART_QUNDOFORMAT}" "${POT_PART_QUNDOFORMAT2}" | ||
36 | cat "${POT_PART_QUNDOFORMAT2}" | awk ' | 39 | cat "${POT_PART_QUNDOFORMAT2}" | awk ' | ||
37 | /^msgid "/ { | 40 | /^msgid "/ { | ||
38 | if (lastLine !~ /^msgctxt/ && lastLine !~ /^"/ && lastLine != "#, fuzzy") { | 41 | if (lastLine !~ /^\"/ && lastLine !~ /^msgctxt/ && lastLine != "#, fuzzy") { | ||
39 | print "msgctxt \"(qtundo-format)\"" | 42 | print "msgctxt \"(qtundo-format)\"" | ||
40 | } | 43 | } | ||
41 | } | 44 | } | ||
42 | { print ; lastLine = $0 }' > "${POT_PART_QUNDOFORMAT}" | 45 | { print ; lastLine = $0 }' > "${POT_PART_QUNDOFORMAT}" | ||
43 | 46 | | |||
44 | rm -f "${POT_PART_QUNDOFORMAT2}" | 47 | rm -f "${POT_PART_QUNDOFORMAT2}" | ||
45 | } | 48 | } | ||
46 | 49 | | |||
47 | function krita_xgettext_internal() { | 50 | function kundo2_aware_xgettext_internal() { | ||
48 | SRC_FILES="$*" | 51 | SRC_FILES="$*" | ||
49 | POT_PART_NORMAL="`mktemp $podir/_normal_XXXXXXXX.pot`" | 52 | POT_PART_NORMAL="`mktemp $podir/_normal_XXXXXXXX.pot`" | ||
50 | POT_PART_QUNDOFORMAT="`mktemp $podir/_qundoformat_XXXXXXXX.pot`" | 53 | POT_PART_QUNDOFORMAT="`mktemp $podir/_qundoformat_XXXXXXXX.pot`" | ||
51 | POT_MERGED="`mktemp $podir/_merged_XXXXXXXX.pot`" | 54 | POT_MERGED="`mktemp $podir/_merged_XXXXXXXX.pot`" | ||
52 | 55 | | |||
53 | $XGETTEXT ${CXG_EXTRA_ARGS} ${SRC_FILES} -o "${POT_PART_NORMAL}" --force-po | 56 | $XGETTEXT ${CXG_EXTRA_ARGS} ${SRC_FILES} -o "${POT_PART_NORMAL}" --force-po | ||
54 | $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}" | 57 | | ||
58 | XGETTEXT_FLAGS_KUNDO2="\ | ||||
59 | --copyright-holder=This_file_is_part_of_KDE \ | ||||
60 | --msgid-bugs-address=http://bugs.kde.org \ | ||||
61 | --from-code=UTF-8 | ||||
62 | -C -k --kde \ | ||||
63 | -kkundo2_i18n:1 -kkundo2_i18np:1,2 -kkundo2_i18nc:1c,2 -kkundo2_i18ncp:1c,2,3 \ | ||||
64 | " | ||||
65 | | ||||
66 | $XGETTEXT_PROGRAM ${XGETTEXT_FLAGS_KUNDO2} ${CXG_EXTRA_ARGS} ${SRC_FILES} -o "${POT_PART_QUNDOFORMAT}" | ||||
55 | 67 | | |||
56 | if [ $(cat ${POT_PART_NORMAL} ${POT_PART_QUNDOFORMAT} | grep -c \(qtundo-format\)) != 0 ]; then | 68 | if [ $(cat ${POT_PART_NORMAL} ${POT_PART_QUNDOFORMAT} | grep -c \(qtundo-format\)) != 0 ]; then | ||
57 | echo "ERROR: Context '(qtundo-format)' should not be added manually. Use kundo2_i18n*() calls instead." 1>&2 | 69 | echo "ERROR: Context '(qtundo-format)' should not be added manually. Use kundo2_i18n*() calls instead." 1>&2 | ||
58 | exit 17 | 70 | exit 17 | ||
59 | fi | 71 | fi | ||
60 | 72 | | |||
61 | if [ -s "${POT_PART_QUNDOFORMAT}" ]; then | 73 | if [ -s "${POT_PART_QUNDOFORMAT}" ]; then | ||
62 | add_ctxt_qtundo "${POT_PART_QUNDOFORMAT}" | 74 | add_ctxt_qtundo "${POT_PART_QUNDOFORMAT}" | ||
63 | fi | 75 | fi | ||
64 | 76 | | |||
65 | if [ -s "${POT_PART_NORMAL}" -a -s "${POT_PART_QUNDOFORMAT}" ]; then | 77 | if [ -s "${POT_PART_NORMAL}" -a -s "${POT_PART_QUNDOFORMAT}" ]; then | ||
78 | # ensure an empty line or else KDE_HEADER search will fail | ||||
79 | # in case POT_PART_NORMAL only contains header | ||||
80 | echo "" >>${POT_PART_NORMAL} | ||||
81 | | ||||
66 | ${MSGCAT} -F "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" > ${POT_MERGED} | 82 | ${MSGCAT} -F "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" > ${POT_MERGED} | ||
67 | MERGED_HEADER_LINE_COUNT=$(cat ${POT_MERGED} | grep "^$" -B 100000 --max-count=1 | wc -l) | 83 | MERGED_HEADER_LINE_COUNT=$(cat ${POT_MERGED} | grep "^$" -B 100000 --max-count=1 | wc -l) | ||
68 | | ||||
69 | KDE_HEADER="$(cat ${POT_PART_NORMAL} | grep "^$" -B 100000 --max-count=1)" | 84 | KDE_HEADER="$(cat ${POT_PART_NORMAL} | grep "^$" -B 100000 --max-count=1)" | ||
70 | MERGED_TAIL="$(cat ${POT_MERGED} | tail -n +$MERGED_HEADER_LINE_COUNT)" | 85 | MERGED_TAIL="$(cat ${POT_MERGED} | tail -n +$MERGED_HEADER_LINE_COUNT)" | ||
71 | 86 | | |||
72 | # Print out the resulting .pot | 87 | # Print out the resulting .pot | ||
73 | echo "$KDE_HEADER" | 88 | echo "$KDE_HEADER" | ||
74 | echo "$MERGED_TAIL" | 89 | echo "$MERGED_TAIL" | ||
75 | elif [ -s "${POT_PART_NORMAL}" ]; then | 90 | elif [ -s "${POT_PART_NORMAL}" ]; then | ||
91 | echo "# POT_PART_NORMAL only" | ||||
76 | cat "${POT_PART_NORMAL}" | 92 | cat "${POT_PART_NORMAL}" | ||
77 | elif [ -s "${POT_PART_QUNDOFORMAT}" ]; then | 93 | elif [ -s "${POT_PART_QUNDOFORMAT}" ]; then | ||
94 | echo "# POT_PART_QUNDOFORMAT only" | ||||
78 | cat "${POT_PART_QUNDOFORMAT}" | 95 | cat "${POT_PART_QUNDOFORMAT}" | ||
79 | fi | 96 | fi | ||
80 | 97 | | |||
81 | rm -f "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" "${POT_MERGED}" | 98 | rm -f "${POT_PART_NORMAL}" "${POT_PART_QUNDOFORMAT}" "${POT_MERGED}" | ||
82 | } | 99 | } | ||
83 | 100 | | |||
84 | # Sets EXCLUDE variable to excludes compatible with the find(1) command, e.g. '-path a -o -path b'. | 101 | # Sets EXCLUDE variable to excludes compatible with the find(1) command, e.g. '-path a -o -path b'. | ||
85 | # To unconditionally exclude dir (with subdirs) just put an empty file .i18n in it. | 102 | # To unconditionally exclude dir (with subdirs) just put an empty file .i18n in it. | ||
86 | # To exclude dir for all translations but one, e.g. foo.pot, put a single "foo" line into the .i18n file. | 103 | # To disable excluding for given file, e.g. foo.pot, add "foo.pot" line to the .i18n file. | ||
87 | function find_exclude() { | 104 | function find_exclude() { | ||
88 | EXCLUDE="" | 105 | EXCLUDE="" | ||
89 | for f in `find . -name .i18n | sed 's/\/\.i18n$//g' | sort`; do | 106 | for f in `find . -name .i18n | sed 's/\/\.i18n$//g' | sort`; do | ||
90 | if ! grep -q "^${1}$" "$f/.i18n" ; then | 107 | if ! grep -q "^${1}$" "$f/.i18n" ; then | ||
91 | if [ -n "$EXCLUDE" ] ; then EXCLUDE="$EXCLUDE -o " ; fi | 108 | if [ -n "$EXCLUDE" ] ; then EXCLUDE="$EXCLUDE -o " ; fi | ||
92 | EXCLUDE="$EXCLUDE -path $f" | 109 | EXCLUDE="$EXCLUDE -path $f" | ||
93 | fi | 110 | fi | ||
94 | done | 111 | done | ||
95 | if [ -z "$EXCLUDE" ] ; then EXCLUDE="-path __dummy__" ; fi # needed because -prune in find needs args | 112 | if [ -z "$EXCLUDE" ] ; then EXCLUDE="-path __dummy__" ; fi # needed because -prune in find needs args | ||
96 | } | 113 | } |