Created
November 11, 2012 21:16
-
-
Save knxroot/4056301 to your computer and use it in GitHub Desktop.
google translate from bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
help='translate <text> [[<source language>] <target language>] | |
if target missing, use DEFAULT_TARGET_LANG | |
if source missing, use auto' | |
# adjust to taste | |
DEFAULT_TARGET_LANG=en | |
if [[ $1 = -h || $1 = --help ]] | |
then | |
echo "$help" | |
exit | |
fi | |
if [[ $3 ]]; then | |
source="$2" | |
target="$3" | |
elif [[ $2 ]]; then | |
source=auto | |
target="$2" | |
else | |
source=auto | |
target="$DEFAULT_TARGET_LANG" | |
fi | |
result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$1" http://translate.google.com) | |
encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result") | |
#iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8 | |
iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8 | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following error " unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings
unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings
unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings
demon:translate Benedikt$ sh ./translate.sh
unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings
unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings
unsupportedrsion from UTF-8
iconv: try 'iconv -l' to get the list of supported encodings"