def lookup(query, destlanguage): # Set up URL url = "http://translate.google.com/translate_a/t?client=t&text=%s&sl=%s&tl=%s" % ( utils.urlescape(query), 'zh-CN', destlanguage) con = urllib2.Request( url, headers={ 'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11' }, origin_req_host='http://translate.google.com') # Open the connection log.info("Issuing Google query for %s to %s", query, url) req = urllib2.urlopen(con) # Build the result literal from the request literal = u"" for line in req: literal += line.decode('utf-8') # Parse the response: try: log.info("Parsing response %r from Google", literal) result = parsegoogleresponse(literal) except ValueError, e: # Give the exception a more precise error message for debugging raise ValueError( "Error while parsing translation response from Google: %s" % str(e))
def weblinkgeneration(self, expression): # Generate a list of links to online dictionaries, etc to query the expression return " ".join([ '[<a href="' + urltemplate.replace("{searchTerms}", utils.urlescape(expression)) + '" title="' + tooltip + '">' + text + '</a>]' for text, tooltip, urltemplate in self.config.weblinks ])
def lookup(query, destlanguage): # Set up URL url = "http://translate.google.com/translate_a/t?client=t&text=%s&sl=%s&tl=%s" % (utils.urlescape(query), 'zh-CN', destlanguage) con = urllib2.Request(url, headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'}, origin_req_host='http://translate.google.com') # Open the connection log.info("Issuing Google query for %s to %s", query, url) req = urllib2.urlopen(con) # Build the result literal from the request literal = u"" for line in req: literal += line.decode('utf-8') # Parse the response: try: log.info("Parsing response %r from Google", literal) result = parsegoogleresponse(literal) except ValueError, e: # Give the exception a more precise error message for debugging raise ValueError("Error while parsing translation response from Google: %s" % str(e))
def weblinkgeneration(self, expression): # Generate a list of links to online dictionaries, etc to query the expression return " ".join(['[<a href="' + urltemplate.replace("{searchTerms}", utils.urlescape(expression)) + '" title="' + tooltip + '">' + text + '</a>]' for text, tooltip, urltemplate in self.config.weblinks])