xgettext msguniq msgunfmt msgmerge msgfmt
are included.
Code: Select all
#!/bin/sh
TITLE='translate .md files'
usage () {
die "
Create / edit translation file (GNU message catalog) from md files
usage: $0 [file.md|directory]
case file.md then each file under /etc /root /usr /var will be processed
in case of directory then each file *.md in this directory will be processed
example 1: $0 README.md
example 2: $0 /usr/share/doc/faqs/
At least one file must contain %%textdomain <TEXTDOMAIN>
Translation is stored in $TEXTDOMAIN/${LANG%_*}/LC_MESSAGES/<TEXTDOMAIN>.mo
Existing translations are always being re-used (msgmerge)
"
}
die () { [ $DISPLAY ] && Xdialog --title="$TITLE" --msgbox "$1" 0 0 || echo $1; exit 1; }
[ `whoami` = root ] || die "This tool is for root only."
create_pot () {
# create ${WD}/${aTEXTDOMAIN}.pot file from .md file(s) using xgettext and mdview --po
aTEXTDOMAIN=$1
FILES=$2
local MD=MD$$
echo " " | xgettext -o ${WD}/${aTEXTDOMAIN}_.pot -L shell --package-name=$aTEXTDOMAIN --from-code=UTF-8 --force-po -
sed -i 's/charset=CHARSET/charset=UTF-8/' ${WD}/${aTEXTDOMAIN}_.pot
echo "" >> ${WD}/${aTEXTDOMAIN}_.pot
for aFILE in $FILES; do
grep -v '$(g' ${aFILE} > $MD
mdview --po "" $MD >> ${WD}/${aTEXTDOMAIN}_.pot
xgettext -L shell $aFILE -j -o ${WD}/${aTEXTDOMAIN}_.pot
done
msguniq -u --to-code=UTF-8 -o ${WD}/${aTEXTDOMAIN}.pot ${WD}/${aTEXTDOMAIN}_.pot
rm ${WD}/${aTEXTDOMAIN}_.pot
rm $MD
}
create_po () {
aTEXTDOMAIN=$1
if [ -f "${TRANSLATED}" ]; then
msgunfmt ${TRANSLATED} -o - > ${WD}/${aTEXTDOMAIN}_OLD.po
else
urxvt -e msginit --no-translator --no-wrap --locale=${transLANG} --input=${WD}/${aTEXTDOMAIN}.pot --output-file=${WD}/${aTEXTDOMAIN}_OLD.po
fi
}
translate_until_error_free () {
ATEXTDOMAIN=$1
#code from momanager by Barry Kauler
#130502 L18L/BK: stay in a loop if there is an error converting .po to .mo...
ABORTFLG=0
cp -f $WD/${ATEXTDOMAIN}.po $WD/${ATEXTDOMAIN}.poBACKUP1
while true ; do
killall geany 2> /dev/null #otherwise geany will return immediately and continue executing this script.
sleep 0.5
geany $WD/${ATEXTDOMAIN}.po
if [ ! diff -q $WD/${ATEXTDOMAIN}.poBACKUP1 $WD/${ATEXTDOMAIN}.po >/dev/null 2>&1 ] ; then
ABORTFLG=8
break
fi
msgfmt --output-file=- $WD/${ATEXTDOMAIN}.po >/dev/null 2> $WD/po-2-mo.err
[ ! -s $WD/po-2-mo.err ] && break
pupdialog --background '#FF8080' --backtitle "${0##/*}: po to mo fail" --extra-button --yes-label "Fix" --no-label "Abort" --extra-label "Revert" --yesno "There are errors in the .po file:
`cat $WD/po-2-mo.err`
NOTE: The above error message is logged to $WD/po-2-mo.err
Click the 'Fix' button to edit the .po file again...
Click 'Revert' to go back to original .po file, and try again...
Click 'Abort' to give up..." 0 0
case $? in
0) continue ;; #Fix
3) cp -f $WD/${ATEXTDOMAIN}.poBACKUP1 $WD/${ATEXTDOMAIN}.po ; continue ;; #Revert
*) ABORTFLG=9 ; break ;; #1=Abort
esac
done
if [ $ABORTFLG -eq 0 ];then
rxvt -e msgfmt --check --output-file=$WD/${ATEXTDOMAIN}.mo $WD/${ATEXTDOMAIN}.po
pupdialog --background yellow --colors --backtitle "${0##/*}: post-edit mo" --yes-label "OK" --no-label "Cancel" --yesno "The file $WD/${ATEXTDOMAIN}.po has been edited and converted to $WD/${ATEXTDOMAIN}.mo. If all went well, click the \ZbOK\ZB button to copy it to \Zb${TRANSLATED}\ZB. Note, as a precaution, the old mo file is at $WD/${ATEXTDOMAIN}.moOLD." 0 0
ABORTFLG=$?
fi
if [ $ABORTFLG -eq 0 ];then
cp -f $WD/${ATEXTDOMAIN}.mo "${TRANSLATED}"
else
pupdialog --background pink --backtitle "${0##/*}: mo edit aborted" --msgbox "You have declined to update the mo file. Ok, the previous one has been retained." 0 0
fi
}
##############################
case $1 in -*) usage ;; esac
read transLANG < $FATDOG_STATE_DIR/language #ex:de_BE.UTF-8
[ "$transLANG" ] || transLANG=$LANG
[ "$transLANG" ] || die "Sorry, need language LANG."
transLANG=${transLANG%_*} #ex:de
WD=/tmp/md; mkdir -p $WD
[ "$1" ] && FROM=`realpath "$1" 2>/dev/null`
[ $FROM ] || FROM=$(Xdialog --title "$TITLE to $transLANG" --backtitle="Choose .md file \n(or cancel to select a directory)" --no-buttons --fselect $HOME 0 0 2>&1)
[ "${FROM##*.}" = "md" ] || [ -d $FROM ] || die "no .md file selected"
[ $FROM ] || FROM=$(eval Xdialog --title \"$TITLE to $transLANG\" --backtitle=\"All .md files in directory:\" --no-buttons --dselect /usr/share/doc/faqs/ 0 0 2>&1)
[ ${FROM##*.} = md ] || [ -d $FROM ] || die "no .md file found in $FROM"
if [ -d $FROM ]; then
FILES="`ls ${FROM}/*.md`"
else
FILES="`find /etc /root /usr /var -maxdepth 3 -wholename ${FROM}`"
fi
aTEXTDOMAIN="`grep -m1 %%textdomain $FILES | cut -d' ' -f2 | uniq `"
[ "$aTEXTDOMAIN" ] || die "no %%textdomain found"
TRANSLATED=${TEXTDOMAINDIR}/${transLANG}/LC_MESSAGES/${aTEXTDOMAIN}.mo
create_pot $aTEXTDOMAIN "$FILES"
create_po $aTEXTDOMAIN
msgmerge ${WD}/${aTEXTDOMAIN}_OLD.po ${WD}/${aTEXTDOMAIN}.pot -o - > $WD/${aTEXTDOMAIN}.po
# remove comment sign, ex: change #~ msgid "bla" to msgid "bla"
sed -i 's/^#~ //' $WD/${aTEXTDOMAIN}.po #that is: keep unused translations !
sync
translate_until_error_free $aTEXTDOMAIN
exit 0
(small changes in index.md [attached])
25 messages....