You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
scripts/conversions/tde-l10n/tde-l10n_split_desktop

163 lines
4.2 KiB

#!/bin/sh
# default keywords
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="${D}|$1"
;;
esac
shift
done
oldIFS=$IFS
IFS="|"
set -- ${D#|}
IFS=$oldIFS
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
(
printf "[<default>]\n"
cat "$D"
) > "$D.orig"
grep -v "^[a-zA-Z][^=]*\[[a-z][^=]*\]" "$D.orig" | \
grep -Fxv "[<default>]" > "$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" | \
printf "%s" "${DX#|}" | tr "|" "\0" | xargs -r0 \
xgettext --foreign-user --from-code=UTF-8 -Cc -ki18n -o - | \
sed "s|\.tde_l10n||g" | \
sed "s|Content-Type: text/plain; charset=CHARSET|Content-Type: text/plain; charset=UTF-8|" | \
msguniq -o - \
> "$TRANSLATIONS_DIR/$POT"
POT_HEADER=$(sed -n "1,/^$/p" "$TRANSLATIONS_DIR/$POT")
# remove temporary files
printf "%s" "${DX#|}" | tr "|" "\0" | xargs -r0 \
rm
# process languages
printf "%s" "${DO#|}" | tr "|" "\0" | xargs -r0 cat |
sed -n "s|.*\[\([^]]*\)\][ ]*=.*|\1|p" | \
grep -vx "xx" | \
sort -u | \
while read L; do
printf "%s\n" "$POT_HEADER" | sed "s|\(Language: \)|\1$L|" > "$TRANSLATIONS_DIR/$L.po"
# process sections
printf "%s\n" "${DO#|}" | tr "|" "\n" |
while read D; do
sed -n "s|^\[\(.*\)\]$|\1|p" "$D" | \
sed -e 's|/|\\\\/|g' | \
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
printf "%s\n" "$MSGID"
sed -n -e "s|\"|\\\\\"|g" -e "/^\[$S\]/,/^\[/s|^$V\[$L\][ ]*=[ ]*\(.*\)$|msgstr \"\1\"|p" "$D" | head -n1
printf "\n"
fi
done >> "$TRANSLATIONS_DIR/$L.po"
done
done
# update according to template
printf "%s" "-- $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"