0,建议首先创建3个文件夹:
①:1、values_all
②:2、xml
③:3、output
1,android项目多语言strings.xml文件复制到一个文件夹:
import os
import shutildef find_string_xml(directory):string_xml_files = []for root, _, filenames in os.walk(directory):for filename in filenames:if filename == "strings.xml":string_xml_files.append(os.path.join(root, filename))return string_xml_filesdef copy_file(source_file, dest_file):try:with open(source_file, 'rb') as f_source, open(dest_file, 'wb') as f_dest:shutil.copyfileobj(f_source, f_dest)except Exception as e:print(f"复制 {source_file} 到 {dest_file} 失败:{e}")def get_parent_dir_name(file_path):if not os.path.exists(file_path):raise FileNotFoundError(f"文件 {file_path} 不存在。")# 获取父目录的绝对路径parent_dir = os.path.dirname(file_path)# 获取父目录的名称parent_dir_name = os.path.basename(parent_dir)return parent_dir_namedef find_and_copy_string_xml(source_directory, dest_directory):copied_files = 0for source_file in find_string_xml(source_directory):parent_name = get_parent_dir_name(source_file)file_name = os.path.basename(source_file)dest_file = os.path.join(dest_directory, f"{parent_name}_{file_name}")copy_file(source_file, dest_file)copied_files += 1print(f"已将 {source_file} 复制到 {dest_file}")return copied_files# 使用示例
source_directory = "C:\\Users\\DHY-20210315\\Documents\\2024-05-20\\1、values_all"
dest_directory = "C:\\Users\\DHY-20210315\\Documents\\2024-05-20\\2、xml"copied_files = find_and_copy_string_xml(source_directory, dest_directory)
print(f"共复制了 {copied_files} 个 strings.xml 文件。")
2,将项目现存的其他语言对齐到指定的语言strings.xml,不存在的填充指定的。其他语言多出来的添加在此语言strings.xml文件最后。
以下代码以英文为标准:
import os
import xml.etree.ElementTree as ETdef parse_xml_to_dict(xml_file):# 解析XML文件tree = ET.parse(xml_file)root = tree.getroot()# 创建一个空字典用于存储结果result = {}# 遍历XML中的每个<string>标签for string_elem in root.findall('string'):# 获取name属性值作为键,文本内容作为值key = string_elem.attrib['name']value = string_elem.text# 存储键值对到字典中result[key] = valuereturn resultdef write_dict_to_xml(basic_dict, result_dict, output_file):# 打开文件,写入XML头部信息with open(output_file, "w", encoding="utf-8") as f:f.write('<?xml version="1.0" encoding="utf-8"?>\n')f.write('<resources>\n')# 遍历字典,每个键值对创建一个<string>标签for key, value in basic_dict.items():value2 = result_dict.get(key)if value2:value = value2del result_dict[key]# 创建<string>标签f.write(' <string name="{}">{}</string>\n'.format(key, value))if result_dict:f.write('\n\n')for key, value in result_dict.items():f.write(' <string name="{}">{}</string>\n'.format(key, value))f.write('\n')# 写入XML结束标签f.write('</resources>\n')def start_run(basic_xml, in_folder_path, out_folder_path):if not os.path.exists(basic_xml):print("basic_xml不存在")return Falsebasic_dict = parse_xml_to_dict(basic_xml)# 获取文件夹中所有的XML文件路径xml_files = [file for file in os.listdir(in_folder_path) if file.endswith('.xml')]# 遍历每个XML文件并解析内容for xml_file in xml_files:xml_file_path = os.path.join(in_folder_path, xml_file)print("解析文件:", xml_file_path)# 解析XML文件并存储结果到字典中result_dict = parse_xml_to_dict(xml_file_path)write_dict_to_xml(basic_dict, result_dict, os.path.join(out_folder_path, xml_file))return Truestr_xml = "C:\\Users\\DHY-20210315\\Documents\\2024-05-20\\values_strings2.xml"
in_dir = "C:\\Users\\DHY-20210315\\Documents\\2024-05-20\\2、xml"
out_dir = "C:\\Users\\DHY-20210315\\Documents\\2024-05-20\\3、output"
if start_run(str_xml, in_dir, out_dir):print("操作完成~")