473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4680
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fred | last post by:
Hi, I'm sure there are a bunch of them, but googling just returned full-fledged CMS that require a DBMS and generate dynamic pages. I'd just like a script that would 1. look in a directory...
8
by: Harlin Seritt | last post by:
I have a remote linux server where I can only access it via ssh. I have a script that I need to have run all the time. I run like so: python script.py & It runs fine. When I log off ssh I...
3
by: Mivabe | last post by:
I'm using a Python script om my Wildfire XMPP server to connect to external transports (for instantce MSN) I configured the script as a windows service with 'srvany' and 'instrv" and everything...
1
by: michael.buonomo | last post by:
We have been using the Google recommended python script for about a year. We recently realized that the script was not crawling our sites url's, but just our folders which reside on the server. The...
5
by: johnny | last post by:
Anyone know how I can make Machine A python script execute a python script on Machine B ?
2
by: Viewer T. | last post by:
I am trying to write a script that deletes certain files based on certain criteria. What I am trying to do is to automate the process of deleting certain malware files that disguise themselves...
0
by: norseman | last post by:
Gros Bedo wrote: ============================== Yes. man ps explains try ps -AFL | grep then kill -9 found (check it more than twice) 1) If your script is known to hang use what...
1
by: Erik Anderson | last post by:
Hey I am trying to get the hang of both Python and Selenium RC at the same time and having a bit of difficulty and was wondering if anyone here could assist me validating my Python Script. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.