Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Google translate from command line

  1. #1
    Join Date
    Jan 2008
    Beans
    19
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Google translate from command line

    Hi,

    I found the Google translate is pretty cool. I would like to use it to translate some text from my computer in batch(in script).

    Do you know any way that I could use it easily from shell?


  2. #2
    Join Date
    Feb 2009
    Beans
    66
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Cool Re: Google translate from command line

    Hi mag_dex

    Interesting idea. I never tried it myself but if I were to, my starting point might be using a commandline driven textbased web browser such as Lynx maybe (?)

    http://lynx.isc.org/
    http://en.wikipedia.org/wiki/Lynx_(web_browser)

  3. #3
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Google translate from command line

    Save this in ~/bin/translate

    PHP Code:
    #!/usr/bin/env python
    from urllib2 import urlopen
    from urllib import urlencode
    import sys

    # The google translate API can be found here: 
    # http://code.google.com/apis/ajaxlanguage/documentation/#Examples

    lang1=sys.argv[1]
    lang2=sys.argv[2]
    langpair='%s|%s'%(lang1,lang2)
    text=' '.join(sys.argv[3:])
    base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
    params=urlencode( (('v',1.0),
                       (
    'q',text),
                       (
    'langpair',langpair),) )
    url=base_url+params
    content
    =urlopen(url).read()
    start_idx=content.find('"translatedText":"')+18
    translation
    =content[start_idx:]
    end_idx=translation.find('"}, "')
    translation=translation[:end_idx]
    print 
    translation 
    Make it executable:
    Code:
    chmod +x ~/bin/translate
    Use it like this:
    Code:
    translate en es where are you
    en (English) is the source language
    es (Spanish) is the target language
    "where are you" is the text to be translated

    Output:
    Code:
    dónde estás
    Last edited by unutbu; March 10th, 2009 at 11:50 AM.

  4. #4
    Join Date
    Jan 2008
    Beans
    19
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Google translate from command line

    Thanks! The code is great and works!

  5. #5
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Google translate from command line

    I'm glad you like it

  6. #6
    Join Date
    Nov 2007
    Beans
    105
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Google translate from command line

    excellent! works perfect
    i'm not a python guru, but know php. in php, to parse that data, you generally use json_decode.
    i googled a bit, and found this:
    http://docs.python.org/library/json.html
    but wouldn' that be better if this is used?
    just my two cents though

    Thanks again for the great script!

  7. #7
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Google translate from command line

    Thank you very much for the feedback and information, SoulSmasher.
    I didn't know about JSON until now.

    Happily, it appears to be quite easy to use json. Below is an updated version of the script.

    To use the script with Ubuntu, you'll need to have the python-json package installed. (It requires about 160 KiB to install).

    PHP Code:
    #!/usr/bin/env python
    from urllib2 import urlopen
    from urllib import urlencode
    import sys
    try:
        
    import json
    except ImportError
    :
        print(
    'You need to install the python-json package')
        
    sys.exit(1)

    # The google translate API can be found here: 
    # http://code.google.com/apis/ajaxlanguage/documentation/#Examples

    lang1=sys.argv[1]
    lang2=sys.argv[2]
    langpair='%s|%s'%(lang1,lang2)
    text=' '.join(sys.argv[3:])
    base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
    params=urlencode( (('v',1.0),
                       (
    'q',text),
                       (
    'langpair',langpair),) )
    url=base_url+params
    content
    =urlopen(url).read()
    try:
        
    trans_dict=json.loads(content)
    except AttributeError:
        
    trans_dict=json.read(content)
    print(
    trans_dict['responseData']['translatedText']) 
    Last edited by unutbu; June 15th, 2009 at 05:36 AM.

  8. #8
    Join Date
    Nov 2007
    Beans
    105
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Google translate from command line

    Hi, thanks for the quick reply unutbu
    I tried your new code, as far as i can say it works flawlessly
    thank you again for the script, saves me from huge pain

  9. #9
    Join Date
    May 2008
    Beans
    32

    Re: Google translate from command line

    works like a charm
    thanks mate

  10. #10
    Join Date
    Nov 2007
    Beans
    105
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Google translate from command line

    i found a bug.
    when there's ' character inside the text, it won't translate

    i.e: try this

    translate fr en n'est-ce pas

    nothing shows and i've to close it with ctrl+c combination
    Last edited by SoulSmasher; June 17th, 2009 at 07:09 AM. Reason: typo

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •