def get_translation(self, email): """returns translation""" go = Goslate() #=====[ Step 1: get translation ]===== lang = email.subject.strip().lower().capitalize() langs = go.get_languages() if lang in langs.values(): lang_code = [k for k,v in langs.items() if langs[k] == lang][0] try: translation = go.translate(email.body, lang_code) except: translation = "Translation error." else: translation = u'%s no esta soportado intente con es/en/fr' % lang #=====[ Step 2: format ]===== return """ Traduccion para: ================ %s --- %s """ % (email.subject.strip().encode('ascii', 'ignore'), translation.encode('ascii', 'ignore'))
class GoogleTranslateRepresentationGenerator(RepresentationGenerator): ''' Generate pseudoreference with Google Translate ''' # <lang> -- target language def __init__(self, lang='en'): self.lang = lang self.gs = Goslate() def generate(self, data_obj): if 'source' not in data_obj: print('No source for pseudo-reference generation') return data_obj references = [] try: for ref in self.gs.translate([' '.join(sentence) for sentence in data_obj['source']], self.lang): references.append(word_tokenize(ref)) # TODO: might it be some other error? except: print('Network error, no pseudo-reference is generated') return data_obj data_obj['pseudo-reference'] = references return data_obj
class GoogleTranslateRepresentationGenerator(RepresentationGenerator): ''' Generate pseudoreference with Google Translate ''' # <lang> -- target language def __init__(self, lang='en'): self.lang = lang self.gs = Goslate() def generate(self, data_obj): if 'source' not in data_obj: print('No source for pseudo-reference generation') return data_obj references = [] try: for ref in self.gs.translate( [' '.join(sentence) for sentence in data_obj['source']], self.lang): references.append(word_tokenize(ref)) # TODO: might it be some other error? except: print('Network error, no pseudo-reference is generated') return data_obj data_obj['pseudo-reference'] = references return data_obj
def get_translation(self, email): """returns translation""" go = Goslate() #=====[ Step 1: get translation ]===== lang = email.subject.strip().lower().capitalize() langs = go.get_languages() if lang in langs.values(): lang_code = [k for k, v in langs.items() if langs[k] == lang][0] try: translation = go.translate(email.body, lang_code) except: translation = "Translation error." else: translation = u'%s no esta soportado intente con es/en/fr' % lang #=====[ Step 2: format ]===== return """ Traduccion para: ================ %s --- %s """ % (email.subject.strip().encode( 'ascii', 'ignore'), translation.encode('ascii', 'ignore'))
def translate_string(request): print(request.GET) if request.is_ajax(): source = request.GET.get('source', False) if source: go = Goslate() result = {'result': go.translate(source, 'en')} print(source, result) return HttpResponse(json.dumps(result), content_type='application/json') else: HttpResponse(json.dumps({'result': ''}), content_type='application/json')
def translation(self): """ Translate text using Google translator. """ if 'translation' in self.js: translation = self.js['translation'] else: gs = Goslate() translation = gs.translate(self.text_with_title, 'en') self.add_key(key='translation', value=translation) return translation
def get_features(self, context_obj): if 'source' not in context_obj: raise NoDataError('source', context_obj, 'GoogleTranslateFeatureExtractor') if 'pseudo-reference' in context_obj: translation = context_obj['pseudo-reference'] else: gs = Goslate() translation = word_tokenize(gs.translate(' '.join(context_obj['source']), self.lang)) if context_obj['token'] in translation: return [1] return [0]
def get_features(self, context_obj): if 'source' not in context_obj: raise NoDataError('source', context_obj, 'GoogleTranslateFeatureExtractor') if 'pseudo-reference' in context_obj: translation = context_obj['pseudo-reference'] else: gs = Goslate() translation = word_tokenize( gs.translate(' '.join(context_obj['source']), self.lang)) if context_obj['token'] in translation: return [1] return [0]
class TranslatePlugin(PluginObject): # TODO: Rewrite this with some other translation API # Google will eventually realise that we're scraping, so goslate isn't # suitable. Additionally, it doesn't work with twisted for async, instead # requiring the optional `futures` module for that goslate = None def setup(self): self.goslate = Goslate() self.commands.register_command( "translate", self.translate_command, self, "translate.translate", ["tr", "t"], True ) @run_async_threadpool def translate_command(self, protocol, caller, source, command, raw_args, parsed_args): if len(parsed_args) < 2: caller.respond( "Usage: {CHARS}" + command + " <languages> <text>" ) return langs = parsed_args[0] text = u" ".join([to_unicode(x) for x in parsed_args[1:]]) if u":" in langs: split = langs.split(u":") from_lang, to_lang = split[0], split[1] else: from_lang, to_lang = u"", langs try: translation = self.goslate.translate(text, to_lang, from_lang) source.respond(u"[{}] {}".format(to_lang, translation)) except Error as e: source.respond(u"Translation error: {}".format(e)) except Exception as e: self.logger.exception("Translation error") source.respond(u"Translation error: {}".format(e))
class TranslatePlugin(PluginObject): commands = None goslate = None def setup(self): self.commands = CommandManager() self.goslate = Goslate() self.commands.register_command( "translate", self.translate_command, self, "translate.translate", ["tr", "t"], True ) @run_async_threadpool def translate_command(self, protocol, caller, source, command, raw_args, parsed_args): if len(parsed_args) < 2: caller.respond( "Usage: {CHARS}" + command + " <languages> <text>" ) return langs = parsed_args[0] text = u" ".join([to_unicode(x) for x in parsed_args[1:]]) if u":" in langs: split = langs.split(u":") from_lang, to_lang = split[0], split[1] else: from_lang, to_lang = u"", langs try: translation = self.goslate.translate(text, to_lang, from_lang) source.respond(u"[{}] {}".format(to_lang, translation)) except Error as e: source.respond(u"Translation error: {}".format(e)) except Exception as e: self.logger.exception("Translation error") source.respond(u"Translation error: {}".format(e))
class AsciiTranslator(object): def __init__(self): self.t = Goslate() def translate(self, s, lang_to, lang_from=None): lineBreaks = s.count(' ') s = s.replace(' ', ' ') try: s = self.t.translate(s, lang_to, lang_from) except: # usually a socket timeout return self.addNewLines(s, lineBreaks) # utf-8 encoded string s = self.replaceUTFchars(s) s = self.addNewLines(s, lineBreaks) # now ascii encoded return s def addNewLines(self, s, lineBreaks): if (lineBreaks == 0): return s else: words = s.split(' ') numWords = len(words) wordsPerLine = int(math.ceil(1.0 * numWords / (lineBreaks + 1))) s = "" for i in xrange(numWords): s += words[i] if ((i + 1) % wordsPerLine == 0): s += '\n' else: s += ' ' return s.strip('\n') def replaceUTFchars(self, s): ascii = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') # replaces UTF characters with closed ascii representation # e.g. characters with accents -> no accents return ascii
class AsciiTranslator(object): def __init__(self): self.t = Goslate() def translate(self, s, lang_to, lang_from=None): lineBreaks = s.count(' ') s = s.replace(' ', ' ') try: s = self.t.translate(s, lang_to, lang_from) except: # usually a socket timeout return self.addNewLines(s, lineBreaks) # utf-8 encoded string s = self.replaceUTFchars(s) s = self.addNewLines(s, lineBreaks) # now ascii encoded return s def addNewLines(self, s, lineBreaks): if (lineBreaks == 0): return s else: words = s.split(' ') numWords = len(words) wordsPerLine = int(math.ceil(1.0 * numWords / (lineBreaks + 1))) s = "" for i in xrange(numWords): s += words[i] if ((i+1) % wordsPerLine == 0): s += '\n' else: s += ' ' return s.strip('\n') def replaceUTFchars(self, s): ascii = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') # replaces UTF characters with closed ascii representation # e.g. characters with accents -> no accents return ascii
class TranslatePlugin(PluginObject): commands = None goslate = None def setup(self): self.commands = CommandManager() self.goslate = Goslate() self.commands.register_command("translate", self.translate_command, self, "translate.translate", ["tr", "t"], True) @run_async_threadpool def translate_command(self, protocol, caller, source, command, raw_args, parsed_args): if len(parsed_args) < 2: caller.respond("Usage: {CHARS}" + command + " <languages> <text>") return langs = parsed_args[0] text = u" ".join([to_unicode(x) for x in parsed_args[1:]]) if u":" in langs: split = langs.split(u":") from_lang, to_lang = split[0], split[1] else: from_lang, to_lang = u"", langs try: translation = self.goslate.translate(text, to_lang, from_lang) source.respond(u"[{}] {}".format(to_lang, translation)) except Error as e: source.respond(u"Translation error: {}".format(e)) except Exception as e: self.logger.exception("Translation error") source.respond(u"Translation error: {}".format(e))
class AppLocalizer(object): def __init__(self, application=None): self.application = application self.genroroot = getGenroRoot() try: from goslate import Goslate self.translator = Goslate() self.languages = self.translator.get_languages() except: self.translator = False self.languages = dict(en='English',it='Italian') roots = [os.path.join(self.genroroot,n) for n in ('gnrpy/gnr','gnrjs','resources/common','resources/mobile')] self.slots = [dict(roots=roots,destFolder=self.genroroot,code='core',protected=True,language='en')] for p in self.application.packages.values(): self.slots.append(dict(roots=[p.packageFolder],destFolder=p.packageFolder, code=p.id, protected = (p.project == 'gnrcore'),language=p.language)) #if os.path.exists(self.application.customFolder): # self.slots.append(dict(roots=[self.application.customFolder],destFolder=self.application.customFolder, # code='customization',protected=False)) self.buildLocalizationDict() def buildLocalizationDict(self): self.updatableLocBags = dict(all=[],unprotected=[]) self.localizationDict = dict() for s in self.slots: if not s['protected']: self.updatableLocBags['unprotected'].append(s['destFolder']) self.updatableLocBags['all'].append(s['destFolder']) locbag = self.getLocalizationBag(s['destFolder']) if locbag: self.updateLocalizationDict(locbag,s['language']) def translate(self,txt,language=None): return self.getTranslation(txt,language=language)['translation'] def getTranslation(self,txt,language=None): language = (language or self.application.locale).split('-')[0].lower() result = dict(status='OK',translation=None) if isinstance(txt,GnrLocString): lockey = txt.lockey translation_dict = self.localizationDict.get(lockey) if translation_dict: translation = translation_dict.get(language) if not translation: result['status'] = 'NOLANG' translation = translation_dict.get('en') or translation_dict.get('base') if txt.args or txt.kwargs: translation = translation.__mod__(*txt.args,**txt.kwargs) else: result['status'] = 'NOKEY' translation = txt result['translation'] = translation return result else: def translatecb(m): m = m.groupdict() lockey = m.get('key') or m.get('key_emb') loctext = m.get('value') or m.get('value_emb') loclang = m.get('lang') or m.get('lang_emb') or 'en' if not lockey: lockey = flatten(loctext) lockey = '%s_%s' %(loclang,lockey) translation_dict = self.localizationDict.get(lockey) if translation_dict: translation = translation_dict.get(language) if not translation: result['status'] = 'NOLANG' translation = translation_dict.get('en') or translation_dict.get('base') return translation else: result['status'] = 'NOKEY' return loctext result['translation'] = TRANSLATION.sub(translatecb,txt) if txt else '' return result def autoTranslate(self,languages): languages = languages.split(',') def cb(m): safekey = '[%i]' %len(safedict) safedict[safekey] = m.group(1) return safekey for lockey,locdict in self.localizationDict.items(): safedict = dict() base_to_translate = SAFEAUTOTRANSLATE.sub(cb,locdict['base']) baselang = lockey.split('_',1)[0] for lang in languages: if lang==baselang: locdict[lang] = base_to_translate continue if not locdict.get(lang): translated = self.translator.translate(base_to_translate,lang) for k,v in safedict.items(): translated = translated.replace(k,v) locdict[lang] = translated def getLocalizationBag(self,locfolder): destpath = os.path.join(locfolder,'localization.xml') if os.path.exists(destpath): try: locbag = Bag(destpath) except Exception: locbag = Bag() else: locbag = Bag() return locbag def updateLocalizationDict(self,locbag,language=None): locdict = {} if locbag.filter(lambda n: n.attr.get('_key')): for n in locbag: loc = n.attr key = loc.pop('_key',None) if key: loc.pop('_T',None) loc['base'] = key locdict[flatten(key)] = loc else: def cb(n): if not n.value: loc = dict(n.attr) locdict_key = n.label if len(n.label)<3 or n.label[2]!='_': locdict_key = '%s_%s' %(language,n.label) locdict[locdict_key] = loc locbag.walk(cb) self.localizationDict.update(locdict) def updateLocalizationFiles(self,scan_all=True): for s in self.slots: if scan_all or s['destFolder'] != self.genroroot: locbag = Bag() for root in s['roots']: d = DirectoryResolver(root,include='*.py,*.js')() d.walk(self._updateModuleLocalization,locbag=locbag,_mode='deep',destFolder=s['destFolder'] ) locbag.toXml(os.path.join(s['destFolder'],'localization.xml'),pretty=True,typeattrs=False, typevalue=False) self.buildLocalizationDict() def _updateModuleLocalization(self,n,locbag=None,destFolder=None): if n.attr.get('file_ext') == 'directory': return moduleLocBag = Bag() def addToLocalizationBag(m): lockey = m.group('key_emb') or m.group('key') loctext = m.group('text_emb') or m.group('text') or m.group('text_func') loclang = m.group('lang_emb') or m.group('lang') or 'en' if not loctext: return lockey = lockey or flatten(loctext) lockey = '%s_%s' %(loclang,lockey) if not lockey in moduleLocBag: locdict = dict(self.localizationDict.get(lockey) or dict()) locdict['base'] = loctext moduleLocBag.setItem(lockey,None,_attributes=locdict) with open(n.attr['abs_path'],'r') as f: filecontent = f.read() LOCREGEXP.sub(addToLocalizationBag,filecontent) if moduleLocBag: modulepath = os.path.relpath(n.attr['abs_path'],destFolder) path,ext = os.path.splitext(modulepath) locbag.setItem(path.replace('/','.'),moduleLocBag,path=modulepath,ext=ext.replace('.',''))
class LineAutoBot(object): def __init__(self, group): self.group = group self.__consts = Constants() self.__settings = Settings() self.__trans = Goslate() self.obj = json.loads(self.__consts.WANCAK_RAND.read()) def katamutiara(self): count_rows = 0 with open(os.path.join(os.path.dirname(__file__), self.__consts.PATH_KATAMUTIARA,\ 'kalimat.txt'),'r') as f: read_kalimat = f.readlines() with open(os.path.join(os.path.dirname(__file__), self.__consts.PATH_KATAMUTIARA,\ 'bagian.txt'),'r') as fs: read_bagian = fs.readlines() count_rows += sum(1 for _ in read_bagian) for x in range(0, count_rows + 1): j = randint(0, 110) join_data = "<bot-filsafat>" + "\n\n" + \ read_kalimat[j] + "\n" + read_bagian[j] self.group.sendMessage(join_data) break return count_rows def translate(self, msg): detect = str(self.__trans.detect(msg)) if 'id' in detect: send_translate = "<bot-translate> %s " % (self.__trans.translate( msg[14:], 'en')) send = self.group.sendMessage(send_translate) elif 'en' in detect: send_translate = "<bot-translate> %s " % (self.__trans.translate( msg[14:], 'id')) send = self.group.sendMessage(send_translate) else: send = self.group.sendMessage("<bot-translate> Gak ngerti!") # Running under Linux OS/Windows OS/Etc. def meme(self): self.__consts.WANCAK_RAND.close() url_image = str(self.obj['img']) urlreq = urllib2.Request(url_image, headers=self.__consts.CONST_REFERER) op = urllib2.urlopen(urlreq) sv_file = open(url_image[-38:], 'wb+') sv_file.write(str(op.read())) sv_file.close() self.group.sendImage(url_image[-38:]) # Running under Linux OS Only. """ def another_meme(self): self.__consts.WANCAK_RAND.close() url_image = str(self.obj['img']) header = 'Referer: http://1cak.com/' savedir = '/tmp/' cmd = "wget %s --header='%s' -P %s" % (url_image, header,savedir) get_image = os.system(cmd) #regex_url = re.search('http://cdn14.1cak.com/posts/(.*)', url_image).group(1) position = '%s%s' % (savedir, url_image[-38:]) self.group.sendImage(position) """ # convert youtube video to mp3. def youtubemp3(self, link, profile, mesg): try: if profile in mesg: sys.exit() else: url = "%s%s" % (self.__consts.YOUTUBE_CONVERTER, link) #header = 'Referer: http://youtubeinmp3.com/api/' #urlreq = urllib2.Request(url, headers=header) urlop = urllib2.urlopen(url) obj = json.loads(urlop.read()) msg = "\ \n [+] Judul: %s \ \n[+] Link Download Mp3: %s" % (obj['title'], obj['link']) self.group.sendMessage(msg) except Exception: self.group.sendMessage( "[+] Sory bro!, URL youtube tidak valid! [+]") # download youtube video with the best quality. def youtube_download(self, url, profile, mesg): try: if profile in mesg: sys.exit() else: video = new(url) get_vid = video.getbest(preftype="mp4") #dl = get_vid.download(quiet=False, filepath=self.__settings.SITE_PATH) msg = " \ \n [+] Judul Video: %s \n \ \n[+] Uploader: %s \n \ \n[+] Durasi: %s \n \ \n[+] Link Download: %s%s.%s \n \ \n[+] File video akan dihapus 1 menit lagi. \n \ " % (video.title, video.author, video.duration, self.__settings.SITE_URL, \ video.title.replace(" ", "%20"), get_vid.extension) self.group.sendImageWithURL(video.thumb) self.group.sendMessage(msg) except Exception: self.group.sendMessage( "[+] Sory broh!, URL youtube tidak valid! [+]") def another_simsimi(self, msg, name, profile, mesg): if profile in mesg: sys.exit() else: acak = str(int(uniform(100000, 300000))) data = {'av': 5.2, 'ft': 1.0, 'lc': 'id', 'os': 'i', 'req': msg, \ 'tz': "Asia/Jakarta", 'uid': acak} url = self.__consts.SIMI_CHAT + urllib.urlencode(data) data = urllib2.urlopen(url) jawab = json.loads(data.read()) bot_jawab = "<bot> @%s: %s" % (name, jawab['sentence_resp'].replace\ ("simi", self.__settings.REPLACEMENT_CALL)) self.group.sendMessage(bot_jawab) def jadwal_bioskop(self): nresults = urllib2.urlopen(self.__consts.MOVIE_PLAYING) obj = json.loads(nresults.read()) nresults.close() total = 0 msg = '' try: dt = "\n <bot> Misi permisi, ini film di bioskop yang lagi tayang ...\n" for out in obj: print "" show = "\n [+] Judul: %s \n [~] Link: %s \n" % (out['title'], out['url']) join_data = ''.join(show.split("\t")) outmsg = "%s" % (join_data) msg += outmsg total += 1 lastmsg = "\n [+] Total film baru tayang: %d film" % (total) self.group.sendMessage(dt + msg + lastmsg) except urllib2.HTTPError as err: print "Something went wrong!", err return total, msg
class LineAutoBot(object): def __init__(self, group): self.group = group self.__consts = Constants() self.__settings = Settings() self.__trans = Goslate() self.obj = json.loads(self.__consts.WANCAK_RAND.read()) def katamutiara(self): count_rows = 0 with open(os.path.join(os.path.dirname(__file__), self.__consts.PATH_KATAMUTIARA,\ 'kalimat.txt'),'r') as f: read_kalimat = f.readlines() with open(os.path.join(os.path.dirname(__file__), self.__consts.PATH_KATAMUTIARA,\ 'bagian.txt'),'r') as fs: read_bagian = fs.readlines() count_rows+=sum(1 for _ in read_bagian) for x in range(0, count_rows+1): j = randint(0, 110) join_data = "<bot-filsafat>" + "\n\n" + \ read_kalimat[j] + "\n" + read_bagian[j] self.group.sendMessage(join_data) break return count_rows def translate(self, msg): detect = str(self.__trans.detect(msg)) if 'id' in detect: send_translate = "<bot-translate> %s " % (self.__trans.translate(msg[14:], 'en')) send = self.group.sendMessage(send_translate) elif 'en' in detect: send_translate = "<bot-translate> %s " % (self.__trans.translate(msg[14:], 'id')) send = self.group.sendMessage(send_translate) else: send = self.group.sendMessage("<bot-translate> Gak ngerti!") # Running under Linux OS/Windows OS/Etc. def meme(self): self.__consts.WANCAK_RAND.close() url_image = str(self.obj['img']) urlreq = urllib2.Request(url_image, headers=self.__consts.CONST_REFERER) op = urllib2.urlopen(urlreq) sv_file = open(url_image[-38:],'wb+') sv_file.write(str(op.read())) sv_file.close() self.group.sendImage(url_image[-38:]) # Running under Linux OS Only. """ def another_meme(self): self.__consts.WANCAK_RAND.close() url_image = str(self.obj['img']) header = 'Referer: http://1cak.com/' savedir = '/tmp/' cmd = "wget %s --header='%s' -P %s" % (url_image, header,savedir) get_image = os.system(cmd) #regex_url = re.search('http://cdn14.1cak.com/posts/(.*)', url_image).group(1) position = '%s%s' % (savedir, url_image[-38:]) self.group.sendImage(position) """ # convert youtube video to mp3. def youtubemp3(self, link, profile, mesg): try: if profile in mesg: sys.exit() else: url = "%s%s" % (self.__consts.YOUTUBE_CONVERTER, link) #header = 'Referer: http://youtubeinmp3.com/api/' #urlreq = urllib2.Request(url, headers=header) urlop = urllib2.urlopen(url) obj = json.loads(urlop.read()) msg = "\ \n [+] Judul: %s \ \n[+] Link Download Mp3: %s" % (obj['title'], obj['link']) self.group.sendMessage(msg) except Exception: self.group.sendMessage("[+] Sory bro!, URL youtube tidak valid! [+]") # download youtube video with the best quality. def youtube_download(self, url, profile, mesg): try: if profile in mesg: sys.exit() else: video = new(url) get_vid = video.getbest(preftype="mp4") #dl = get_vid.download(quiet=False, filepath=self.__settings.SITE_PATH) msg = " \ \n [+] Judul Video: %s \n \ \n[+] Uploader: %s \n \ \n[+] Durasi: %s \n \ \n[+] Link Download: %s%s.%s \n \ \n[+] File video akan dihapus 1 menit lagi. \n \ " % (video.title, video.author, video.duration, self.__settings.SITE_URL, \ video.title.replace(" ", "%20"), get_vid.extension) self.group.sendImageWithURL(video.thumb) self.group.sendMessage(msg) except Exception: self.group.sendMessage("[+] Sory broh!, URL youtube tidak valid! [+]") def another_simsimi(self, msg, name, profile, mesg): if profile in mesg: sys.exit() else: acak = str(int(uniform(100000,300000))) data = {'av': 5.2, 'ft': 1.0, 'lc': 'id', 'os': 'i', 'req': msg, \ 'tz': "Asia/Jakarta", 'uid': acak} url = self.__consts.SIMI_CHAT + urllib.urlencode(data) data = urllib2.urlopen(url) jawab = json.loads(data.read()) bot_jawab = "<bot> @%s: %s" % (name, jawab['sentence_resp'].replace\ ("simi", self.__settings.REPLACEMENT_CALL)) self.group.sendMessage(bot_jawab) def jadwal_bioskop(self): nresults = urllib2.urlopen(self.__consts.MOVIE_PLAYING) obj = json.loads(nresults.read()) nresults.close() total = 0 msg = '' try: dt = "\n <bot> Misi permisi, ini film di bioskop yang lagi tayang ...\n" for out in obj: print "" show = "\n [+] Judul: %s \n [~] Link: %s \n" % (out['title'], out['url']) join_data = ''.join(show.split("\t")) outmsg = "%s" % (join_data) msg+=outmsg total+=1 lastmsg = "\n [+] Total film baru tayang: %d film" % (total) self.group.sendMessage(dt+msg+lastmsg) except urllib2.HTTPError as err: print "Something went wrong!", err return total, msg
class AppLocalizer(object): def __init__(self, application=None): self.application = application self.genroroot = getGenroRoot() try: from goslate import Goslate self.translator = Goslate() self.languages = self.translator.get_languages() except: self.translator = False self.languages = dict(en='English', it='Italian') roots = [ os.path.join(self.genroroot, n) for n in ('gnrpy/gnr', 'gnrjs', 'resources/common', 'resources/mobile') ] self.slots = [ dict(roots=roots, destFolder=self.genroroot, code='core', protected=True, language='en') ] for p in self.application.packages.values(): self.slots.append( dict(roots=[p.packageFolder], destFolder=p.packageFolder, code=p.id, protected=(p.project == 'gnrcore'), language=p.language)) #if os.path.exists(self.application.customFolder): # self.slots.append(dict(roots=[self.application.customFolder],destFolder=self.application.customFolder, # code='customization',protected=False)) self.buildLocalizationDict() def buildLocalizationDict(self): self.updatableLocBags = dict(all=[], unprotected=[]) self.localizationDict = dict() for s in self.slots: if not s['protected']: self.updatableLocBags['unprotected'].append(s['destFolder']) self.updatableLocBags['all'].append(s['destFolder']) locbag = self.getLocalizationBag(s['destFolder']) if locbag: self.updateLocalizationDict(locbag, s['language']) def translate(self, txt, language=None): return self.getTranslation(txt, language=language)['translation'] def getTranslation(self, txt, language=None): language = (language or self.application.locale).split('-')[0].lower() result = dict(status='OK', translation=None) if isinstance(txt, GnrLocString): lockey = txt.lockey translation_dict = self.localizationDict.get(lockey) if translation_dict: translation = translation_dict.get(language) if not translation: result['status'] = 'NOLANG' translation = translation_dict.get( 'en') or translation_dict.get('base') if txt.args or txt.kwargs: translation = translation.__mod__(*txt.args, **txt.kwargs) else: result['status'] = 'NOKEY' translation = txt result['translation'] = translation return result else: def translatecb(m): m = m.groupdict() lockey = m.get('key') or m.get('key_emb') loctext = m.get('value') or m.get('value_emb') loclang = m.get('lang') or m.get('lang_emb') or 'en' if not lockey: lockey = flatten(loctext) lockey = '%s_%s' % (loclang, lockey) translation_dict = self.localizationDict.get(lockey) if translation_dict: translation = translation_dict.get(language) if not translation: result['status'] = 'NOLANG' translation = translation_dict.get( 'en') or translation_dict.get('base') return translation else: result['status'] = 'NOKEY' return loctext result['translation'] = TRANSLATION.sub(translatecb, txt) if txt else '' return result def autoTranslate(self, languages): languages = languages.split(',') def cb(m): safekey = '[%i]' % len(safedict) safedict[safekey] = m.group(1) return safekey for lockey, locdict in self.localizationDict.items(): safedict = dict() base_to_translate = SAFEAUTOTRANSLATE.sub(cb, locdict['base']) baselang = lockey.split('_', 1)[0] for lang in languages: if lang == baselang: locdict[lang] = base_to_translate continue if not locdict.get(lang): translated = self.translator.translate( base_to_translate, lang) for k, v in safedict.items(): translated = translated.replace(k, v) locdict[lang] = translated def getLocalizationBag(self, locfolder): destpath = os.path.join(locfolder, 'localization.xml') if os.path.exists(destpath): try: locbag = Bag(destpath) except Exception: locbag = Bag() else: locbag = Bag() return locbag def updateLocalizationDict(self, locbag, language=None): locdict = {} if locbag.filter(lambda n: n.attr.get('_key')): for n in locbag: loc = n.attr key = loc.pop('_key', None) if key: loc.pop('_T', None) loc['base'] = key locdict[flatten(key)] = loc else: def cb(n): if not n.value: loc = dict(n.attr) locdict_key = n.label if len(n.label) < 3 or n.label[2] != '_': locdict_key = '%s_%s' % (language, n.label) locdict[locdict_key] = loc locbag.walk(cb) self.localizationDict.update(locdict) def updateLocalizationFiles(self, scan_all=True): for s in self.slots: if scan_all or s['destFolder'] != self.genroroot: locbag = Bag() for root in s['roots']: d = DirectoryResolver(root, include='*.py,*.js')() d.walk(self._updateModuleLocalization, locbag=locbag, _mode='deep', destFolder=s['destFolder']) locbag.toXml(os.path.join(s['destFolder'], 'localization.xml'), pretty=True, typeattrs=False, typevalue=False) self.buildLocalizationDict() def _updateModuleLocalization(self, n, locbag=None, destFolder=None): if n.attr.get('file_ext') == 'directory': return moduleLocBag = Bag() def addToLocalizationBag(m): lockey = m.group('key_emb') or m.group('key') loctext = m.group('text_emb') or m.group('text') or m.group( 'text_func') loclang = m.group('lang_emb') or m.group('lang') or 'en' if not loctext: return lockey = lockey or flatten(loctext) lockey = '%s_%s' % (loclang, lockey) if not lockey in moduleLocBag: locdict = dict(self.localizationDict.get(lockey) or dict()) locdict['base'] = loctext moduleLocBag.setItem(lockey, None, _attributes=locdict) with open(n.attr['abs_path'], 'r') as f: filecontent = f.read() LOCREGEXP.sub(addToLocalizationBag, filecontent) if moduleLocBag: modulepath = os.path.relpath(n.attr['abs_path'], destFolder) path, ext = os.path.splitext(modulepath) locbag.setItem(path.replace('/', '.'), moduleLocBag, path=modulepath, ext=ext.replace('.', ''))