def translate_text(text="", lang="French", site="google"): """ fetch translated text """ t1 = time.time() translated = "" try: if site == "babelfish": # translate with babelfish lp = get_country_id(lang, "babelfish") if lp: # data dictionary data = { "ei": "UTF-8", "doit": "done", "fr": "bf-home", "intl": "1", "tt": "urltext", "trtext": text.replace("\n", "[_]"), "lp": lp, "btnTrTxt": "Translate" } # get html source source = get_html_source(urlBF, data) # find translated text result = "".join( re.findall( "<div id=\"result\"><div style=\"[^\"]+\">([^<]+)", source)) translated = result.replace(" [_] ", "\n").replace("[_]", "\n") if not translated: site += " and google" if not translated or (site == "google"): # translate with google tl = get_country_id(lang, "google") # data dictionary data = { "hl": "", "ie": "UTF-8", "text": text, "sl": "en", "tl": tl } # get html source source = get_html_source(urlGG, data) # find translated text result_box = "".join( re.compile("(<span id=result_box.*?)</div></div>", re.S).findall(source)) translated = "".join( re.findall("<span title.*?>(.*?)</span>", result_box)).replace("<br>", "\n") except: print_exc() if translated: # fix entity translated = htmlentitydecode(translated) print "Translate text %r into %r with %s took %s" % (text, lang, site, time_took(t1)) return translated
def writeXML( filename, comments, new_strings ): # get english language for default layout englishfile = os.path.join( os.path.dirname( os.path.dirname( filename ) ), "english", "strings.xml" ) root = parseSource( englishfile, True ) t1 = time.time() for string in root.findall( "string" ): id = string.attrib.get( "id" ) string.text = ( new_strings.get( id ) or "" ) root.write( filename, comments, ENCODING ) print "Writing file took %s [%s]" % ( time_took( t1 ), filename )
def parseSource( source, full=False ): t1 = time.time() if full: # parse xml with processing instructions and comments parser = Parser() tree = parse( source, parser ) parser._root = tree._root tree.write = parser.write else: # speed up with cElementTree ;-) and not custom parser tree = parse( source ) print "Parsing file took %s [%s]" % ( time_took( t1 ), source ) return tree
def parseSource(source, full=False): t1 = time.time() if full: # parse xml with processing instructions and comments parser = Parser() tree = parse(source, parser) parser._root = tree._root tree.write = parser.write else: # speed up with cElementTree ;-) and not custom parser tree = parse(source) print "Parsing file took %s [%s]" % (time_took(t1), source) return tree
def writeXML(filename, comments, new_strings): # get english language for default layout englishfile = os.path.join(os.path.dirname(os.path.dirname(filename)), "english", "strings.xml") root = parseSource(englishfile, True) t1 = time.time() for string in root.findall("string"): id = string.attrib.get("id") string.text = (new_strings.get(id) or "") root.write(filename, comments, ENCODING) print "Writing file took %s [%s]" % (time_took(t1), filename)
def translate_text( text="", lang="French", site="google" ): """ fetch translated text """ t1 = time.time() translated = "" try: if site == "babelfish": # translate with babelfish lp = get_country_id( lang, "babelfish" ) if lp: # data dictionary data = { "ei": "UTF-8", "doit": "done", "fr": "bf-home", "intl": "1", "tt": "urltext", "trtext": text.replace( "\n", "[_]" ), "lp": lp, "btnTrTxt": "Translate" } # get html source source = get_html_source( urlBF, data ) # find translated text result = "".join( re.findall( "<div id=\"result\"><div style=\"[^\"]+\">([^<]+)", source ) ) translated = result.replace( " [_] ", "\n" ).replace( "[_]", "\n" ) if not translated: site += " and google" if not translated or ( site == "google" ): # translate with google tl = get_country_id( lang, "google" ) # data dictionary data = { "hl": "", "ie": "UTF-8", "text": text, "sl": "en", "tl": tl } # get html source source = get_html_source( urlGG, data ) # find translated text result_box = "".join( re.compile( "(<span id=result_box.*?)</div></div>", re.S ).findall( source ) ) translated = "".join( re.findall( "<span title.*?>(.*?)</span>", result_box ) ).replace( "<br>", "\n" ) except: print_exc() if translated: # fix entity translated = htmlentitydecode( translated ) print "Translate text %r into %r with %s took %s" % ( text, lang, site, time_took( t1 ) ) return translated
START_TIME = time.time() for lang in os.listdir( r"C:\Program Files\XBMC\language" ): #if lang.lower() != "french": continue filename = r"C:\Program Files\XBMC\language\%s\strings.xml" % lang root = parseSource( filename ) #t1 = time.time() print lang #strings = root.findall( "string" ) #print len( strings ) #for string in strings: # print string.attrib.get( "id" ), ( string.text or "" ) #for elem in root.getroot():#.find( "strings" ): #if elem.tag != "string" and elem.tag is Comment: # print "Comment", elem.text # print #if not elem.text or not elem.text.strip( " \n" ): # print elem.attrib.get( "id" ) # root._root.remove( elem ) #for e in elem.getiterator( ): # print e #comments = getComments( "PRE-11.0 Git:20110617-085ec3f", "Frost", "*****@*****.**" ) #root.write( filename + ".xml", comments ) #print time_took( t1 ) print "-"*100 #break #""" print time_took( START_TIME )
START_TIME = time.time() for lang in os.listdir(r"C:\Program Files\XBMC\language"): #if lang.lower() != "french": continue filename = r"C:\Program Files\XBMC\language\%s\strings.xml" % lang root = parseSource(filename) #t1 = time.time() print lang #strings = root.findall( "string" ) #print len( strings ) #for string in strings: # print string.attrib.get( "id" ), ( string.text or "" ) #for elem in root.getroot():#.find( "strings" ): #if elem.tag != "string" and elem.tag is Comment: # print "Comment", elem.text # print #if not elem.text or not elem.text.strip( " \n" ): # print elem.attrib.get( "id" ) # root._root.remove( elem ) #for e in elem.getiterator( ): # print e #comments = getComments( "PRE-11.0 Git:20110617-085ec3f", "Frost", "*****@*****.**" ) #root.write( filename + ".xml", comments ) #print time_took( t1 ) print "-" * 100 #break #""" print time_took(START_TIME)