def invert_escape_string(lang): if type(lang) == str: try: return json.loads(r'"' + lang + r'"') except: sytlog.log(u"请检查:[" + str(lang) + u']的格式\n') return lang return lang
def translate_with_dict(lanlist, dictionary): for item in lanlist: obj = item['lang'] if type(obj) == str: dest = find_in_ws(obj, dictionary) if dest != None and dest != "": dest = invert_escape_string(dest) item['lang'] = dest elif pick_it(obj): sytlog.log(u"[" + obj + u"]未找到译文\n") else: translate_in_obj(obj, dictionary)
def __init__(self, parent): tkinter.Frame.__init__(self, master = parent) root = self.winfo_toplevel() title2_1 = tkinter.Label(master=self, text = u"待翻译json文件:") file2_1 = tkinter.StringVar(value = "") self._jsonfile = file2_1 input2_1 = tkinter.Entry(master=self, textvariable=file2_1, width = 80) viewtool.bind_drop(input2_1) command = lambda : viewtool.set_json_dir(root, file2_1, filetypes=[("Json File", "*.json")], title=u"待翻译json", initialdir=config.json_root()) btn2_1 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) row = 0 title2_1.grid(row = row, column = 0, sticky = tkinter.W) row += 1 input2_1.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn2_1.grid(row = row, column = 6, sticky = tkinter.EW, padx = 5) transbtn = tkinter.Button(master=self, text=u"机译", command=self.translate) row += 1 transbtn.grid(row = row, column = 2, sticky = tkinter.EW) title3_1 = tkinter.Label(master=self, text = u"原文:") # self._srcword = file3_1 input3_1 = tkinter.Text(master=self, width = 80, height = 10) command = lambda : sytlog.log(translateapi.twconvertor.convert(input3_1.get("0.0", tkinter.END))) btn3_1 = tkinter.Button(master=self, text=u"翻译", command=command, width = 10) row += 1 title3_1.grid(row = row, column = 0, sticky = tkinter.W) row += 1 input3_1.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.NSEW) btn3_1.grid(row = row, column = 6, sticky = tkinter.EW, padx = 5)
def update(self): root = self.winfo_toplevel() jsonfilename = self._jsonfile.get() if jsonfilename == "" or not os.path.isfile(jsonfilename): MsgBox.showerror(u"错误", u"请选择一个json文件") return else: excelfilename = self._excelfilenew.get() excelfilename2 = self._excelfileold.get() if excelfilename == "" or not os.path.isfile(excelfilename) or excelfilename2 == "" or not os.path.isfile(excelfilename2): MsgBox.showerror(u"错误", u"请选择好Excel文件") return sytlog.log(u"开始更新\n") with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] ref_workbook=openpyxl.load_workbook(excelfilename, read_only = True) ws = ref_workbook[ref_workbook.sheetnames[0]]# index为0为第一张表 dictionary = translateapi.excel_to_dict(ws) ref_workbook.close() ref_workbook = openpyxl.load_workbook(excelfilename2, read_only = True) ws = ref_workbook[ref_workbook.sheetnames[0]] old_dict = translateapi.excel_to_dict(ws) ref_workbook.close() update_dict = translateapi.make_update_dict(old_dict, dictionary) if len(update_dict) > 0: newlist = translateapi.update_with_dict(lanlist, update_dict) if len(newlist) > 0: jdata['data'] = newlist (_, file_name) = os.path.split(jsonfilename) # (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master = root, filetypes=[("Json File", "*.json")], title = "请选择保存json文件的路径", initialdir = config.json_root(), initialfile = file_name) if save_dir == "": sytlog.log(u'取消\n') return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".json") with open(save_full_name, "w", encoding='UTF-8') as f: json.dump(jdata, f, separators=(',',':'), ensure_ascii=False) sytlog.log(u"更新完成.\n") else: sytlog.log(u"找不到更新的译文\n" + str(update_dict.keys()) + '\n') else: sytlog.log(u"无更新的译文\n")
def skip_language(self, jsonfilename, input_ui): root = self.winfo_toplevel() dbnamestring = input_ui.get("0.0", tk.END).strip() if jsonfilename == "" or not os.path.isfile(jsonfilename): MsgBox.showerror(u"错误", u"请选择一个json文件") return elif dbnamestring == "": MsgBox.showerror(u"错误", u"请输入要忽略的表名,用','隔开") return else: dbnames = dbnamestring.split(",") with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] def passed(item): return item != None and item['table_name'] not in dbnames slimlist = [item for item in lanlist if passed(item)] jdata['data'] = slimlist (_, file_name) = os.path.split(jsonfilename) # (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename( master=root, filetypes=[("Json File", "*.json")], title="请选择保存json文件的路径", initialdir=config.json_root(), initialfile=file_name) if save_dir == "": sytlog.log(u'取消\n') return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".json") with open(save_full_name, "w", encoding='UTF-8') as f: json.dump(jdata, f, separators=(',', ':'), ensure_ascii=False) sytlog.log(u"成功.\n") # if __name__ == "__main__": # DiffLan(TkinterDnD.Tk(), u"抽取未翻译的原文")
def translate(self): root = self.winfo_toplevel() jsonfilename = self._jsonfile.get() if jsonfilename == "" or not os.path.isfile(jsonfilename): MsgBox.showerror(u"错误", u"请选择一个json文件") return else: with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] translateapi.translate2tw(lanlist) (_, file_name) = os.path.split(jsonfilename) # (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master = root, filetypes=[("Json File", "*.json")], title = u"请选择保存json文件的路径", initialdir = config.json_root(), initialfile = file_name) if save_dir == "": sytlog.log(u'取消\n') return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".json") with open(save_full_name, "w", encoding='UTF-8') as f: json.dump(jdata, f, separators=(',',':'), ensure_ascii=False) sytlog.log(u"翻译完成.\n")
def make_update_dict(old_dict, new_dict): update_dict = {} sytlog.log("----------------------------------------\n") for cn, new in new_dict.items(): if cn in old_dict: old = old_dict[cn] if old != new: old = invert_escape_string(old) old = escape_string(old) update_dict[old] = new sytlog.log(cn + "\n" + old + "\n" + new + "\n\n") else: cn = invert_escape_string(cn) cn = escape_string(cn) update_dict[cn] = new sytlog.log(cn + "\n" + new + "\n\n") sytlog.log("----------------------------------------\n") return update_dict
def pick_cn(self): root = self.winfo_toplevel() jsonfilename = self._jsonfile.get() if jsonfilename == "" or not os.path.isfile(jsonfilename): sytlog.log(u"没有选择json文件\n") MsgBox.showerror(u"错误", u"请选择一个json文件") return else: sytlog.log(u"开始提取\n") with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] cnset = translateapi.create_cn_set(lanlist) if len(cnset) > 0: excelfilename = self._excelfile.get() if excelfilename != "" and os.path.isfile(excelfilename): wb1 = openpyxl.load_workbook(excelfilename, read_only = True) dict1 = translateapi.excel_to_dict(wb1[wb1.sheetnames[0]]) wb1.close() set1 = set() for k in dict1: set1.add(translateapi.invert_escape_string(k)) cnset = set(cnset) - set1 if len(cnset) == 0: sytlog.log(u"没有可提取的中文\n") return wb = translateapi.create_lan_excel_from_map(cnset) (_, file_name) = os.path.split(jsonfilename) (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master = root, filetypes=[("Excel File", "*.xlsx")], title = "请选择保存Excel文件的路径", initialdir = config.excel_root(), initialfile = short_name + u".xlsx") if save_dir == "": sytlog.log(u'取消\n') return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".xlsx") wb.save(save_full_name) wb.close() sytlog.log(u"提取完成\n") else: sytlog.log(u"没有可提取的中文\n")
def decorator(*args, **kw): try: func(*args, **kw) except Exception as e: sytlog.log(str(e)) MsgBox.showerror(title = u"错误", message=str(e))
def choose(vsn): global _cur _cur = _cfg[vsn] sytlog.log(u"选择版本[%s]\n" % _cur['name'])