manage_generate_json.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import json
  2. import os
  3. import pandas as pd
  4. output_folder="output_manage"
  5. def read_from_excel(oem):
  6. # 读取输出的Excel文件, 格式:key(A) zh-CN(B) en-US(C) th-TH(C) zh-TW(D)
  7. excel_filename = output_folder + "/excels/" + oem + ".xlsx"
  8. df = pd.read_excel(excel_filename)
  9. # 将 A 列和 C 列转换为zh-CN字典
  10. dict_cn = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
  11. # 将 A 列和 C 列转换为en-US字典
  12. dict_en = dict(zip(df.iloc[:, 0], df.iloc[:, 2]))
  13. # 将 A 列和 D 列转换为th-TH字典
  14. dict_th = dict(zip(df.iloc[:, 0], df.iloc[:, 3]))
  15. # 将 A 列和 E 列转换为zh-TW字典
  16. dict_tw = dict(zip(df.iloc[:, 0], df.iloc[:, 4]))
  17. return dict_cn, dict_en, dict_th, dict_tw
  18. def read_from_json(file_path):
  19. # 从JSON文件中读取内容
  20. with open(file_path, 'r', encoding='utf-8') as file:
  21. data = json.load(file)
  22. return data
  23. def get_leaf_paths(data, path="", result=None):
  24. # 递归函数,遍历所有终节点并将全路径和值存入字典
  25. # 不需要翻译的Key
  26. ignore_data = ['app.name', 'mine.setting.chinese',
  27. 'mine.setting.english', 'mine.setting.enth']
  28. if result is None:
  29. result = {}
  30. if isinstance(data, dict):
  31. # 如果是字典,继续遍历字典中的每个键
  32. for key, value in data.items():
  33. current_path = f"{path}.{key}" if path else key
  34. get_leaf_paths(value, current_path, result)
  35. elif isinstance(data, list):
  36. # 如果是列表,遍历每个元素
  37. for index, item in enumerate(data):
  38. current_path = f"{path}[{index}]"
  39. get_leaf_paths(item, current_path, result)
  40. else:
  41. # 如果是终节点,将当前路径和值保存到字典中
  42. if path not in ignore_data:
  43. result[path] = data
  44. return result
  45. def generate_dict_from_json(file_path):
  46. data = read_from_json(file_path)
  47. # 生成全路径的字典
  48. leaf_paths_dict = get_leaf_paths(data)
  49. return leaf_paths_dict
  50. # 定义递归函数来遍历并修改 JSON 的值,同时传递键的全路径和辅助字典
  51. def modify_json_values(data, modify_func, path="", extra_data=None, exclude_data=None):
  52. if isinstance(data, dict): # 如果是字典类型,遍历键值对
  53. for key, value in data.items():
  54. new_path = f"{path}.{key}" if path else key # 更新路径
  55. if isinstance(value, (dict, list)): # 如果值是字典或列表,递归调用
  56. modify_json_values(value, modify_func,
  57. new_path, extra_data, exclude_data)
  58. else:
  59. # 对终节点进行修改,传递完整路径和辅助字典
  60. data[key] = modify_func(
  61. value, new_path, extra_data, exclude_data)
  62. elif isinstance(data, list): # 如果是列表类型,遍历列表中的每个元素
  63. for index, item in enumerate(data):
  64. new_path = f"{path}[{index}]" # 更新路径为列表的索引
  65. if isinstance(item, (dict, list)):
  66. modify_json_values(item, modify_func,
  67. new_path, extra_data, exclude_data)
  68. else:
  69. # 修改列表中的值,并传递完整路径
  70. data[index] = modify_func(item, new_path, extra_data)
  71. def modify_func(value, path, extra_data, exclude_data):
  72. # 修改函数,基于额外字典的内容修改值
  73. if extra_data and path in extra_data:
  74. # exclude_data 为空 或是path不在exclude_data
  75. if (exclude_data is None) or (exclude_data and path not in exclude_data):
  76. new_value = extra_data[path].replace('\r\n', '\n')
  77. return new_value.replace('\n', '\r\n') # 从 extra_data 中取出新值
  78. return value # 如果路径不在 extra_data 中,保持原值
  79. def save_to_json(path, data):
  80. # 创建目录(如果不存在)
  81. directory = os.path.dirname(path) # 获取文件路径的目录部分
  82. if not os.path.exists(directory):
  83. os.makedirs(directory) # 创建目录
  84. # 将修改后的字典保存为 .json 文件
  85. with open(path, 'w', encoding='utf-8') as json_file:
  86. json.dump(data, json_file, ensure_ascii=False, indent=4)
  87. def generate_json(oem, excel_dic, lang):
  88. '''
  89. 管理后台暂无OEM
  90. # OEM的Lang文件
  91. oem_input = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
  92. oem + '/locales/extras/' + lang + '.json'
  93. # OEM输出文件地址
  94. oem_output = output_folder + '/jsons/' + oem + '/' + lang + '.json'
  95. # 读取OEM的JSON文件
  96. oem_data = read_from_json(oem_input)
  97. # 更新 common_data
  98. modify_json_values(oem_data, modify_func, extra_data=excel_dic)
  99. # 另存为OEM的json文件
  100. save_to_json(oem_output, oem_data)
  101. print("%s %s json save success" % (oem, lang))
  102. # 通用JSON:oem中存在的Key,则不更新common的值
  103. oem_dic = generate_dict_from_json(oem_input)
  104. '''
  105. # 通用的Lang文件
  106. common_input = 'C:/Workspaces/Code_Git/MTP2.0_NEWMANAGE_WEB/public/locales/' + lang + '.json'
  107. # 输出文件地址
  108. common_output = output_folder + '/jsons/' + lang + '.json'
  109. # 读取JSON文件
  110. common_data = read_from_json(common_input)
  111. # 更新 common_data
  112. # modify_json_values(common_data, modify_func, extra_data=excel_dic, exclude_data=oem_dic)
  113. modify_json_values(common_data, modify_func, extra_data=excel_dic, exclude_data=None)
  114. # 另存为json文件
  115. save_to_json(common_output, common_data)
  116. print("%s json save success" % lang)
  117. if __name__ == '__main__':
  118. oem = 'newmanage'
  119. dic_cn, dic_en, dic_th, dic_tw = read_from_excel(oem)
  120. generate_json(oem, dic_cn, 'zh-CN')
  121. generate_json(oem, dic_en, 'en-US')
  122. generate_json(oem, dic_th, 'th-TH')
  123. generate_json(oem, dic_tw, 'zh-TW')