| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import json
- import os
- import pandas as pd
- def read_from_excel(oem):
- # 读取输出的Excel文件, 格式:key(A) zh-CN(B) en-US(C) th-TH(C) zh-TW(D)
- excel_filename = "output/excels/" + oem + ".xlsx"
- df = pd.read_excel(excel_filename)
- # 将 A 列和 C 列转换为zh-CN字典
- dict_cn = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
- # 将 A 列和 C 列转换为en-US字典
- dict_en = dict(zip(df.iloc[:, 0], df.iloc[:, 2]))
- # 将 A 列和 D 列转换为th-TH字典
- dict_th = dict(zip(df.iloc[:, 0], df.iloc[:, 3]))
- # 将 A 列和 E 列转换为zh-TW字典
- dict_tw = dict(zip(df.iloc[:, 0], df.iloc[:, 4]))
- return dict_cn, dict_en, dict_th, dict_tw
- def read_from_json(file_path):
- # 从JSON文件中读取内容
- with open(file_path, 'r', encoding='utf-8') as file:
- data = json.load(file)
- return data
- def get_leaf_paths(data, path="", result=None):
- # 递归函数,遍历所有终节点并将全路径和值存入字典
- # 不需要翻译的Key
- ignore_data = ['app.name', 'mine.setting.chinese',
- 'mine.setting.english', 'mine.setting.enth']
- if result is None:
- result = {}
- if isinstance(data, dict):
- # 如果是字典,继续遍历字典中的每个键
- for key, value in data.items():
- current_path = f"{path}.{key}" if path else key
- get_leaf_paths(value, current_path, result)
- elif isinstance(data, list):
- # 如果是列表,遍历每个元素
- for index, item in enumerate(data):
- current_path = f"{path}[{index}]"
- get_leaf_paths(item, current_path, result)
- else:
- # 如果是终节点,将当前路径和值保存到字典中
- if path not in ignore_data:
- result[path] = data
- return result
- def generate_dict_from_json(file_path):
- data = read_from_json(file_path)
- # 生成全路径的字典
- leaf_paths_dict = get_leaf_paths(data)
- return leaf_paths_dict
- # 定义递归函数来遍历并修改 JSON 的值,同时传递键的全路径和辅助字典
- def modify_json_values(data, modify_func, path="", extra_data=None, exclude_data=None):
- if isinstance(data, dict): # 如果是字典类型,遍历键值对
- for key, value in data.items():
- new_path = f"{path}.{key}" if path else key # 更新路径
- if isinstance(value, (dict, list)): # 如果值是字典或列表,递归调用
- modify_json_values(value, modify_func,
- new_path, extra_data, exclude_data)
- else:
- # 对终节点进行修改,传递完整路径和辅助字典
- data[key] = modify_func(
- value, new_path, extra_data, exclude_data)
- elif isinstance(data, list): # 如果是列表类型,遍历列表中的每个元素
- for index, item in enumerate(data):
- new_path = f"{path}[{index}]" # 更新路径为列表的索引
- if isinstance(item, (dict, list)):
- modify_json_values(item, modify_func,
- new_path, extra_data, exclude_data)
- else:
- # 修改列表中的值,并传递完整路径
- data[index] = modify_func(item, new_path, extra_data)
- def modify_func(value, path, extra_data, exclude_data):
- # 修改函数,基于额外字典的内容修改值
- if extra_data and path in extra_data:
- # exclude_data 为空 或是path不在exclude_data
- if (exclude_data is None) or (exclude_data and path not in exclude_data):
- new_value = extra_data[path].replace('\r\n', '\n')
- return new_value.replace('\n', '\r\n') # 从 extra_data 中取出新值
- return value # 如果路径不在 extra_data 中,保持原值
- def save_to_json(path, data):
- # 创建目录(如果不存在)
- directory = os.path.dirname(path) # 获取文件路径的目录部分
- if not os.path.exists(directory):
- os.makedirs(directory) # 创建目录
- # 将修改后的字典保存为 .json 文件
- with open(path, 'w', encoding='utf-8') as json_file:
- json.dump(data, json_file, ensure_ascii=False, indent=4)
- def generate_json(oem, excel_dic, lang):
- # OEM的Lang文件
- oem_input = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
- oem + '/locales/extras/' + lang + '.json'
- # OEM输出文件地址
- oem_output = 'output/jsons/' + oem + '/' + lang + '.json'
- # 读取OEM的JSON文件
- oem_data = read_from_json(oem_input)
- # 更新 common_data
- modify_json_values(oem_data, modify_func, extra_data=excel_dic)
- # 另存为OEM的json文件
- save_to_json(oem_output, oem_data)
- print("%s %s json save success" % (oem, lang))
- # 通用JSON:oem中存在的Key,则不更新common的值
- oem_dic = generate_dict_from_json(oem_input)
- # 通用的Lang文件
- common_input = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/public/locales/' + lang + '.json'
- # 输出文件地址
- common_output = 'output/jsons/' + lang + '.json'
- # 读取JSON文件
- common_data = read_from_json(common_input)
- # 更新 common_data
- modify_json_values(common_data, modify_func,
- extra_data=excel_dic, exclude_data=oem_dic)
- # 另存为json文件
- save_to_json(common_output, common_data)
- print("%s json save success" % lang)
- if __name__ == '__main__':
- oem = 'tss'
- dic_cn, dic_en, dic_th, dic_tw = read_from_excel(oem)
- generate_json(oem, dic_cn, 'zh-CN')
- generate_json(oem, dic_en, 'en-US')
- generate_json(oem, dic_th, 'th-TH')
- generate_json(oem, dic_tw, 'zh-TW')
|