Add a script to prepare desktop files for translation.

Signed-off-by: Slávek Banko <slavek.banko@axis.cz>
pull/3/head
Slávek Banko 4 years ago
parent a7c35253fa
commit 7b46022903
Signed by: SlavekB
GPG Key ID: 608F5293A04BE668

@ -0,0 +1,153 @@
#!/bin/sh
# default keyword
KEYWORDS="Name GenericName Comment Keywords Description ExtraNames X-TDE-Submenu"
# parse arguments
while [ $# -gt 0 ]; do
key="$1"
case $key in
-k*)
if [ "${1#-k}" = "" ]; then
KEYWORD="$2"
shift
else
KEYWORD=${1#-k}
fi
if [ "$KEYWORD" = "-" ]; then
KEYWORDS=""
else
KEYWORDS="$KEYWORDS $KEYWORD"
fi
;;
-o*)
if [ "${1#-o}" = "" ]; then
POT="$2"
shift
else
POT=${1#-o}
fi
;;
*)
[ -f "$1" ] && \
D+=("$1")
;;
esac
shift
done
set -- "${D[@]}"
if [ -z "$1" ]; then
echo "No desktop file specified for processing. Exiting..."
exit 1
fi
if [ -z "$KEYWORDS" ]; then
echo "No keywords specified to extract. Exiting..."
exit 1
fi
# determine path for translations
TRANSLATIONS_DIR="translations/desktop_files"
if [ -n "`git rev-parse --git-dir 2>/dev/null`" ]; then
TRANSLATIONS_DIR="$(git rev-parse --show-toplevel)/${TRANSLATIONS_DIR}"
APPNAME=$(basename $(git rev-parse --show-toplevel))
fi
# determine POT name
if [ -z "$POT" ]; then
if [ $# -gt 1 ]; then
POT="$APPNAME-desktops"
else
POT="$(basename "$1")/"
fi
fi
if [ "${POT%/}" != "${POT}" ]; then
TRANSLATIONS_DIR="$TRANSLATIONS_DIR/${POT%/}"
POT=${POT%/}
fi
if [ "${POT%.pot}" = "${POT}" ]; then
POT=${POT}.pot
fi
[ -d "$TRANSLATIONS_DIR" ] || \
mkdir -p "$TRANSLATIONS_DIR"
# prepare keywords to match
KEYWORDS_MATCH="$(echo "$KEYWORDS" | sed -e "s|^ *||" -e "s| *$||" -e "s# \+#\\\\|#g" )"
# prepare desktop files for xgettext
unset DO
unset DX
while [ $# -gt 0 ]; do
D="$1"
shift
DO="$DO|$D.orig"
DX="$DX|$D.tde_l10n"
if [ ! -f "$D.orig" ]; then
mv "$D" "$D.orig"
grep -v "^[a-zA-Z][^=]*\[[a-z][^=]*\]" "$D.orig" > "$D"
fi
# xgettext for desktop files generates a reference to the source file
# one line below the actual position and does not have the option
# to use the variable name as a comment in the POT file.
# That's why we have our own mechanism for extracting strings.
sed \
-e "s|\"|\\\\\"|g" \
< $D | \
sed \
-e "s#^\($KEYWORDS_MATCH\)[ ]*=[ ]*\(.*\)#/*\1*/i18n(\"\2\");#;t" \
-e "s|.*||" \
> $D.tde_l10n
done
# extract strings
#xgettext --foreign-user -L Desktop -k -k"Name" -k"GenericName" -k"Comment" -k"Keywords" -k"Description" -k"ExtraNames" -k"X-TDE-Submenu" -o - "$D" | \
echo -n "${DX#|}" | tr "|" "\0" | xargs -r0 \
xgettext --foreign-user -Cc -ki18n -o - | \
sed "s|\.tde_l10n||g" | \
sed "s|Content-Type: text/plain; charset=CHARSET|Content-Type: text/plain; charset=UTF-8|" \
> "$TRANSLATIONS_DIR/$POT"
POT_HEADER=$(sed -n "1,/^$/p" "$TRANSLATIONS_DIR/$POT")
# remove temporary files
echo -n "${DX#|}" | tr "|" "\0" | xargs -r0 \
rm
# process languages
echo -n "${DO#|}" | tr "|" "\0" | xargs -r0 cat |
sed -n "s|.*\[\([^]]*\)\][ ]*=.*|\1|p" | \
grep -vx "xx" | \
sort -u | \
while read L; do
echo "$POT_HEADER" | sed "s|\(Language: \)|\1$L|" > "$TRANSLATIONS_DIR/$L.po"
echo >> "$TRANSLATIONS_DIR/$L.po"
# process sections
echo "${DO#|}" | tr "|" "\n" |
while read D; do
sed -n "s|^\[\(.*\)\]$|\1|p" "$D" | \
while read S; do
# process variables
sed -n "/^\[$S\]/,/^\[/s|^\([^#\[][^\[]*\)\[$L\][ ]*=.*|\1|p" "$D" | \
while read V; do
MSGID=`sed -n -e "s|\"|\\\\\"|g" -e "/^\[$S\]/,/^\[/s|^$V[ ]*=[ ]*\(.*\)$|msgid \"\1\"|p" "$D"`
if [ -n "$MSGID" ] && [ -z "$(grep -Fx "$MSGID" "$TRANSLATIONS_DIR/$L.po")" ]; then
echo "$MSGID"
sed -n -e "s|\"|\\\\\"|g" -e "/^\[$S\]/,/^\[/s|^$V\[$L\][ ]*=[ ]*\(.*\)$|msgstr \"\1\"|p" "$D" | head -n1
echo
fi
done >> "$TRANSLATIONS_DIR/$L.po"
done
done
# update according to template
echo -n "-- $L "
msgmerge --update --backup=none "$TRANSLATIONS_DIR/$L.po" "$TRANSLATIONS_DIR/$POT"
done
# The LINGUAS file will be generated during build.
#ls "$TRANSLATIONS_DIR" | LANG=C sort | sed -n "s|\.po$||p" > "$TRANSLATIONS_DIR/LINGUAS"
Loading…
Cancel
Save