Skip to content

Instantly share code, notes, and snippets.

@fskale
Last active April 17, 2024 07:48
Show Gist options
  • Save fskale/12511841f3540e30ae83d15c99d198e1 to your computer and use it in GitHub Desktop.
Save fskale/12511841f3540e30ae83d15c99d198e1 to your computer and use it in GitHub Desktop.
Translate a .POT file using translate-shell (Shell script version)
#!/usr/bin/env bash
# Translate a POT file using BASH and the translate-shell utility
# homebrew users might install translate-shell by entering
# brew install translate-shell
# Linux user might try the default package manager (dnf/apt-get)
[[ ${BASH_VERSINFO[0]} -lt 4 ]] &&
echo "BASH VERSION MUST BE >= 4" &&
exit 1
POT="$1"
EXT=${POT##*.}
FNM=${POT%.*}
OUT="$PWD/${FNM}_trans.po"
[[ ! -f $1 ]] &&
echo -e "File not found: $1" &&
exit 1
! [[ $EXT =~ 'pot'$ ]] &&
echo -e "Only .pot extension supported : your file extension $EXT" &&
exit 1
[[ -f $OUT ]] &&
echo -e "File already existent !: $OUT" &&
exit 1
POTC=$(cat $POT)
TPRG=$(which trans)
TOPT="-b -s en -t de"
[[ ! -x $TPRG ]] && echo -e "translate-shell binary not found: $TPRG" && exit 1
declare -a ARRAY
readarray -t ARRAY <<< $POTC
ARR_N=${#ARRAY[@]}
for (( i = 0; $i < $ARR_N; i=$((i + 1)) )); do
NAME=${ARRAY[$i]}
if [[ $NAME =~ "msgstr \"\"" ]]; then
TRANS=${ARRAY[$((i - 1))]}
[[ $TRANS =~ ^msgid[[:space:]]\"(.*)\"$ ]]
STR=${BASH_REMATCH[1]}
if [[ $STR ]]; then
STR_T=$($TPRG $TOPT "$STR")
echo "msgstr \"$STR_T\"" >> $OUT
else
echo "msgstr \"\"" >> $OUT
fi
else
echo $NAME >> $OUT
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment