Google Translate

translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; }
Usage: translate <phrase> <source-language> <output-language> Example: translate hello en es See this for a list of language codes: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
Sample Output
hola

65
2010-03-08 03:15:48

6 Alternatives + Submit Alt

  • allow multiword translations Show Sample Output


    8
    translate() { lng1="$1";lng2="$2";shift;shift; wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=${@// /+}&langpair=$lng1|$lng2" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; }
    bandie91 · 2010-05-02 22:15:30 1
  • substitute "example" with desired string; tl = target language (en, fr, de, hu, ...); you can leave sl parameter as-is (autodetection works fine) Show Sample Output


    5
    wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=translation+example&sl=auto&tl=fr" | sed 's/\[\[\[\"//' | cut -d \" -f 1
    sairon · 2011-03-06 13:46:16 0
  • translate <phrase> <source-language> <output-language> works from command line


    2
    cmd=$( wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; ); echo "$cmd"
    dtolj · 2010-03-13 01:09:00 0
  • Translates a string from English to Portuguese by using google translator web service. Show Sample Output


    1
    curl -s -A "Mozilla" "http://translate.google.com.br/translate_a/t?client=t&text=Hi+world&hl=pt-BR&sl=en&tl=pt&multires=1&ssel=0&tsel=0&sc=1" | awk -F'"' '{print $2}'
    gean01 · 2011-07-03 00:11:52 0
  • translate <some phrase> [output-language] [source-language] 1) "some phrase" should be in quotes 2) [output-language] - optional (default: English) 3) [source-language] - optional (default: auto) translate "bonjour petit lapin" hello little rabbit translate "bonjour petit lapin" en hello little rabbit translate "bonjour petit lapin" en fr hello little rabbit Show Sample Output


    1
    translate(){wget -U "Mozilla/5.0" -qO - "https://translate.google.com/translate_a/single?client=t&sl=${3:-auto}&tl=${2:-en}&dt=t&q=$1" | cut -d'"' -f2}
    klisanor · 2014-06-10 12:08:51 0

What Others Think

See https://redmine.sputnick-area.net/projects/show/polytranslator
sputnick · 428 weeks and 4 days ago
Also try: translate(){ curl -s "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hi&langpair=$(curl -s "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$1" | sed 's/.*"language":"\([^"]*\)".*/\1^C/')%7C$2" | sed 's/.*{"translatedText":"\([^"]*\)".*/\1\n/' Will fetch the source language for you.
matthewbauer · 428 weeks and 3 days ago
translate(){ curl -s "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hi&langpair=$(curl -s "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$1" | sed 's/.*"language":"\([^"]*\)".*/\1^C/')%7C$2" | sed 's/.*{"translatedText":"\([^"]*\)".*/\1\n/'; }
matthewbauer · 428 weeks and 3 days ago
Actually, if you leave the source language blank, it will guess it anyway and it's normally pretty successful. Also, you can have the command translate to English if you don't provide a language to translate to by using a default value for the second parameter, like this: translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=%7C${2:-en}" | sed 's/.*{"translatedText":"\([^"]*\)".*/\1\n/'; }
eightmillion · 428 weeks and 3 days ago
In bash it doesn't work with spaces or accentuated letters (in french for example). This one does it (accentuated, bash working, and guessing source language if not provided) : translate(){ curl -s "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=`perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$1"`&langpair=`if [ "$3" != "" ]; then echo $2; fi;`%7C`if [ "$3" == "" ]; then echo $2; else echo $3; fi;`" | sed 's/{"responseData": {"translatedText":"\([^"]*\)".*}, .*}/\1\n/'; }
fab4am · 428 weeks and 3 days ago
ok the wget one seems to work too with spaces and accents
fab4am · 428 weeks and 3 days ago
how do you know the abbreviations for languages? link please.
kjowens · 425 weeks and 5 days ago
{ "responseData": null, "responseDetails": "Suspected Terms of Service Abuse. Please see code.google.com/apis/errors", "responseStatus": 403 }
vp_arth · 238 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: