#!/usr/bin/python # Python Google Translator v1.1, by Macie # Source code: www.linuxfusion.net/macie/Scripts/gtranslate.py # # Purpose: translate various languages, whether single word or # complete sentence, without the need of a localized language # database. All data is queried from translate.google.com # Credits: Google for providing their translation site. Please # show support for thir service by using their translation # website directly. This will not only show your support but # also allow you to make improvements to their translations # by providing suggestions and corrections. # START CONFIG # 0=off / 1=on # Default language. Good if you plan to use this script to # only translate a specific pair of languages. # Otherwise set to 0. default_lang = "0" # Print translation type. This will simply print language # titles of what is being done. print_lang = "1" # Should translations be logged? Also, define what file you # want to save the translations to. # Example: # Unix systems: /var/log/translation.log # windows systems: c:\Log\translation.txt logging = "0" log_file = "/tmp/translation.log" # Query language availability in real time? This will query # translate.google.com to check "what" can be translated to "what" # rather then using the locally defined options. Enable this if # this script is not allowing you to translate a language that you # can plainly see is "now" available via google. update_langdb = "0" # Google translation page URL. Probably NOT a good idea to modify. url = "http://translate.google.com/" # END CONFIG import sys import urllib import urllib2 import re import string from time import gmtime, strftime global lang_defs, lang_allow # Default language definitions lang_defs = {'ar':'Arabic','zh-CN':'Chinese','nl':'Dutch', \ 'en':'English','fr':'French','de':'German', \ 'it':'Italian','ja':'Japanese','ko':'Korean', \ 'pt':'Portuguese','ru':'Russian','es':'Spanish', \ 'zh-TW':'Taiwan','el':'Greek'} lang_allow = ['en:ar','en:nl','en:fr','en:de','en:el', \ 'en:it','en:ja','en:ko','en:pt','en:ru', \ 'en:es','fr:en','fr:de','de:en','de:fr', \ 'el:en','it:en','ja:en','ko:en','pt:en', \ 'ru:en','es:en', 'ar:en','nl:en', \ 'zh-CN:en','en:zh-TW','en:zh-CN'] # Queries lang_defs and lang_allow to show what is available def print_avail_lang(): for lang in lang_allow: from_to = give_lang_name(lang) show_lang = " "+lang+" "+'['+from_to[0]+'->'+from_to[1]+']' print show_lang # Assigns name values to desired language pair def give_lang_name(lang_name): from_to = ["?", "?"] parse_div = string.find(lang_name, ':') lang_from = lang_name[0 : parse_div] lang_to = lang_name[parse_div+1 : len(lang_name)] for (lkey, lname) in lang_defs.items(): if lkey == lang_from: from_to[0] = lname if lkey == lang_to: from_to[1] = lname return from_to # URL download/form submit function def pull_url(url, data): data = urllib.urlencode(data) header = { 'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US)'} send_request = urllib2.Request(url, data, header) html_data = urllib2.urlopen(send_request) html_text = html_data.read() return html_text # If something failes, print usage and exit def usage(): print 'Usage:' print ' ',sys.argv[0],' ""' print 'If default_lang var is set, then you can get away with:' print ' ',sys.argv[0],'""' print 'Use double quotations for more than one word, sentences, etc.\n' print 'Language translations available: ' print_avail_lang() print 'Example:',sys.argv[0],'en:fr "How are you?"' sys.exit(0) # Get an updated language list if update_langdb == "1": print "Retreaving updated language list..." form_val = {} html_text = pull_url(url, form_val) # Hopefully google won't change their code anytime soon # otherwise the filters blow will break badly split_forms = html_text.replace('', '\n') filter_html = re.search("" ,split_forms).group() filter_html = filter_html.replace('', '\n') filter_vars = ['','selected>','