def judge_translate_exist(log_file, translate_res_dir, translate_str, copy_mode=None): lookup_key_list = [] kevin_utils.print_log(log_file, "\n项目中的字符: %s\n" % translate_str) for path in os.listdir(translate_res_dir + "\\values"): file_path = translate_res_dir + "\\values\\" + path if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile(file_path): file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: string_list = re.findall(filter_string_value_regular, temp_read_str) if string_list: for string in string_list: if string == translate_str: lookup_key_list.append( re.findall( kevin_utils.get_filter_key_regular( copy_mode), temp_read_str)[0]) temp_read_str = "" line = file.readline() file.close() return lookup_key_list
def statistics_count(project_res_dir, copy_mode=None): for root, dirs, file_paths in os.walk(project_res_dir): if dirs: for res_dir_name in dirs: for file_name in os.listdir(project_res_dir + "\\" + res_dir_name): file_path = project_res_dir + "\\" + res_dir_name + "\\" + file_name if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile( file_path): file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: key_list = re.findall( kevin_utils.get_filter_key_regular( copy_mode), temp_read_str) if key_list: for key in key_list: if key in string_key_list: string_count_list[ key] = string_count_list[ key] + 1 temp_read_str = "" line = file.readline() file.close()
def string_in_project(res_dir_name, string_key, copy_mode=None): """ 判断字符在项目文件中是否不存在 res_dir_name : 资源文件夹名称 string_key : 字符名称 """ dir_path = project_res_dir + "\\" + res_dir_name + "\\" if not os.path.exists(dir_path) or os.path.isfile(dir_path): return False temp_file_list = os.listdir(dir_path) for file_name in temp_file_list: file_path = project_res_dir + "\\" + res_dir_name + "\\" + file_name if kevin_utils.get_filter_file_name( copy_mode) in file_name and os.path.isfile(file_path): if os.path.exists(file_path): file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: key_list = re.findall( kevin_utils.get_filter_key_regular(copy_mode), temp_read_str) if key_list: if key_list[0] == string_key: return True temp_read_str = "" line = file.readline() file.close() return False
def translate_res_delete_string(translate_res_dir, callback=None, copy_mode=None): if callback: callback(1, 100, label="正在删除重复的Key...") for root, dirs, file_paths in os.walk(translate_res_dir): if dirs: count = 0 for res_dir_name in dirs: count += 1 for file_name in os.listdir(translate_res_dir + "\\" + res_dir_name): file_path = translate_res_dir + "\\" + res_dir_name + "\\" + file_name if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile( file_path): file = open(file_path, encoding='utf-8') temp_file = open(file_path + ".ijs", "w", encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: key_list = re.findall( kevin_utils.get_filter_key_regular( copy_mode), temp_read_str) if key_list: for i in range(len(key_list)): if key_list[ i] in translate_project_key_list: index = translate_project_key_list.index( key_list[i]) if translate_reference_key_list[ index] != translate_project_key_list[ index]: old_str = "<string name=\"" + key_list[ i] + "\">" new_str = "<string name=\"" + "让此字符变的没有作用" + "\">" temp_read_str = temp_read_str.replace( old_str, new_str) temp_file.write(temp_read_str) temp_read_str = "" line = file.readline() temp_file.write(temp_read_str) temp_file.close() file.close() os.remove(file_path) os.rename(file_path + ".ijs", file_path) if callback: callback(count, len(dirs)) if callback: callback(100, 100)
def delete_empty_res_dir(project_res_dir, log_file, copy_mode=None): for root, dirs, file_paths in os.walk(project_res_dir): if dirs: for res_dir in dirs: is_delete_dir = True for child_root, child_dirs, child_file_paths in os.walk( project_res_dir + "\\" + res_dir): if child_dirs: is_delete_dir = False if is_delete_dir: for file_name in child_file_paths: if kevin_utils.get_filter_file_name( copy_mode) not in file_name: is_delete_dir = False break if is_delete_dir: for path in os.listdir(project_res_dir + "\\" + res_dir): file_path = project_res_dir + "\\" + res_dir + "\\" + path if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile( file_path): file = open(file_path, mode='r', encoding='utf-8') line = file.readline() while line: key_list = re.findall( kevin_utils.get_filter_key_regular( copy_mode), line) if key_list: is_delete_dir = False break line = file.readline() file.close() if is_delete_dir: os.remove(file_path) if is_delete_dir: os.rmdir(project_res_dir + "\\" + res_dir) kevin_utils.print_log(log_file, "删除空语言: %s" % res_dir)
def statistics_string(project_res_dir, browser=None, copy_mode=None): if browser: browser.clear() string_key_list.clear() string_value_list.clear() string_count_list.clear() for path in os.listdir(project_res_dir + "\\values\\"): file_path = project_res_dir + "\\values\\" + path if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile(file_path): file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: string_list = re.findall( kevin_utils.get_filter_key_regular(copy_mode), temp_read_str) if string_list: string = temp_read_str.strip("\n").strip(" ") string_key_list.append(string_list[0]) string_value_list.append(string) string_count_list[string_list[0]] = 0 temp_read_str = "" if "<!--" in temp_read_str and "-->" in temp_read_str: if re.findall("""<!--(.+?)-->""", temp_read_str): temp_read_str = "" line = file.readline() file.close() statistics_count(project_res_dir) if browser: for i in range(len(string_value_list)): browser.append(string_value_list[i]) count = string_count_list[string_key_list[i]] if count >= 20: browser.append("翻译数" + kevin_utils.text_style1(count)) else: browser.append("翻译数" + kevin_utils.text_style2(count)) browser.append("\n")
def find_translate(translate_res_dir, project_res_dir, callback=None, ignore_language_list=None, copy_mode=None): if callback: callback(1, 100, label="正在解析字符...") global translate_reference_key_list translate_reference_key_list.clear() global translate_project_key_list translate_project_key_list.clear() if not os.path.exists(kevin_utils.get_log_path()): os.makedirs(kevin_utils.get_log_path()) log_file = open(kevin_utils.get_log_path() + log_find_translate_copy_rename, mode='w', encoding='utf-8') translate_string_list = "" count = 1 for path in os.listdir(project_res_dir + "\\values"): file_path = project_res_dir + "\\values\\" + path if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile(file_path): file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: count += 1 temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: string_list = re.findall(filter_string_value_regular, temp_read_str) if string_list: for i in range(len(string_list)): lookup_key_list = judge_translate_exist( log_file, translate_res_dir, string_list[i]) for reference_key in lookup_key_list: if reference_key and reference_key not in translate_reference_key_list: project_key = re.findall( kevin_utils.get_filter_key_regular( copy_mode), temp_read_str)[i] if project_key and project_key not in translate_project_key_list: translate_reference_key_list.append( reference_key) translate_project_key_list.append( project_key) string = create_sting( project_key, string_list[i]) translate_string_list = translate_string_list + string + "\n" kevin_utils.print_log( log_file, "竞争产品中Key: %s \n" % reference_key) kevin_utils.print_log( log_file, "项目中Key: %s\n" % project_key) kevin_utils.print_log( log_file, "%s\n" % string) kevin_utils.print_log(log_file, "\n%s\n" % ("*" * 50)) temp_read_str = "" if callback: callback(min(count, 90), 100) line = file.readline() file.close() log_file.close() if callback: callback(100, 100) translate_res_delete_string(translate_res_dir, callback=callback) translate_res_rename_string(translate_res_dir, callback=callback) if translate_string_list: multiple_language_utils.copy_multiple_language( translate_string_list, translate_res_dir, project_res_dir, callback=callback, ignore_language_list=ignore_language_list, copy_mode=copy_mode) return translate_string_list
def delete_android_values_string(project_res_dir, input_strings, callback=None, ignore_language_list=None, copy_mode=None): if callback: callback(1, 100) data = { 'translate_string': input_strings, 'project_res_dir': project_res_dir } multiple_language.database.set_json_data(data) if not os.path.exists(kevin_utils.get_log_path()): os.makedirs(kevin_utils.get_log_path()) log_file = open(kevin_utils.get_log_path() + log_delete_res_string, mode='w', encoding='utf-8') delete_string_key_list = kevin_utils.analysis_equal_string( input_strings, log_file, copy_mode=copy_mode) for root, dirs, file_paths in os.walk(project_res_dir): if dirs: count = 0 for res_dir in dirs: count += 1 is_pass_path = False if ignore_language_list: for ignore_language in ignore_language_list: if ignore_language and ignore_language in res_dir: is_pass_path = True if not is_pass_path and os.path.exists(project_res_dir + "\\" + res_dir): if "values" in res_dir: # res_dir != "values" kevin_utils.print_log(log_file, "\n%s\n" % res_dir) for path in os.listdir(project_res_dir + "\\" + res_dir): file_path = project_res_dir + "\\" + res_dir + "\\" + path if kevin_utils.get_filter_file_name( copy_mode) in file_path and os.path.isfile( file_path): tmp_file_path = file_path + ".ijs" file = open(file_path, mode='r', encoding='utf-8') tmp_file = open(tmp_file_path, mode='w', encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: key_list = re.findall( kevin_utils.get_filter_key_regular( copy_mode), temp_read_str) delete_enable = False if key_list: for key in delete_string_key_list: if key == key_list[0]: delete_enable = True kevin_utils.print_log( log_file, "已删除 %s\n" % key) break if not delete_enable: tmp_file.write(temp_read_str) temp_read_str = "" line = file.readline() file.close() tmp_file.close() os.remove(file_path) os.rename(tmp_file_path, file_path) break kevin_utils.print_log(log_file, "\n%s\n" % ("*" * 50)) if callback: callback(count, len(dirs)) log_file.close() if callback: callback(100, 100)
def analysis_add_string(res_dir_name, add_string_key_list, log_file, copy_mode=None): """ 判断项目资源文件夹中是否存在翻译字符,存在则不添加 res_dir_name : 资源文件夹名称 add_string_key_list : 需要添加字符的Key集合 """ no_string_or_strings = True temp_file_list = os.listdir(translate_res_dir + "\\" + res_dir_name) for file_name in temp_file_list: file_path = translate_res_dir + "\\" + res_dir_name + "\\" + file_name if kevin_utils.get_filter_file_name( copy_mode) in file_name and os.path.isfile(file_path): add_string = "" no_string_or_strings = False file = open(file_path, encoding='utf-8') line = file.readline() temp_read_str = "" while line: temp_read_str += line end_string = "</%s>" % kevin_utils.get_filter_key_value( copy_mode) if "resources" in temp_read_str or end_string in temp_read_str: key_list = re.findall( kevin_utils.get_filter_key_regular(copy_mode), temp_read_str) if key_list: for key in add_string_key_list: if key == key_list[0]: # 需要添加的字符项目中不存在 add_string_enable_1 = not string_in_project( res_dir_name, key_list[0], copy_mode=copy_mode) # 需要添加的字符不能重复 add_string_enable_2 = key not in re.findall( kevin_utils.get_filter_key_regular( copy_mode), add_string) if add_string_enable_1 and add_string_enable_2: add_string += "\n" if len( add_string) != 0 else "" if temp_read_str.endswith("\n"): add_string += temp_read_str[:-1] else: add_string += temp_read_str break temp_read_str = "" line = file.readline() file.close() add_string = add_string[:-1] if add_string.endswith( "\n") else add_string if add_string: add_string_to_project_res(res_dir_name, add_string, file_name) kevin_utils.print_log( log_file, "\n语言文件夹 = %s\n新增字符:\n%s\n\n%s\n" % (res_dir_name, add_string, "*" * 50)) else: kevin_utils.print_log( log_file, "\n语言文件夹 = %s\n新增字符:空\n\n%s\n" % (res_dir_name, "*" * 50)) if no_string_or_strings: kevin_utils.print_log( log_file, "\n语言文件夹 = %s\n此文件夹下没有string.xml或者strings.xml\n\n%s\n" % (res_dir_name, "*" * 50))