#!/bin/bash VERSION=1.1 APPNAME=poconvert #$(basename $0) export TEXTDOMAIN=poconvert export OUTPUT_CHARSET=UTF-8 LOGFILE=/tmp/$APPNAME/$APPNAME.log CREDIT="$APPNAME version $VERSION" TTY="" tty | grep -q '^/dev/' && TTY="y" usage() { if [ "$TTY" ] ; then echo "$CREDIT" gettext "convert po, mo files usage: poconvert SOURCE_FILE SOURCE_FILE can be a shell script, pot, po, or mo file." echo else splash "$CREDIT $(gettext "Drop a file on the icon to convert po, mo.")" fi } TIMECOUNT=8 SPLASH=$(which yaf-splash) splash() { [ "$DISPLAY" ] || return [ "$SPLASH" ] || return [ "$TIMEOUT" ] || TIMEOUT=4 OPT="" while [ $# -gt 0 ]; do case "$1" in -*) OPT="$OPT $1"; shift; OPT="$OPT $1"; shift ;; *) break;; esac done [ "$XPID" ] && kill $XPID && XPID="" $SPLASH -timeout $TIMECOUNT $OPT -text "$@" } error() { MSG="$APPNAME: ERROR $@" if [ "$TTY" ]; then echo "$MSG" else splash -bg pink "$MSG" fi } fatal() { error "$@" exit 1 } good() { MSG="$APPNAME: $@" if [ "$TTY" ]; then echo "$MSG" else splash -bg green "$MSG" fi } finish() { [ "$NEW" ] && OPERATION=$(gettext "'%s' is created.") || OPERATION=$(gettext "'%s' is updated.") if [ $STATUS -eq 0 ] && [ -s "$TARGET" ]; then good "$(printf "$OPERATION" "$TARGET") $@" exit 0 fi if [ -s "$LOGFILE" ]; then FAILED=$(gettext "Making '%s' failed.") fatal "$(printf "$FAILED" "$TARGET") $@ $(cat $LOGFILE)" fi fatal "$@" } #dependency check STATUS="y" for P in msgfmt msgmerge msgunfmt xgettext; do [ -z "$(which $P)" ] && STATUS="" && break done if [ -z "$STATUS" ]; then usage fatal $(gettext "Some dependencies lacking. You need devx or poedit.") fi #source file SRCFILE=$1 if [ -z "$SRCFILE" ]; then usage error $(gettext "No files specified.") exit 0 fi NOTFOUND=$(gettext "'%s' not found.") [ -f "$SRCFILE" ] || fatal $(printf "$NOTFOUND" "$SRCFILE") # split source file name DIR=$(dirname "$SRCFILE") [ "$DIR" = '.' ] && DIR=$(pwd) BASE=$(basename "$SRCFILE") EXT="" echo $BASE | grep -q '\.' && EXT=${BASE##*.} [ "$EXT" ] && BASE=${BASE%%.$EXT} #good "$DIR:$BASE:$EXT" mkdir -p $(dirname $LOGFILE) rm -f $LOGFILE STATUS=0 NEW="y" case "$EXT" in pot) #make po TARGET=$DIR/$BASE.po if [ -s "$TARGET" ]; then NEW="" msgmerge --update "$TARGET" "$SRCFILE" 2>&1 > $LOGFILE STATUS=$? finish fi cp -f "$SRCFILE" "$TARGET" finish ;; po) #make mo TARGET=$DIR/$BASE.mo [ -f "$TARGET" ] && NEW="" msgfmt -v -o "$TARGET" "$SRCFILE" 2>&1 > $LOGFILE STATUS=$? finish ;; mo) # make po TARGET=$DIR/$BASE.po if [ -f "$TARGET" ] ;then NEW="" TARGET=$DIR/$BASE.po rm -f "$TARGET.bak" mv "$TARGET" "$TARGET.bak" fi msgunfmt -v -o "$TARGET" "$SRCFILE" 2>&1 > $LOGFILE STATUS=$? finish ;; *) NG=$(gettext "'%s' is not gettexted shell script.") # shell script? file "$SRCFILE" | grep -qw 'text' || fatal $(printf "$NG" "$SRCFILE") WORDS=$(grep '^[^#]*TEXTDOMAIN=' "$SRCFILE" | tail -n1| cut -s -d'=' -f2 | cut -d'#' -f1) for W in $WORDS; do DOMAIN=$W; break; done # extract the first word [ "$DOMAIN" ] || fatal $(printf "$NG" "$SRCFILE") TARGET=$DIR/$DOMAIN.pot [ -f "$TARGET" ] && NEW="" ( cd "$DIR" xgettext --language=shell --from-code=utf8 --keyword=xmsg --keyword=xecho --package-name=$BASE "$SRCFILE" -o - >"$TARGET" 2>$LOGFILE ) STATUS=$? finish ;; esac #good "Done."