1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#!/bin/bash
#
# Set up tmp file
#
POTMP=${TMPDIR:-${TMP:-/tmp}}
if [ ! -d $POTMP -o ! -w $POTMP ]
then
echo $POTMP does not exist or is not writable
exit 1
fi
TMPFILE=`mktemp $POTMP/poedit.XXXXXX` || exit 1
TMPFILE2=`mktemp $POTMP/poedit.XXXXXX` || exit 1
function usage()
{
echo "Usage: poedit [ -a ] [ -n ] <file.po>" >&2
exit 1
}
#
# Check what msgs to include
#
INCLUDE_ALL_MSGS=no
IGNORE_ENCODING=no
TEMP=`getopt -o an -n 'poedit' -- "$@"`
if [ $? != 0 ] ; then usage ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a)
INCLUDE_ALL_MSGS=yes
shift
;;
-n)
IGNORE_ENCODING=yes
shift
;;
--)
shift
break
;;
*)
echo "Internal error!" >&2
exit 1
;;
esac
done
POFILE="$1"
#
# Some sanity checks
#
[ -z "$POFILE" ] && usage
if ! [ -w "$POFILE" ] ;then
echo "$POFILE: not writable" >&2
exit 1
fi
EDITCMD=sensible-editor
#
# Get file encoding
#
if [ $IGNORE_ENCODING = no ] ; then
input_encoding=$(potool -ft -fnth "$POFILE" | egrep -i '^"Content-Type:' | perl -p -e 's,^.*charset=(.*?)( *;.*)?\\n"$,$1,')
if [ -z "$input_encoding" ] ; then
echo "Failed to retrieve encoding from the content-type header." 1>&2
echo "Make sure the file has a proper header before running $0" 1>&2
exit 1
fi
locale_encoding=$(locale charmap)
if [ -z "$locale_encoding" ] ; then
echo "Failed to retrieve the locale charmap. Something is very wrong." 1>&2
exit 1
fi
fi
#
# Filter the file
#
if [ "$INCLUDE_ALL_MSGS" = "yes" ]
then
potool "$POFILE" -ft > "$TMPFILE"
echo >> "$TMPFILE"
potool "$POFILE" -fnt >> "$TMPFILE"
else
potool "$POFILE" -fnth > "$TMPFILE"
fi
if [ $IGNORE_ENCODING = no ] ; then
#
# Recode the file so it is in the locale's encoding
#
iconv -f "$input_encoding" -t "$locale_encoding" < "$TMPFILE" > "$TMPFILE2"
if [ $? -ne 0 ]; then
echo "Recoding from [$input_encoding] to [$locale_encoding] failed." 1>&2
echo "Temp. file: " $TMPFILE 1>&2
exit 1
fi
#
# Fix the charset information
#
change-po-charset "$locale_encoding" "$TMPFILE2" > "$TMPFILE"
if [ $? -ne 0 ]; then
echo "Failed to substitute the encoding attribute in the header." 1>&2
echo "Make sure the file has a proper header before running $0" 1>&2
exit 1
fi
fi
#
# Run editor and update the file on success
#
$EDITCMD "$TMPFILE"
if [ $IGNORE_ENCODING = no ] ; then
#
# Change the charset information back
#
change-po-charset "$input_encoding" "$TMPFILE" > "$TMPFILE2"
if [ $? -ne 0 ]; then
echo "Failed to substitute the encoding attribute in the header." 1>&2
echo "Temp. file: " $TMPFILE 1>&2
exit 1
fi
#
# Recode the file back to the original encoding
#
iconv -f "$locale_encoding" -t "$input_encoding" < "$TMPFILE2" > "$TMPFILE"
if [ $? -ne 0 ]; then
echo "Recoding back from [$locale_encoding] to [$input_encoding] failed." 1>&2
echo "Temp. file: " $TMPFILE 1>&2
exit 1
fi
fi
if [ $? -eq 0 ]; then
mv "$POFILE" "$POFILE~"
potool "$POFILE~" "$TMPFILE" > "$POFILE"
if [ $? -eq 0 ]; then
printf "Before: %s/%s\n" `potool -ft -s "$POFILE~"` `potool -s "$POFILE~"`
printf "After: %s/%s\n" `potool -ft -s "$POFILE"` `potool -s "$POFILE"`
rm -f "$POFILE~" "$TMPFILE"
else
mv -f "$POFILE~" "$POFILE"
echo "Temp. file: " $TMPFILE
fi
else
echo $EDITCMD exited abnormally, not updating the po file
exit 1
fi
|