#!/bin/bash
##########################################################
# mymemory.sh #
# use http://mymemory.translated.net #
# machine translation engine #
# from the command line #
# by tony baldwin #
# http://www.tonybaldwin.me #
# script at http://tonyb.us/mymem #
# released according to the GPL v. 3 #
# see API at http://mymemory.translated.net/doc/spec.php #
##########################################################
# getting variables
read -p "Language pair? (source|target, e.g. pt|en): " pair
read -p "Enter phrase to be translated: " ph
phrase=`echo $ph | sed 's/\ /%20/g'`
#sending variable to mymemory, writing to file
# what we get is a tmx file. We could write that to a file and keep it
# but we don't, here.
curl -s "http://mymemory.translated.net/api/get?q=$phrase&langpair=$pair&of=tmx" > mymemout.txt
# with some sed fu, strip that tmx to plain text for display in terminal
sed -i '
s/></>\n</g
s/<seg>\(.*\)<\/seg>/\1/g
s/<.*>//g
:a
/^$/d
t a
' mymemout.txt
sed -i '
N
s/\n/\t/' mymemout.txt
sed -i '/header/,+5d' mymemout.txt
sed -i "s/'/\'/g" mymemout.txt
sed -i "s/"/\'/g" mymemout.txt
sed -i 's/^[[:space:]]*//' mymemout.txt
sed -i '/^$/d' mymemout.txt
sed -i '0~2G' mymemout.txt
# display our results nicely in terminal
echo "----------------------------------------------------"
echo -e "$pair\n"
cat mymemout.txt
echo "----------------------------------------------------
translation courtesy http://mymemory.translated.net
mymem script by tony baldwin, http://tonyb.us/mymem
----------------------------------------------------"
# remove the output file
rm mymemout.txt
exit