473,695 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Jun 27 '08 #1
3 4699
On Apr 21, 8:58 am, Kenneth McDonald
<kenneth.m.mcdo n...@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?

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
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('Fren ch', 'The quick brown fox jumped over the lazy dog.')
Le renard brun rapide a sauté par-dessus le chien paresseux.

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

In [4]: translate('Span ish', '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.altav ista.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(u rlopen(url, urlencode(kwds) ))
print soup.find('div' , style='padding: 10px;').string

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

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

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 !!


Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 for articles in raw HTML, 2. generate a cooked output for each page (ie. add header and footer, CSS link in HEAD, etc.), 3. regenerate the homepage with "Last modified" bit following each
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 notice that the script died when I logged off. How do I make sure it stays running? thanks,
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 seems te work fine. The service starts with the system, but as soon as an local useraccount (remote desktop) logs off on the server the script vanishes. Google helped me discovering that it has something to do something with...
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 python script seems to be designed for 'non database' sites, not a site which is using .asp, and has dynamic pages. We are an ecommerce site. What are other ecommerce sites using to create an xml file? Are they using the python script?
by: johnny | last post by:
Anyone know how I can make Machine A python script execute a python script on Machine B ?
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 as system files and hidden files. When I use os.remove() after importing the os module is raises a Windows Error: Access denied probably because the files have disguised themselves as system files. Is there anway to get adequate privileges under...
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 another wrote: py s.py tokens & capture=$! (get the PID ready)
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 gotten rid of most of the initial errors besides one. Here is my Python Script from selenium import selenium import unittest class SignUpTask(unittest.TestCase): """ The following needs to have the issues corrected to make it run....
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.