469,964 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Python script to automate use of Google Translate? (or othertranslator)

I have the need to occasionally translate a single word
programatically. Would anyone have a Python script that would let me
do this using Google (or another) translation service?

Thanks,
Ken
Jun 27 '08 #1
3 4406
Lie
On Apr 21, 8:58 am, Kenneth McDonald
<kenneth.m.mcdon...@sbcglobal.netwrote:
I have the need to occasionally translate a single word
programatically. Would anyone have a Python script that would let me
do this using Google (or another) translation service?

Thanks,
Ken
Are you sure you want to use Google translation service (or other
online translation services) cause you won't be able to translate if
you don't have internet. On the other hand, if you're only looking
translator for yourself, you could search for some firefox plugins or
Google Toolbar.
Jun 27 '08 #2
I have the need to occasionally translate a single word
programatically. Would anyone have a Python script that
would let me do this using Google (or another) translation
service?
As a matter of fact, yes, I do! This happens to be my most favourite pieceof Python code I've ever written, too...

In [1]: from translate import *

In [2]: translate('French', 'The quick brown fox jumped over the lazy dog.')
Le renard brun rapide a sauté par-dessus le chien paresseux.

In [3]: translate('German', 'The quick brown fox jumped over the lazy dog.')
Der schnelle braune Fuchs sprang über den faulen Hund.

In [4]: translate('Spanish', 'The quick brown fox jumped over the lazy dog.')
El zorro marrón rápido saltó sobre el perro perezoso.

And translate.py:

import sys
from urllib import urlopen, urlencode
from BeautifulSoup import BeautifulSoup

url = 'http://babelfish.altavista.com/tr'
languages = {
'French' : 'en_fr',
'German' : 'en_de',
'Italian' : 'en_it',
'Spanish' : 'en_es',
'Russian' : 'en_ru',
'Portuguese': 'en_pt',
'Dutch' : 'en_nl',
'Japanese' : 'en_ja',
}

def translate(lang, text):
kwds = { 'trtext' : text, 'lp' : languages[lang]}
soup = BeautifulSoup(urlopen(url, urlencode(kwds)))
print soup.find('div', style='padding:10px;').string

if __name__ == '__main__':
translate(sys.argv[1], sys.argv[2])

Enjoy!
Regards,
Trent.
Jun 27 '08 #3
Trent Nelson wrote:
>>I have the need to occasionally translate a single word
programatically. Would anyone have a Python script that
would let me do this using Google (or another) translation
service?

As a matter of fact, yes, I do! This happens to be my most favourite piece of Python code I've ever written, too...
thanks Trent,
that's really beautiful !!

cheers,
Stef

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Fred | last post: by
8 posts views Thread by Harlin Seritt | last post: by
3 posts views Thread by Mivabe | last post: by
1 post views Thread by michael.buonomo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.