def create_pseudo_translation(jar_file, project_dir, result_file=None, translate_type='empty'): """ 创建伪翻译 java -jar OmegaT.jar <project-dir> --mode=console-createpseudotranslatetmx --pseudotranslatetmx=<filename> --pseudotranslatetype=[equal|empty] :param jar_file: OmegaT 的 jar 文件 :param project_dir: 项目目路 :param result_file: 结果文件,tmx 格式 :param translate_type: 翻译类型,可选 equal | empty :return: """ if result_file is None: result_file = filex.get_result_file_name('pseudo', '', 'tmx') cmd = 'java -jar %s %s --mode=console-createpseudotranslatetmx --pseudotranslatetmx=%s ' \ '--pseudotranslatetype=%s' % (jar_file, project_dir, result_file, translate_type) print(cmd) subprocess.call(cmd, shell=True) print('已输出文件 %s' % result_file) auto_file = 'data/auto.tmx' shutil.copyfile(result_file, auto_file) print('已复制文件 %s' % auto_file) translation_file = 'data/translation.tmx' shutil.copyfile(result_file, translation_file) print('已复制文件 %s' % translation_file)
def process_keymap_reference_card_translation(en_file, cn_file, result_file=None): """ 将翻译结果转为md文件 :param en_file: :param cn_file: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(en_file, '_result', 'md') en_lines = filex.read_lines(en_file) cn_lines = filex.read_lines(cn_file) if en_lines is None or cn_lines is None: return None # 以[]中一个或多个#开头,接内容 p_title = re.compile(r'^\[(#+)\]\s?(.*)') result = [] for i in range(len(cn_lines)): line = cn_lines[i] line = encodex.unicode_str_to_chinese(line) line = re.sub(p_title, r'\1 \2', line) en_line = en_lines[i].replace('\n', '') if '【' in en_line: shortcut = en_line.split('【')[1] line = line.replace('* ', "") line = '* %-30s%s' % ('【%s】' % shortcut, line) result.append(line) filex.write_lines(result_file, result)
def check_and_append_tips_name(file_path, tips_name_file, result_file=None): """ 根据检查tips的文件是否全 该方法用于 IdeTipsAndTricks 没有指定所有的文件,但还是需要翻译文件名的,所以补全 """ if result_file is None: result_file = filex.get_result_file_name(tips_name_file, '_append') file_list = filex.list_file(file_path) print('共%d个文件' % len(file_list)) lines = filex.read_lines(tips_name_file) tips_name = [] for line in lines: if '=' in line: name = line.split('=')[0] tips_name.append(name) # 名字只加一层,exclude里面的不处理 file_name = '%s\\%s.html' % (file_path, name) if file_name in file_list: file_list.remove(file_name) else: print('文件不存在%s' % file_name) print('共有%d个tip名' % len(tips_name)) print('还缺%d个文件' % len(file_list)) # 写入结果 lines.append('\n# append\n') for file_name in file_list: name = os.path.splitext(os.path.split(file_name)[1])[0] word = Tips.camel_word_to_words(name) lines.append('%s=%s\n' % (name, word)) filex.write_lines(result_file, lines)
def get_all_translation(en_dir, cn_dir, dict_file=None, dict_diff_file=None, suffix=''): if dict_file is None: base_name = os.path.split(cn_dir)[1] dict_file = 'data/%s_dict.txt' % base_name if dict_diff_file is None: dict_diff_file = filex.get_result_file_name(dict_file, '_diff') """读取并输出所有翻译""" all_translation, diff_translation = Translator.check_same_en_difference_cn( en_dir, cn_dir, False, suffix) result = list() for key, value in all_translation.items(): result.append('%s=%s\n' % (key, value)) print('size is %d' % len(sorted(all_translation.keys()))) filex.write_lines(dict_file, result) result = list() for key, value in diff_translation.items(): result.append('%s=%s\n\n' % (key, value)) print('size is %d' % len(sorted(diff_translation.keys()))) filex.write_lines(dict_diff_file, result)
def process_keymap_reference_card(file_path, result_file=None): """ 处理快捷键参考文件 来自:https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf 保存后将其复制出来,然后用“【”划分快捷键,再进行一次处理 :param file_path: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(file_path, '_modified', 'properties') lines = filex.read_lines(file_path) if lines is None: return result = [] # 以一个或多个#开头,接内容 p_title = re.compile(r'^(#+)\s?(.*)') for line in lines: line = line.replace('\n', '') line = re.sub(p_title, r'[\1] \2', line) if '【' in line: split_result = line.split('【') line = '* %s' % split_result[0] result.append(line + '\n') filex.write_lines(result_file, result)
def add_ellipsis_and_shortcut(en_file, cn_file, result_file=None): """ 处理快捷键,将_字母替换为(_字母) :param en_file: :param cn_file: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(cn_file, '_add_ellipsis_and_shortcut') en_dict = Tools.get_dict_from_file(en_file, delete_value_ellipsis=False, delete_value_underline=False) cn_dict = Tools.get_dict_from_file(cn_file, delete_value_ellipsis=False, delete_value_underline=False) count = 0 p_ellipsise = re.compile('……|…$') p_period = re.compile('\.') for (k, v) in en_dict.items(): if v.endswith('.'): # 以.结尾 if k in cn_dict.keys(): cn_value = cn_dict[k] old_value = cn_value if v.endswith('...'): # 省略号结尾 cn_value = re.sub(p_ellipsise, '...', cn_value) if not cn_value.endswith('...'): cn_value += '...' elif v.endswith('.'): # 句号结尾 cn_value = re.sub(p_period, '。', cn_value) if not cn_value.endswith('。'): cn_value += '。' if cn_value != old_value: print('修改【%s】为【%s】' % (old_value, cn_value)) cn_dict[k] = cn_value if '_' in v: # 有快捷方式 index = v.find('_') shortcut = v[index + 1:index + 2] # 包含快捷键 if k in cn_dict.keys(): # 都有 cn_value = cn_dict[k] count += 1 # 已经是(_字母结)结尾的,重新替换一遍 p = re.compile(r'(.*)(\(_\w\))') if re.match(p, cn_value) is not None: replace_result = re.sub(p, r'\1' + '(_%s)' % shortcut, cn_value) print('替换%d,key=%s,v=%s,cn=%s,r=%s' % (count, shortcut, v, cn_value, replace_result)) else: replace_result = cn_value.replace('_', '') + '(_%s)' % shortcut print('添加%d,key=%s,v=%s,cn=%s,r=%s' % (count, shortcut, v, cn_value, replace_result)) cn_dict[k] = replace_result result = Tools.translate_file_by_dict(en_file, cn_dict, '') # 重新翻译 result.insert(0, '# from:[AndroidStudio翻译(3)-ActionsBundle中文翻译](http://blog.pingfangx.com/2355.html)\n') filex.write_lines(result_file, result)
def convert_to_omegat_dict(en_file, cn_file, output_file=None): """ 将翻译结果转为omegaT的字典 :param en_file: 英文文件 :param cn_file: 中文文件 :param output_file: 输出文件 :return: """ if output_file is None: output_file = filex.get_result_file_name(cn_file, '_omegat_result', 'xml') en_dict = Tools.get_dict_from_file(en_file) cn_dict = Tools.get_dict_from_file(cn_file) tmx = Et.Element('tmx') tmx.attrib['version'] = '1.1' Et.SubElement(tmx, 'header') body = Et.SubElement(tmx, 'body') for (k, v) in cn_dict.items(): if k in en_dict.keys(): en_value = en_dict[k] cn_value = v # 判断是否有多个句子,"."加一个空格 added = False if '. ' in en_value: en_split = en_value.split('. ') if en_split[1] != '': # 包含“.”,不是在最后的“...” # 检查中文 if '。 ' in cn_value: cn_split = cn_value.split('。 ') if len(en_split) == len(cn_split): added = True # 中英长度相等 for i in range(len(en_split)): Tools.add_translate_element( body, en_split[i], cn_split[i]) print('分开添加:' + cn_split[i]) else: print('') print(en_value) print(cn_value) print('%d,%d' % (len(en_split), len(cn_split))) else: print('') print(en_value) print(cn_value) print('英文中有“. ”,中文中不包含“。 ”') if not added: if cn_value: # 只添加不为空的 Tools.add_translate_element(body, en_value, cn_value) tree = Et.ElementTree(tmx) tree.write(output_file, encoding='utf-8') print('输出为' + output_file)
def delete_symbol(delete_type, file, file_type=0, result_file=None): delete_action = DeleteAction.action_list[delete_type] if result_file is None: result_file = filex.get_result_file_name(file, delete_action[2]) if file_type == DeleteAction.FILE_TYPE_OMEGAT: DeleteAction.delete_symbol_of_omegat(file, result_file, delete_action[3:]) else: DeleteAction.delete_symbol_of_file(file, result_file, delete_action[3:])
def update_omegat_dict_by_pseudo_dict(omegat_dict_file, pseudo_dict_file, result_file=None): "根据伪翻译记忆更新记忆库" if result_file is None: result_file = filex.get_result_file_name(pseudo_dict_file, '_update_by_pseudo') source_dict = Tools.get_dict_from_omegat(omegat_dict_file) pseudo_dict = Tools.get_dict_from_omegat(pseudo_dict_file) print('记忆库共%d条记录,伪翻译共%d条记录' % (len(source_dict), len(pseudo_dict))) result_dict = ActionsBundle.update_dict_add_extra_info(source_dict, pseudo_dict) print('处理结果共%s条记录' % len(result_dict)) Tools.save_omegat_dict(result_dict, result_file)
def process_tips_manifest_file(self): tips = Tips() project_name_list = os.listdir(self.source_dir) for project_name in project_name_list: project_path = self.source_dir + os.sep + project_name if os.path.isdir(project_path): tips_file_path = project_path + os.sep + 'IdeTipsAndTricks' + os.sep + 'IdeTipsAndTricks.xml' tips_en_file = filex.get_result_file_name(tips_file_path, '_en', 'properties') if os.path.exists(tips_file_path): print('处理 %s' % tips_file_path) tips.process_tips_manifest_file(tips_file_path, tips_en_file)
def export_omegat_dictionary_to_file(file_path, result_file=None): """导出omegat的词库为文件""" if result_file is None: result_file = filex.get_result_file_name(file_path, '', 'txt') omegat_dict = Translator.get_omegat_dict(file_path) result = list() for key, value in omegat_dict.items(): result.append('%s=%s\n' % (key, value)) print('size is %d' % len(sorted(omegat_dict.keys()))) filex.write_lines(result_file, result)
def check_and_append_tips_name(self): tips = Tips() project_name_list = os.listdir(self.source_dir) for project_name in project_name_list: project_path = self.source_dir + os.sep + project_name if os.path.isdir(project_path): tips_file_path = project_path + os.sep + 'IdeTipsAndTricks' + os.sep + 'IdeTipsAndTricks.xml' tips_en_file = filex.get_result_file_name(tips_file_path, '_en', 'properties') if os.path.exists(tips_file_path): print('处理 %s' % tips_file_path) project_tips_path = project_path + os.sep + 'resources_en' + os.sep + 'tips' tips.check_and_append_tips_name(project_tips_path, tips_en_file, tips_en_file)
def handle_keymap_file(en_file, cn_file, comment_file, result_file=None): """ 将一行一行的keymap重新处理 导出为.properties,这样OmegaT处理时会换照配置文件去处理 我们以[#]或[desc]加空格为开头,会被过滤器正确解析 :param en_file: :param cn_file: :param comment_file: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(cn_file, '_add_desc_and_comment', 'properties') lines = filex.read_lines(cn_file) if lines is None: return desc_dict = KeymapList.get_action_desc_dict(en_file) comment_dict = KeymapList.get_comment_dict(comment_file) count = 0 desc_count = 0 result = [] for line in lines: line = line.replace('\n', '') if line.startswith('#'): old_line = line # 因为有加了#,所以处理下 line = line.lstrip('# ') # 相差的长度是trip掉的,注意在替换了\n之后 prefix = old_line[0:len(old_line) - len(line)].rstrip() else: prefix = '#' * 5 append = '' count += 1 if line in desc_dict.keys(): desc = desc_dict[line] desc_count += 1 print('%d/%d,line=%s,desc=%s' % (count, desc_count, line, desc)) # 有描述,添加 append += '\n\n%s %s' % ('[desc]', desc) if line in comment_dict.keys(): comment = comment_dict[line] print('%s的描述为%s' % (line, comment)) append += '\n\n%s' % comment line = '\n\n[%s] %s%s' % (prefix, line, append) result.append(line) filex.write_lines(result_file, result)
def inspect_file(pseudo_file, translation_file, inspection_list, result_file=None, print_msg=False, print_change=False): """ :param pseudo_file: 伪翻译文件,也可以与翻译文件相同,用于自校检 :param translation_file: 翻译文件 :param inspection_list: 要检测的方法列表,如检测tips时是不需要检测快捷方式的 :param result_file: 结果文件 :param print_msg: 是否输出操作时的消息 :param print_change: 是否输出更改时的消息,如果2个都置为False,可以查看需要处理的内容 :return: """ if result_file is None: result_file = filex.get_result_file_name(translation_file, '_inspection') pseudo_dict = Tools.get_dict_from_omegat(pseudo_file) translation_dict = Tools.get_dict_from_omegat(translation_file) print(u'pseudo size is %d' % (len(pseudo_dict))) print(u'translation size is %d' % (len(translation_dict))) result = dict() i = 0 for en in pseudo_dict.keys(): cn_key = None if en in translation_dict.keys(): cn_key = en else: abbreviated_en = DeleteAction.delete_all_symbol_of_string( en, False) if abbreviated_en in translation_dict.keys(): cn_key = abbreviated_en if cn_key: value = translation_dict[cn_key] if not value: continue translation_dict.pop(cn_key) old = value for inspection in inspection_list: value = inspection(en, value, print_msg) if old != value: if print_change: i += 1 print(u'\n%d.\n【%s】根据【%s】被修改为\n【%s】\n' % (i, old, en, value)) result[en] = value else: print(u'不包含key:%s' % en) print(u'remain translation size %d' % (len(translation_dict))) Tools.save_omegat_dict(result, result_file)
def export_to_omegat(file_path, result_file=None): """导出为OmegaT的记忆文件""" if result_file is None: result_file = filex.get_result_file_name(file_path, '', '.tmx.xml') translation_dict = filex.get_dict_from_file(file_path) output_dict = dict() for key, value in translation_dict.items(): if value: if '【】' in value: output_dict[key] = value.split('【】')[0] else: output_dict[key] = value Tools.save_omegat_dict(output_dict, result_file)
def write_to_excel(result_file, excel_file=None): """ 写入excel :param result_file: :param excel_file: :return: """ if excel_file is None: excel_file = filex.get_result_file_name(result_file, '', 'xls') lines = filex.read_lines(result_file, ignore_line_separator=True) if lines is None: return excelx.write_list_to_excel(excel_file, lines, title=Floor.get_excel_title())
def process_dir_translation_result(en_dir, cn_dir, result_dir=None, name_pattern=None): """处理文件夹中的所有文件""" if result_dir is None: result_dir = cn_dir + '_add' en_file_list = filex.list_file(en_dir, name_pattern) length = len(en_file_list) for i in range(length): en_file = en_file_list[i] print('process %d/%d' % (i + 1, length)) cn_file = en_file.replace(en_dir, cn_dir) cn_file = filex.get_result_file_name(cn_file, '_zh_CN') result_file = en_file.replace(en_dir, result_dir) ActionsBundle.add_ellipsis_and_shortcut(en_file, cn_file, result_file)
def __init__(self, file: str, output: str = None, start_line=None, end_line=None): self.file = file self.output = output if self.output is None: self.output = filex.get_result_file_name(self.file, '_line') self.start_line = start_line """起始行,用于只处理部分内容""" self.end_line = end_line """结束行,一般最后的致谢等内容不需要处理,需要过滤"""
def process_tips_manifest_file(self, file_path, result_file=None): """ 处理清单文件,整理tips的名称方便翻译 :param file_path: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(file_path, '_en', 'properties') ordered_file_list = self.get_tips_order_files(file_path) result = [] for file in ordered_file_list: name = file.split('.')[0] word = self.camel_word_to_words(name) result.append('%s=%s\n' % (name, word)) filex.write_lines(result_file, result)
def process_google_translation_to_omegat_dict(en_file, cn_file, result_file=None): if result_file is None: result_file = filex.get_result_file_name(en_file, '', 'tmx') en_dict = filex.get_dict_from_file(en_file) cn_dict = filex.get_dict_from_file(cn_file) print('英文词典%d条' % (len(en_dict))) print('中文词典%d条' % (len(cn_dict))) result = dict() for cn_key, cn_value in cn_dict.items(): cn_key = cn_key.strip() cn_value=cn_value.strip() if cn_key in en_dict.keys(): en_value = en_dict[cn_key] result[en_value] = cn_value print('结果%d条' % (len(result))) Tools.save_omegat_dict(result, result_file)
def process_file_for_translation(file, result_file=None): """ 在翻译前处理文件 删除快捷方式 删除末尾的.或省略号 :param file: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(file, '_modified') print('删除省略号') DeleteAction.delete_ellipsis(file, result_file) # 后面的将接着用result_fiel print('删除快捷方式') DeleteAction.delete_underline_shortcut(result_file, result_file) print('删除&形式的快捷方式') DeleteAction.delete_and_shortcut(result_file, result_file) print('再次删除省略号,防止位于快捷方式之前') DeleteAction.delete_ellipsis(result_file, result_file)
def process_line_break_for_translation(self, file, output=None): """处理文件换行""" if output is None: output = filex.get_result_file_name(file, '_line') ans = [] lines = filex.read_lines(file, ignore_line_separator=True) if not lines: print(f'文件内容为空:{file}') exit() n = len(lines) i = 0 while i < n: line = lines[i] print(f'{i} {line}') if self.need_inline(i, line, ans): print(f'{i} 行符合合并规则,尝试连接到上一行 {ans[-1]}') self.inline(line, ans) else: ans.append(line) i += 1 filex.write_lines(output, ans, add_line_separator=True)
def translate_file_by_reference(self, en_file, cn_file, result_file=None, untranslated_replace=None): """ 根据参考翻译文件 :param en_file:英文 :param cn_file: 参考中文 :param result_file: 结果文件 :param untranslated_replace:未翻译时替换 :return: """ if result_file is None: result_file = filex.get_result_file_name(en_file, '_translation_result') translation_dict = Tools.get_dict_from_file(cn_file) result = self.translate_file_by_dict(en_file, translation_dict, untranslated_replace) filex.write_lines(result_file, result)
def change_unicode_to_chinese(file_path, output_file=None): """ 将unicode转为中文 :param file_path:源文件 :param output_file: 输出文件 :return: """ if output_file is None: output_file = filex.get_result_file_name(file_path, '_cn_result') lines = filex.read_lines(file_path) if lines is None: return result = [] for line in lines: line = line.replace('\n', '') if '=' in line: key_value = line.split('=', 1) line = '%s=%s' % (key_value[0], encodex.unicode_str_to_chinese(key_value[1])) result.append(line + '\n') filex.write_lines(output_file, result)
def filter(file_path, ignore_reg_list, result_path=None): """过滤掉不需要翻译的""" if result_path is None: result_path = filex.get_result_file_name(file_path, '_filter') """删除空翻译""" tree = Et.parse(file_path) tmx = tree.getroot() body = tmx.find('body') empty_translation_list = list() for tu in body.iter('tu'): en = None for tuv in tu.iter('tuv'): if tuv.attrib['lang'] == 'EN-US': en = tuv.find('seg').text continue_loop = False if ignore_reg_list: for ignore_reg in ignore_reg_list: if ignore_reg.startswith('^'): if re.match(ignore_reg, en): continue_loop = True else: # 不以 ^ 开头才搜索 if re.search(ignore_reg, en): continue_loop = True if continue_loop: print('\n跳过【%s】' % en) empty_translation_list.append(tu) break if continue_loop: continue print('删除 %d 条空翻译' % len(empty_translation_list)) for empty_translation in empty_translation_list: body.remove(empty_translation) tree.write(result_path, encoding='utf-8') print('保存完成')
def filter_log(self, log_file): lines = filex.read_lines(log_file) print(f'日志 {len(lines)} 行') lines = list(filter(self.filter_line, lines)) print(f'过滤后 {len(lines)} 行') filex.write_lines(filex.get_result_file_name(log_file, '_result'), lines)
def process_translated_keymap_file(en_file, cn_file, result_file=None): """ 处理翻译完的文件 :param en_file: :param cn_file: :param result_file: :return: """ if result_file is None: result_file = filex.get_result_file_name(cn_file, '_final') en_lines = filex.read_lines(en_file) cn_lines = filex.read_lines(cn_file) if cn_lines is None: return result = [] p_title = re.compile(r'^\[(#+)\]\s?(.*)') p_title2 = re.compile(r'(\n*)(#+\s)(.*)') p_desc = re.compile(r'^\[desc\]\s?(.*)') p_comment = re.compile(r'^#\[(x+)\](.*)') for i in range(len(cn_lines)): line = cn_lines[i].replace('\n', '') if line.startswith('#'): # 以#开头不翻译,取原文 line = en_lines[i].replace('\n', '') match = re.match(p_comment, line) if match is not None: # 注释 start_number = match.group(1) if len(start_number) == 1: # 1颗星标记基本注释 replace_result = ' \n译注:%s' % (match.group(2)) else: # 2星以上为加重注释 # 向上寻找标题并加重 last_index = -1 while result[last_index] == '' or re.match(p_title2, result[last_index]) is None: last_index -= 1 last_line = result[last_index] if '★' not in last_line: start_replace = '★' * (len(start_number) - 1) last_line = re.sub(p_title2, r'\1\2*%s\3%s*' % (start_replace, start_replace), last_line) print('替换标题【%s】为【%s】' % (result[last_index], last_line)) result[last_index] = last_line replace_result = ' \n译注:%s' % (match.group(2)) # print('替换【%s】为【%s】' % (line, replace_result)) line = '%s' % replace_result else: # 不以#开头 line = encodex.unicode_str_to_chinese(line) en_line = en_lines[i].replace('\n', '') match = re.match(p_title, line) if match is not None: # 是标题 en_match = re.match(p_title, en_line) if len(en_match.group(1)) == 5: replace_result = '\n\n%s(%s)' % (en_match.group(2), match.group(2)) else: replace_result = '\n\n%s %s(%s)' % (en_match.group(1), en_match.group(2), match.group(2)) # print('\n替换【%s】为【%s】' % (line, replace_result)) line = '%s' % replace_result else: desc_cn_match = re.match(p_desc, line) if desc_cn_match is not None: # 是描述 desc_en_match = re.match(p_desc, en_line) replace_result = ' \n%s \n描述:%s' % (desc_en_match.group(1), desc_cn_match.group(1)) # print('\n描述替换【%s】为【%s】' % (line, replace_result)) line = '%s' % replace_result result.append(line) filex.write_lines(result_file, result)
def get_cn_file_name(en_dir, cn_dir, en_file, suffix=None): """获取英文文件对应的中文文件,可能会变,所以提出来""" cn_file = en_file.replace(en_dir, cn_dir) cn_file = filex.get_result_file_name(cn_file, suffix) return cn_file
def compare_translation(en_dir, compare_dir_list, omegat_dict_file=None, dict_file=None, dict_diff_file=None, by_index=False, trans_unicode=True): if dict_file is None: dict_file = 'data/dict.txt' if dict_diff_file is None: dict_diff_file = filex.get_result_file_name(dict_file, '_diff') separator = '[xx|]' dict_list = list() for i in compare_dir_list: if i == r'C:\Users\Admin\Desktop\AndroidStudio汉化\汉化包\整理': t_dict = dict() for i_file in filex.list_file(i, '.properties'): t_dict.update(filex.get_dict_from_file(i_file)) dict_list.append(t_dict) continue i_all_translation, i_diff_translation = Translator.check_same_en_difference_cn( en_dir, i, False, '', trans_unicode=trans_unicode) dict_list.append(i_all_translation) if omegat_dict_file is not None: dict_list.insert(0, Translator.get_omegat_dict(omegat_dict_file)) for i in range(len(dict_list)): print('%d中共包含翻译%d条' % (i + 1, len(sorted(dict_list[i].keys())))) all_translation = dict() diff_translation = dict() print_i = True if by_index: # 按倒序更新,得到结果 for i in range(len(dict_list) - 1, -1, -1): all_translation.update(dict_list[i]) print('更新%d后,size是%d' % (i + 1, len(sorted(all_translation.keys())))) else: for i in range(len(dict_list)): i_dict = dict_list[i] index = 0 length = len(sorted(i_dict.keys())) for key, i_value in i_dict.items(): index += 1 if print_i: print('\n检查%d/%d,%s' % (index, length, key)) print('词典%d中是%s' % (i, i_value)) has_diff = False for j in range(i + 1, len(dict_list)): j_dict = dict_list[j] if key in j_dict: j_value = j_dict[key] if i_value == j_value: if print_i: print('词典%d中相同' % j) else: has_diff = True if key in diff_translation.keys(): pre_translation = diff_translation[key] if j_value not in pre_translation.split( separator): diff_translation[ key] = pre_translation + separator + j_value.replace( '\n', '') else: diff_translation[key] = (i_value + separator + j_value).replace( '\n', '') if print_i: print('词典%d中是%s' % (j, j_value)) # 处理后移除 j_dict.pop(key) else: if print_i: print('词典%d中缺少' % j) if not has_diff: if print_i: print('统一翻译') if i_value: # 只添加不为空的 all_translation[key] = i_value print('%d中处理%d条,其中%d条翻译相同,%d条不同' % (i, len(sorted( i_dict.keys())), len(sorted(all_translation.keys())), len(sorted(diff_translation.keys())))) print('size is %d' % len(sorted(all_translation.keys()))) if all_translation: if dict_file.endswith('.tmx') or dict_file.endswith('.tmx.xml'): Tools.save_omegat_dict(all_translation, dict_file) else: result = list() for key, value in all_translation.items(): result.append('%s=%s\n' % (key, value)) filex.write_lines(dict_file, result) print('diff size is %d' % len(sorted(diff_translation.keys()))) if diff_translation: result = list() for key, value in diff_translation.items(): result.append('%s=%s\n' % (key, value)) filex.write_lines(dict_diff_file, result)