Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Google translate from command line

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Beans
    29
    Distro
    Kubuntu Development Release

    Re: Google translate from command line

    It's not a bug in the program.
    Your shell is intrepting the ' to be a quote.
    To solve the problem, escape the quote e.g. \'

  2. #2
    Join Date
    Feb 2009
    Beans
    33

    Re: Google translate from command line

    Quote Originally Posted by unutbu View Post
    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
    unutbu u rawk!!! such cool code...

    I tried translating a web page thru command line?? i.e I want to pass an url as an command line argument and get a translated url as output.
    for example I would like to convert German web page "http://www.sueddeutsche.de/" into English, using "http://translate.google.com/?hl=en#" is it possible??

    Thanks!!

  3. #3
    Join Date
    Sep 2011
    Beans
    1

    Re: Google translate from command line

    Quote Originally Posted by unutbu View Post
    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 
    Hi, I'm getting this error with this script now:

    Code:
    null, "responseDetails": "Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors", "responseStatus": 403
    Probably now we need to pass our google account's cookie somehow?..

  4. #4
    Join Date
    May 2010
    Beans
    1

    Re: Google translate from command line

    Quote Originally Posted by amylase View Post
    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)


    Such a very amazing link!
    Thanks you for the post.

Page 2 of 2 FirstFirst 12

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
  •