def load_category(translation_path, category): """读取一类,存到一个 xlsx 文件中 Args: translation_path (str): 翻译文件路径 category (str): 分类名字 """ # 调用 prepare_xxx_lang, prepare_xxx_lang 中再调用 load_lang_xxx, save_lang_xxx if category in file_id_of_pair.keys(): rows = prepare_pair_lang(category, file_id_of_pair[category], translation_path) elif category in file_id_of_list.keys(): rows = load_lang_list(category, file_id_of_list[category], translation_path) elif category in file_id_of_array.keys(): rows = load_lang_array(category, file_id_of_array[category], translation_path) else: usage() sys.exit(2) # save dest_filename = 'en.%ss.lang.xlsx' % category save_xlsx(os.path.join(translation_path, dest_filename), rows[1:], header=rows[0]) log.info('save to %s' % dest_filename)
def main(): if len(sys.argv) != 3: usage() sys.exit(2) xls_dir, output_dir = sys.argv[1:] data_pair, data_list = join_all_xls(xls_dir) save_xlsx(os.path.join(output_dir, 'pair.xlsx'), data_pair) save_xlsx(os.path.join(output_dir, 'list.xlsx'), data_list)
def merge_translation_file(dest_xls_path, src_xls_path, conflict_xls_file, check_category=True): """合并两个文件中的数据,写回 dest 文件 Args: dest_xls_path (str): 合并目标文件路径,当发生冲突时以此文件为准 src_xls_path (str): 合并源文件路径 conflict_xls_file (str | None): 冲突数据存放文件 check_category (bool): 检查类型是否相等 """ # check category if check_category: category = get_category(dest_xls_path) category_src = get_category(src_xls_path) if category != category_src: log.error('category not equal.') raise RuntimeError('category not equal.') # load log.info('load %s' % dest_xls_path) dest_data = load_xls(dest_xls_path) header, dest_data = dest_data[0], dest_data[1:] log.info('load %s' % src_xls_path) src_data = load_xls(src_xls_path)[1:] category = get_category_of_id(dest_data[0][1]) # merge merged_data, conflict_data = merge_translation_data( category, dest_data, src_data) # sort merged_data = sorted(merged_data, key=lambda row: row[1]) conflict_data = sorted(conflict_data, key=lambda row: row[1]) # save log.info('%d conflicts.' % len(conflict_data)) log.info('save %s' % dest_xls_path) save_xlsx(dest_xls_path, merged_data, header=header) if conflict_xls_file is not None and len(conflict_data) > 0: log.info('save %s' % conflict_xls_file) save_xlsx(conflict_xls_file, conflict_data, header=header)
def merge_diff_files(files, output_path): """按要求合并翻译文件,并写入文件""" if len(files) <= 0: return header = None all_diff_data = [] for filename in files: log.info('loading %s...' % os.path.basename(filename)) diff_data = load_xls(filename) header, diff_data = diff_data[0], diff_data[1:] all_diff_data.extend(diff_data) # 去空 category = get_category_of_id(all_diff_data[0][1]) if category in lang_def.file_id_of_array or category in lang_def.file_id_of_list: all_diff_data = [row for row in all_diff_data if row[4] != ''] elif category in lang_def.file_id_of_pair: all_diff_data = [row for row in all_diff_data if row[4] + row[7] != ''] else: log.error('unknown category %s.' % category) raise RuntimeError('unknown category') # 去重 all_diff_data_by_id = {} for row in all_diff_data: row = tuple(row) key = row[0] if key not in all_diff_data_by_id: all_diff_data_by_id[key] = {row} else: if row not in all_diff_data_by_id: all_diff_data_by_id[key].add(row) # 按顺序重排 all_diff_data = [] for _, rows_set in sorted(all_diff_data_by_id.items()): all_diff_data.extend(rows_set) output_name = os.path.basename(output_path) if len(all_diff_data) > 0: log.info('saving %s...' % output_name) save_xlsx(output_path, all_diff_data, header=header) else: log.info('skip %s' % output_name)
def merge_diff_files(files, output_path): """按要求合并翻译文件,并写入文件""" if len(files) <= 0: return header = None all_diff_data = [] for filename in files: log.info('loading %s...' % os.path.basename(filename)) diff_data = load_xls(filename) header, diff_data = diff_data[0], diff_data[1:] all_diff_data.extend(diff_data) # 去空 category = get_category_of_id(all_diff_data[0][1]) if category in lang_def.file_id_of_array or category in lang_def.file_id_of_list: all_diff_data = [row for row in all_diff_data if row[4] != ''] elif category in lang_def.file_id_of_pair: all_diff_data = [row for row in all_diff_data if row[4] + row[7] != ''] else: log.error('unknown category %s.' % category) raise RuntimeError('unknown category') # 去重 all_diff_data_by_id = {} for row in all_diff_data: row = tuple(row) key = row[0] if key not in all_diff_data_by_id: all_diff_data_by_id[key] = {row} else: if row not in all_diff_data_by_id: all_diff_data_by_id[key].add(row) # 按顺序重排 all_diff_data = [] for _, rows_set in sorted(all_diff_data_by_id.items()): all_diff_data.extend(rows_set) output_name = os.path.basename(output_path) if len(all_diff_data) > 0: log.info('saving %s...' % output_name) save_xlsx(output_path, all_diff_data, header=header) else: log.info('skip %s' % output_name)
def main(): lang = 'zh' # getopt try: opts, args = getopt.getopt(sys.argv[1:], 'l:') except getopt.GetoptError as e: log.error(e) sys.exit(2) for o, a in opts: if o == '-l': lang = a cd = sys.path[0] translation_path = os.path.join(cd, '../translation') dest_path = translation_path ui_mgr = UiMgr() # load translation translate_file = os.path.join(translation_path, '%s_translate.txt' % lang) ui_mgr.load_lua_file(translate_file) ui_mgr.apply_translate_from_txt_file( translate_file) # _translate.txt 中同时存了原文和译文 rows = ui_mgr.get_rows() rows = [row for name, row in sorted(rows.items())] header = UiRow('名称') \ .set_id('编号') \ .set_origin('原文') \ .set_translation('译文') \ .set_translator('初翻人员') \ .set_proofreader('校对') \ .set_refiner('润色') \ .set_comments('备注') rows[0:0] = [ header, ] # save xls_file = os.path.join(dest_path, '%s_translate.xlsx' % lang) plain_rows = [row.to_list() for row in rows ] # list of [id, name, origin, translation, ...] save_xlsx(xls_file, plain_rows)
def main(): lang = 'zh' # getopt try: opts, args = getopt.getopt(sys.argv[1:], 'l:') except getopt.GetoptError as e: log.error(e) sys.exit(2) for o, a in opts: if o == '-l': lang = a cd = sys.path[0] translation_path = os.path.join(cd, '../translation') dest_path = translation_path ui_mgr = UiMgr() # load translation translate_file = os.path.join(translation_path, '%s_translate.txt' % lang) ui_mgr.load_lua_file(translate_file) ui_mgr.apply_translate_from_txt_file(translate_file) # _translate.txt 中同时存了原文和译文 rows = ui_mgr.get_rows() rows = [row for name, row in sorted(rows.items())] header = UiRow('名称') \ .set_id('编号') \ .set_origin('原文') \ .set_translation('译文') \ .set_translator('初翻人员') \ .set_proofreader('校对') \ .set_refiner('润色') \ .set_comments('备注') rows[0:0] = [header, ] # save xls_file = os.path.join(dest_path, '%s_translate.xlsx' % lang) plain_rows = [row.to_list() for row in rows] # list of [id, name, origin, translation, ...] save_xlsx(xls_file, plain_rows)
def merge_translation_file(dest_xls_path, src_xls_path, conflict_xls_file, check_category=True): """合并两个文件中的数据,写回 dest 文件 Args: dest_xls_path (str): 合并目标文件路径,当发生冲突时以此文件为准 src_xls_path (str): 合并源文件路径 conflict_xls_file (str | None): 冲突数据存放文件 check_category (bool): 检查类型是否相等 """ # check category if check_category: category = get_category(dest_xls_path) category_src = get_category(src_xls_path) if category != category_src: log.error('category not equal.') raise RuntimeError('category not equal.') # load log.info('load %s' % dest_xls_path) dest_data = load_xls(dest_xls_path) header, dest_data = dest_data[0], dest_data[1:] log.info('load %s' % src_xls_path) src_data = load_xls(src_xls_path)[1:] category = get_category_of_id(dest_data[0][1]) # merge merged_data, conflict_data = merge_translation_data(category, dest_data, src_data) # sort merged_data = sorted(merged_data, key=lambda row: row[1]) conflict_data = sorted(conflict_data, key=lambda row: row[1]) # save log.info('%d conflicts.' % len(conflict_data)) log.info('save %s' % dest_xls_path) save_xlsx(dest_xls_path, merged_data, header=header) if conflict_xls_file is not None and len(conflict_data) > 0: log.info('save %s' % conflict_xls_file) save_xlsx(conflict_xls_file, conflict_data, header=header)
def merge_translation_file(dest_xls_path, src_xls_path, conflict_xls_file): """合并两个文件中的数据,写回 dest 文件 Args: dest_xls_path (str): 合并目标文件路径,当发生冲突时以此文件为准 src_xls_path (str): 合并源文件路径 conflict_xls_file (str | None): 冲突数据存放文件 """ # load print('load %s' % dest_xls_path) dest_data = load_xls(dest_xls_path) header, dest_data = dest_data[0], dest_data[1:] print('load %s' % src_xls_path) src_data = load_xls(src_xls_path)[1:] # check if not (dest_data[0][1].startswith('SI_') and src_data[0][1].startswith('SI_')): raise RuntimeError('invalid ui xls file') # merge merged_data, conflict_data = merge_translation_by_col( dest_data, src_data, id_col=1, origin_col_ids=[ 2, ], translation_col_ids=[3, 4, 5, 6, 7]) # save print('%d conflicts.' % len(conflict_data)) print('save %s' % dest_xls_path) save_xlsx(dest_xls_path, merged_data, header=header) if conflict_xls_file is not None and len(conflict_data) > 0: print('save %s' % conflict_xls_file) save_xlsx(conflict_xls_file, conflict_data, header=header)
def load_category(translation_path, category): """读取一类,存到一个 xlsx 文件中 Args: translation_path (str): 翻译文件路径 category (str): 分类名字 """ # 调用 prepare_xxx_lang, prepare_xxx_lang 中再调用 load_lang_xxx, save_lang_xxx if category in file_id_of_pair.keys(): rows = prepare_pair_lang(category, file_id_of_pair[category], translation_path) elif category in file_id_of_list.keys(): rows = load_lang_list(category, file_id_of_list[category], translation_path) elif category in file_id_of_array.keys(): rows = load_lang_array(category, file_id_of_array[category], translation_path) else: usage() sys.exit(2) # save dest_filename = 'en.%ss.lang.xlsx' % category save_xlsx(os.path.join(translation_path, dest_filename), rows[1:], header=rows[0]) log.info('save to %s' % dest_filename)