def check_same_en_difference_cn(en_dir, cn_dir, print_msg=False, suffix='', trans_unicode=True): """英文相同时,是否有不一致的翻译""" all_translation = dict() diff_translation = dict() en_file_list = filex.list_file(en_dir, '\.(?!png|gif)') for en_file in en_file_list: print('\ncheck ' + en_file) cn_file = Translator.get_cn_file_name(en_dir, cn_dir, en_file, suffix) if not os.path.exists(cn_file): print('中文文件不存在' + cn_file) continue en_dict = Tools.get_dict_from_file(en_file) cn_dict = Tools.get_dict_from_file(cn_file, delete_cn_shortcut=True, trans_unicode=trans_unicode) for key, en_value in en_dict.items(): if key in cn_dict.keys(): # 有key对应的中英文 cn_value = cn_dict[key] if cn_value != en_value: # 中英文不一样才算翻译 if en_value in all_translation.keys(): pre_translation = all_translation[en_value] if pre_translation != cn_value: if en_value not in diff_translation.keys(): diff_translation[ en_value] = pre_translation + '\n' + cn_value else: pre_diff_translation = diff_translation[ en_value] if cn_value not in pre_diff_translation.split( '\n'): # 之前没有记录过才再记录 diff_translation[ en_value] = pre_diff_translation + '\n' + cn_value if print_msg: print( '\n词典中已经存在%s,但翻译不相同\n%s\n%s' % (en_value, pre_translation, cn_value)) else: all_translation[en_value] = cn_value if print_msg: print('the size is %d' % len(sorted(all_translation.keys()))) # 读取守毕 for key in diff_translation.keys(): all_translation.pop(key) return all_translation, diff_translation
def get_action_desc_dict(en_file, print_msg=False): """ 读取操作对应的描述 :param en_file: :param print_msg: :return: """ en_dict = Tools.get_dict_from_file(en_file) desc_dict = dict() # 重复的处理起来麻烦,索性删除了 duplicate_value = list() # 反转 for key, value in en_dict.items(): if key.endswith('.text'): desc_key = key[:-len('.text')] + '.description' if desc_key in en_dict.keys(): desc = en_dict[desc_key] if value in desc_dict.keys(): pre_desc = desc_dict[value] if desc == pre_desc: print('%s重复【%s】' + value, desc) else: duplicate_value.append(value) desc = '%s【或】%s' % (pre_desc, desc) print('%s有不同的描述【%s】【%s】' % (value, desc, pre_desc)) desc_dict[value] = desc for value in duplicate_value: if value in desc_dict.keys(): desc_dict.pop(value) if print_msg: print('删除' + value) if print_msg: print(desc_dict) return desc_dict
def generate_need_translation_file2(en_dir, result_file): """生成需要翻译的文件""" en_file_list = filex.list_file(en_dir) all_translation = dict() for en_file in en_file_list: en_dict = Tools.get_dict_from_file(en_file) all_translation.update(en_dict) result = list() for key, value in all_translation.items(): result.append('%s=%s\n' % (key, value)) filex.write_lines(result_file, result)
def update_omegat_dict(dict_file, omegat_dict_file, result_dict_file): """更新omegat的记忆文件""" pre_dict = Tools.get_dict_from_file(dict_file) print('pre size is %d' % len(sorted(pre_dict.keys()))) omegat_dict = Translator.get_omegat_dict(omegat_dict_file) print('omegat size is %d' % len(sorted(omegat_dict.keys()))) # 更新,以omegat为准 pre_dict.update(omegat_dict) print('result size is %d' % len(sorted(pre_dict.keys()))) Tools.save_omegat_dict(pre_dict, result_dict_file)
def check_same_key_difference_value(dir_path): """如果key相同,value是否有不一致 发现确实有不一致,所以必须区分文件""" file_list = filex.list_file(dir_path) all_translation = dict() for file in file_list: print('\ncheck ' + file) translation_dict = Tools.get_dict_from_file( file, delete_value_and_symbol=True, trans_unicode=True) if all_translation: # 不为空再处理,为空的第一次直接update for key, value in translation_dict.items(): if key in all_translation.keys(): if all_translation[key] != value: print('key相同%s,但value不一致\n%s\n%s' % (key, all_translation[key], value)) all_translation.update(translation_dict)
def generate_need_translation_file(en_dir, result_dir): """生成需要翻译的文件""" en_file_list = filex.list_file(en_dir) all_translation = dict() for en_file in en_file_list: en_dict = Tools.get_dict_from_file(en_file) all_translation.update(en_dict) i = 0 size = 3000 result = list() for key, value in all_translation.items(): i += 1 result.append('%s=%s\n' % (key, value)) if i % size == size - 1: index = int(i / size) result_file = '%s\\%2d.properties' % (result_dir, index) filex.write_lines(result_file, result) result.clear() if result: index = int(i / size) result_file = '%s\\%2d.properties' % (result_dir, index) filex.write_lines(result_file, result) result.clear()
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 check_translation_complete(en_dir, cn_dir, out_put=None, suffix=''): """翻译是否完整""" incomplete_dict = dict() en_file_list = filex.list_file(en_dir, '\.(?!png|gif)') incomplete_file = [] miss_file = [] complete_count = 0 same_file = [] complete_file = [] for en_file in en_file_list: # print('\ncheck ' + en_file) cn_file = Translator.get_cn_file_name(en_dir, cn_dir, en_file, suffix) if not os.path.exists(cn_file): # print('中文文件不存在' + cn_file) miss_file.append(cn_file) continue if filecmp.cmp(en_file, cn_file): # print('文件相同' + en_file) same_file.append(en_file) continue en_dict = Tools.get_dict_from_file(en_file) cn_dict = Tools.get_dict_from_file(cn_file, trans_unicode=True) is_complete = True translation_count_in_file = 0 for key, en_value in en_dict.items(): if key not in cn_dict.keys(): is_complete = False incomplete_dict[key] = en_value # print('没有翻译%s对应的%s' % (key, en_value)) else: cn_value = cn_dict[key] if en_value == cn_value: is_complete = False incomplete_dict[key] = en_value # print('%s对应的翻译仍然是%s,未翻译' % (key, en_value)) else: translation_count_in_file += 1 complete_count += 1 if not is_complete: print('文件未完全翻译' + en_file) incomplete_file.append(en_file) else: if translation_count_in_file == 0: # 一句都没翻译 # print('文件一句都没翻译' + en_file) same_file.append(en_file) else: complete_file.append(en_file) print('文件翻译完整' + en_file) print('缺少%d个文件' % len(miss_file)) print(miss_file) print('有%d个文件完全相同' % len(same_file)) print('有%d个文件未翻译完整' % len(incomplete_file)) print(incomplete_file) print('有%d个文件完整翻译,共%d条翻译' % (len(complete_file), complete_count)) if out_put is not None: result = list() for key, value in incomplete_dict.items(): result.append('%s=%s\n\n' % (key, value)) print('incomplete size is %d' % len(sorted(incomplete_dict.keys()))) filex.write_lines(out_put, result)