Google Translate API (Python)

Updated vegaseat 3 Tallied Votes 2K Views Share

Just a simple exploration of the goslate module (uses the Google Translate API) to translate some common languages. However, some IDE's create character problems.

Note: Your computer has to be connected to the Internet to access Google.

Slavi commented: Awesome +4
''' google_translate101.py
experimenting with the Google Translate API

Goslate: Free Google Translate API
download goslate.py from ...
https://bitbucket.org/zhuoqiang/goslate/raw/tip/goslate.py
info ...
http://pythonhosted.org/goslate/

some language codes
English = 'en'
German = 'de'
French = 'fr'
Spanish = 'es'
Dutch = 'nl'
Swedish = 'sv'
Finnish = 'fi'

be careful which IDE you use
Spyder, IEP and IDLE are okay
PyScripter will screw up some of the foreign characters

tested with Python33  by  vegaseat  06nov2014
'''

import goslate

gs = goslate.Goslate()

# translate a given string to the language given by the code
print(gs.translate('hello world', 'de'))  # Hallo Welt
print(gs.translate('hello world', 'fr'))  # Bonjour tout le monde
print(gs.translate('hello world', 'es'))  # hola mundo
print(gs.translate('hello world', 'nl'))  # hallo wereld
print(gs.translate('hello world', 'sv'))  # hallå världen
print(gs.translate('hello world', 'fi'))  # Hei maailma

print('-'*40)

text = '''\
Good morning!
I hope you have all slept well.
'''
# translate(text, target_language, source_language=u'')
# english to german
print(gs.translate(text, 'de', 'en'))

'''
Guten Morgen!
Ich hoffe, Sie haben alle gut geschlafen.
'''

print('-'*40)
# english to dutch
print(gs.translate(text, 'nl', 'en'))

'''
Goedemorgen!
Ik hoop dat jullie allemaal goed geslapen.
'''

print('-'*40)

# more stuff ...

# romanized Chinese
gs_roman = goslate.Goslate(goslate.WRITING_ROMAN)
print(gs_roman.translate('hello world', 'zh'))  # Nǐ hǎo shìjiè

# detect the language code of a given string
print(gs.detect('hello world'))  # en
sabrina.anarestani 0 Newbie Poster

This is very confusing. This may be a dumb question, but I have no idea what is going on while I desire so as much to become a computer programmer. Is this an example of Python coding and how did you know what to type in to get the program to output what you wanted?

ZZucker 342 Practically a Master Poster

A code snippet is an example of working code that brings up an interesting aspect of Python programming. An English language string is given and then tanslated into several other languages. The translated string is printed out. You could apply the concept to one of your programs.

You need to learn the basics of Python first to start to understand what is going on.

Ekta_1 0 Newbie Poster

But after running for a couple of times it starts throwing 503 service unavailable error. How to deal with that? If the google has a paid service, it must be measures to block these kind of requests.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.