generate_json.py 5.6 KB

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