Desktop

#!/bin/sh
#$Id: gettext.sh,v 1.7 2010/04/22 20:37:40 khorben Exp $
#variables
. "../config.sh"
DEBUG="_debug"
INSTALL="install -m 0644"
LOCALEDIR="$PREFIX/share/locale"
MKDIR="mkdir -p"
MSGFMT="msgfmt"
MSGINIT="msginit"
MSGMERGE="msgmerge"
RM="rm -f"
POTFILES="POTFILES"
XGETTEXT="xgettext --force-po"
#functions
#usage
_usage()
{
echo "Usage: ./gettext.sh <target>" 1>&2
echo " ./gettext.sh -p <prefix> install <target>" 1>&2
echo " ./gettext.sh -p <prefix> uninstall <target>" 1>&2
return 1
}
#debug
_debug()
{
echo $@
$@
}
#gettext_mo
_gettext_mo()
{
package="$1"
lang="$2"
if [ -f "$lang.po" ]; then
$DEBUG $MSGMERGE -U "$lang.po" "$package.pot" || return 1
else
$DEBUG $MSGINIT -l "$lang" -o "$lang.po" -i "$package.pot" \
|| return 1
fi
$DEBUG $MSGFMT -c -v -o "$lang.mo" "$lang.po" || return 1
}
#gettext_pot
_gettext_pot()
{
package="$1"
$DEBUG $XGETTEXT -d "$package" -o "$package.pot" --keyword="_" \
--keyword="N_" -f "$POTFILES" || return 1
}
#main
if [ $# -eq 4 -a "$1" = "-p" ]; then
PREFIX="$2"
LOCALEDIR="$PREFIX/share/locale"
lang="${4%%.mo}"
if [ "$3" = "install" ]; then
$DEBUG $MKDIR "$LOCALEDIR/$lang/LC_MESSAGES" || exit 2
$DEBUG $INSTALL "$4" \
"$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
|| exit 2
exit 0
elif [ "$3" = "uninstall" ]; then
$DEBUG $RM "$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
|| exit 2
exit 0
else
echo "gettext.sh: $3: Unknown operation" 1>&2
fi
fi
if [ $# -ne 1 ]; then
_usage
exit $?
fi
case "$1" in
*.mo)
_gettext_mo "$PACKAGE" "${1%%.mo}" || exit 2
;;
*.pot)
_gettext_pot "${1%%.pot}" || exit 2
;;
*)
exit 2
;;
esac
exit 0