此函数支持多种语言。
如无额外参数,则默认是将中文翻译为英文。
#------------------------------------------------------------------------------ # depend on BeautifulSoup # translate strToTranslate from fromLanguage to toLanguage # return the translated unicode string # some frequently used language abbrv: # Chinese Simplified: zh-CN # Chinese Traditional: zh-TW # English: en # German: de # Japanese: ja # Korean: ko # French: fr # more can be found at: # http://code.google.com/intl/ru/apis/language/translate/v2/using_rest.html#language-params def translateString(strToTranslate, fromLanguage="zh-CN", toLanguage="en"): transOK = False; translatedStr = strToTranslate; transErr = ''; try : # following refer: http://python.u85.us/viewnews-335.html postDict = {'hl':'zh-CN', 'ie':'UTF-8', 'text':strToTranslate, 'langpair':"%s|%s"%(fromLanguage, toLanguage)}; googleTranslateUrl = 'http://translate.google.cn/translate_t'; resp = getUrlRespHtml(googleTranslateUrl, postDict); #logging.debug("---------------google translate resp html:\n%s", resp); except urllib2.URLError,reason : transOK = False; transErr = reason; except urllib2.HTTPError,code : transOK = False; transErr = code; else : soup = BeautifulSoup(resp); resultBoxSpan = soup.find(id='result_box'); if resultBoxSpan and resultBoxSpan.span and resultBoxSpan.span.string : transOK = True; #translatedStr = resultBoxSpan.span.string.encode('utf-8'); googleRetTransStr = resultBoxSpan.span.string; translatedStr = unicode(googleRetTransStr); # just record some special one: # from: #【转载】[SEP4020 u-boot] start.s 注释 # to: # The 【reserved] [the SEP4020 u-boot] start.s comment else : transOK = False; transErr = "can not extract translated string from returned result"; transErr = str(transErr); if transOK : return (transOK, translatedStr); else : return (transOK, transErr);