deng.yinping 11 месяцев назад
Родитель
Сommit
1453594568
10 измененных файлов с 332 добавлено и 586 удалено
  1. 3 0
      .gitignore
  2. 0 134
      generate_excle.py
  3. 0 150
      generate_json.py
  4. 45 0
      main.py
  5. 0 143
      manage_generate_excel.py
  6. 0 155
      manage_generate_json.py
  7. BIN
      output_manage/excels/newmanage.xlsx
  8. 0 4
      readme.md
  9. 251 0
      src/common.py
  10. 33 0
      src/tools.py

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+__pycache__/common.cpython-311.pyc
+src/__pycache__/common.cpython-311.pyc
+src/__pycache__/tools.cpython-311.pyc

+ 0 - 134
generate_excle.py

@@ -1,134 +0,0 @@
-import json
-import pandas as pd
-from openpyxl import load_workbook
-from openpyxl.styles import PatternFill
-
-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
-
-
-def read_data_from_json(oem, lang):
-    # C:\Workspaces\Code_Git\MTP20_WEB_GLOBAL\public\locales\zh-CN.json
-    common_file = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/public/locales/' + lang + '.json'
-    common_data = generate_dict_from_json(common_file)
-
-    # C:\Workspaces\Code_Git\MTP20_WEB_GLOBAL\oem\tss\locales\extras\zh-CN.json
-    oem_file = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
-        oem + '/locales/extras/' + lang + '.json'
-    oem_data = generate_dict_from_json(oem_file)
-
-    if oem_data:
-        for key, value in oem_data.items():
-            # 更新通用字典的oem个性化值(有则更新,无则添加)
-            common_data[key] = value
-
-    return common_data
-
-
-def generate_excle_by_oem(oem):
-    zh_data = read_data_from_json(oem, 'zh-CN')
-    en_data = read_data_from_json(oem, 'en-US')
-    th_data = read_data_from_json(oem, 'th-TH')
-    tw_data = read_data_from_json(oem, 'zh-TW')
-
-    df = pd.DataFrame.from_dict(zh_data, orient='index', columns=['zh-CN'])
-
-    # 将 其它 的值合并到 DataFrame 中
-    df['en-US'] = df.index.map(en_data).fillna('')
-    df['th-TH'] = df.index.map(th_data).fillna('')
-    df['zh-TW'] = df.index.map(tw_data).fillna('')
-
-    # 重置索引,以便将索引变为一列
-    df.reset_index(inplace=True)
-    df.rename(columns={'index': 'Key'}, inplace=True)  # 将索引列重命名为 'Key'
-
-    # 导出到 Excel 文件
-    output_file = 'output/excels/' + oem + '.xlsx'
-    df.to_excel(output_file, index=False)
-
-    # 使用 openpyxl 设置列宽
-    wb = load_workbook(output_file)
-    ws = wb.active
-
-    # 指定列宽
-    ws.column_dimensions['A'].width = 30  # 设置第一列宽度
-    ws.column_dimensions['B'].width = 50  # 设置第二列宽度
-    ws.column_dimensions['C'].width = 50  # 设置第三列宽度
-    ws.column_dimensions['D'].width = 50  # 设置第四列宽度
-    ws.column_dimensions['E'].width = 50  # 设置第五列宽度
-
-    # 保存更改
-    wb.save(output_file)
-
-    print("字典已成功输出到 " + output_file)
-
-def update_excel_by_oleexcel(oem, ole_excel):
-    ''' 增量更新,在生成excel里标注新增的key为黄色 '''
-    # 读取输出的Excel文件, 格式:key(A)	zh-CN(B)	en-US(C)	th-TH(C)    zh-TW(D)
-    ole_file = "output/excels/" + ole_excel + ".xlsx"
-    df = pd.read_excel(ole_file)
-    # 将 A 列和 C 列转换为zh-CN字典
-    old_dic = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
-    
-    new_file = "output/excels/" + oem + ".xlsx"
-    wb = load_workbook(new_file)
-    sheet = wb.active  # 选择活动工作表
-
-    # 定义黄色填充样式
-    yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
-
-    # 遍历第一列
-    for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1):
-        cell = row[0]
-        if cell.value not in old_dic:
-            cell.fill = yellow_fill
-
-    # 保存文件
-    update_file = "output/excels/" + oem + "_inc.xlsx"
-    wb.save(update_file)  # 保存为新文件,避免覆盖原文件
-    
-    print("增量字典已成功输出到 " + update_file)
-
-if __name__ == '__main__':
-    # 根据代码json地址生成excel
-    generate_excle_by_oem('tss')
-    
-    # 增量更新,在生成excel里标注新增的key为黄色
-    update_excel_by_oleexcel('tss', '20241127_tss_inc')

+ 0 - 150
generate_json.py

@@ -1,150 +0,0 @@
-
-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')

+ 45 - 0
main.py

@@ -0,0 +1,45 @@
+
+from src.common import CommonUti
+from src.tools import Tools
+
+output_folder_pc="output"
+json_folder_pc="C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/public/locales/"
+oem_json_folder_pc = "C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/"
+
+output_folder_manage="output_manage"
+json_folder_manage="C:/Workspaces/Code_Git/MTP2.0_NEWMANAGE_WEB\public/locales/"
+oem_json_folder_manage = "C:/Workspaces/Code_Git/MTP2.0_NEWMANAGE_WEB/oem/"
+      
+if __name__ == "__main__":
+    while True:    
+        platform = input("请选择平台 (pc, manage, exit): ")
+        
+        if platform == "exit":
+            break
+        
+        type = input("请选择生成类型 (excel, json): ")
+        if platform == "pc":
+            oem = "tss"
+            inc_file = "20241127_tss_inc"
+            if type == "excel":
+                # 生成交易前端excel及增量
+                Tools.generate_excel(oem, inc_file, output_folder_pc, json_folder_pc, oem_json_folder_pc)
+            elif type == "json":
+                # 生成交易前端json及增量
+                Tools.generate_json(oem, output_folder_pc, json_folder_pc, oem_json_folder_pc)
+        elif platform == "manage":
+            oem = "newmanage"
+            inc_file = "20241201_newmanage_inc"
+            if type == "excel":
+               # 生成新管理后台excel及增量
+               Tools.generate_excel(oem, inc_file, output_folder_manage, json_folder_manage, oem_json_folder_manage)
+            elif type == "json":
+                # 生成新管理后台json
+                Tools.generate_json(oem, output_folder_manage, json_folder_manage, oem_json_folder_manage)
+        else:
+            print("输入错误,请重新输入...")
+        
+        print("==============================")
+
+     
+   

+ 0 - 143
manage_generate_excel.py

@@ -1,143 +0,0 @@
-import json
-import pandas as pd
-from openpyxl import load_workbook
-from openpyxl.styles import PatternFill
-import os
-
-output_folder="output_manage"
-
-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
-
-
-def read_data_from_json(oem, lang):
-    # C:\Workspaces\Code_Git\MTP2.0_NEWMANAGE_WEB\public\locales\zh-CN.json
-    common_file = 'C:/Workspaces/Code_Git/MTP2.0_NEWMANAGE_WEB\public/locales/' + lang + '.json'
-    common_data = generate_dict_from_json(common_file)
-
-    '''
-    管理后台暂无OEM 
-    # C:\Workspaces\Code_Git\MTP2.0_NEWMANAGE_WEB\oem\tss\locales\extras\zh-CN.json
-    oem_file = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
-        oem + '/locales/extras/' + lang + '.json'
-    oem_data = generate_dict_from_json(oem_file)
-
-    if oem_data:
-        for key, value in oem_data.items():
-            # 更新通用字典的oem个性化值(有则更新,无则添加)
-            common_data[key] = value
-    '''
-    
-    return common_data
-
-
-def generate_excle_by_oem(oem):
-    zh_data = read_data_from_json(oem, 'zh-CN')
-    en_data = read_data_from_json(oem, 'en-US')
-    th_data = read_data_from_json(oem, 'th-TH')
-    tw_data = read_data_from_json(oem, 'zh-TW')
-
-    df = pd.DataFrame.from_dict(zh_data, orient='index', columns=['zh-CN'])
-
-    # 将 其它 的值合并到 DataFrame 中
-    df['en-US'] = df.index.map(en_data).fillna('')
-    df['th-TH'] = df.index.map(th_data).fillna('')
-    df['zh-TW'] = df.index.map(tw_data).fillna('')
-
-    # 重置索引,以便将索引变为一列
-    df.reset_index(inplace=True)
-    df.rename(columns={'index': 'Key'}, inplace=True)  # 将索引列重命名为 'Key'
-
-    # 导出到 Excel 文件
-    output_file = output_folder + '/excels/' + oem + '.xlsx'
-    df.to_excel(output_file, index=False)
-
-    # 使用 openpyxl 设置列宽
-    wb = load_workbook(output_file)
-    ws = wb.active
-
-    # 指定列宽
-    ws.column_dimensions['A'].width = 60  # 设置第一列宽度
-    ws.column_dimensions['B'].width = 50  # 设置第二列宽度
-    ws.column_dimensions['C'].width = 50  # 设置第三列宽度
-    ws.column_dimensions['D'].width = 50  # 设置第四列宽度
-    ws.column_dimensions['E'].width = 50  # 设置第五列宽度
-
-    # 保存更改
-    wb.save(output_file)
-
-    print("字典已成功输出到 " + output_file)
-
-def update_excel_by_inc(oem, inc_name):
-    ''' 增量更新,在生成excel里标注新增的key为黄色 '''
-    # 读取输出的Excel文件, 格式:key(A)	zh-CN(B)	en-US(C)	th-TH(C)    zh-TW(D)
-    inc_file = output_folder + "/excels/" + inc_name + ".xlsx"
-    if not os.path.exists(inc_file):
-        return
-        
-    df = pd.read_excel(inc_file)
-    # 将 A 列和 C 列转换为zh-CN字典
-    old_dic = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
-    
-    new_file = output_folder + "/excels/" + oem + ".xlsx"
-    wb = load_workbook(new_file)
-    sheet = wb.active  #  
-
-    # 定义黄色填充样式
-    yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
-
-    # 遍历第一列
-    for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1):
-        cell = row[0]
-        if cell.value not in old_dic:
-            cell.fill = yellow_fill
-
-    # 保存文件
-    update_file = output_folder + "/excels/" + oem + "_inc.xlsx"
-    wb.save(update_file)  # 保存为新文件,避免覆盖原文件
-    
-    print("增量字典已成功输出到 " + update_file)
-
-if __name__ == '__main__':
-    # 根据代码json地址生成excel
-    generate_excle_by_oem('newmanage')
-    
-    # 增量更新,在生成excel里标注新增的key为黄色
-    update_excel_by_inc('newmanage', '20241201_newmanage_inc')

+ 0 - 155
manage_generate_json.py

@@ -1,155 +0,0 @@
-
-import json
-import os
-import pandas as pd
-
-output_folder="output_manage"
-
-def read_from_excel(oem):
-    # 读取输出的Excel文件, 格式:key(A)	zh-CN(B)	en-US(C)	th-TH(C)    zh-TW(D)
-    excel_filename = output_folder + "/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 
-    # OEM的Lang文件
-    oem_input = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
-        oem + '/locales/extras/' + lang + '.json'
-    # OEM输出文件地址
-    oem_output = output_folder + '/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/MTP2.0_NEWMANAGE_WEB/public/locales/' + lang + '.json'
-    # 输出文件地址
-    common_output = output_folder + '/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)
-    modify_json_values(common_data, modify_func, extra_data=excel_dic, exclude_data=None)
-    
-    # 另存为json文件
-    save_to_json(common_output, common_data)
-    print("%s json save success" % lang)
-
-
-if __name__ == '__main__':
-    oem = 'newmanage'
-    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')

BIN
output_manage/excels/newmanage.xlsx


+ 0 - 4
readme.md

@@ -1,6 +1,4 @@
 # 交易前端构建
-文件:generate_excel.py \ generate_json.py
-
 MTP20_WEB_GLOBAL 的 多语言工具,根据JSON生成excel, 翻译后根据excel生成json
 
 在py文件中修改国际化JSON文件地址:
@@ -10,8 +8,6 @@ oem个性化JOSN文件地址
 C:\Workspaces\Code_Git\MTP20_WEB_GLOBAL\oem\tss\locales\extras
 
 # 新管理后台构建
-文件:manage_generate_excel.py \ manage_generate_json.py
-
 在py文件中修改国际化JSON文件地址:
 C:\Workspaces\Code_Git\MTP2.0_NEWMANAGE_WEB\public\locales
 

+ 251 - 0
src/common.py

@@ -0,0 +1,251 @@
+'''
+Author: deng.yinping deng.yinping@muchinfo.cn
+Date: 2024-12-06 10:22:52
+LastEditors: deng.yinping deng.yinping@muchinfo.cn
+LastEditTime: 2024-12-06 14:01:17
+FilePath: \MTP20_WEB_GLOBAL_i18n_Tool\common.py
+Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
+'''
+import json
+import pandas as pd
+from openpyxl import load_workbook
+from openpyxl.styles import PatternFill
+import os
+
+
+class CommonUti:
+
+    @staticmethod
+    def read_from_json(file_path):
+        # 从JSON文件中读取内容
+        with open(file_path, 'r', encoding='utf-8') as file:
+            data = json.load(file)
+
+        return data
+
+    @staticmethod
+    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
+                CommonUti.get_leaf_paths(value, current_path, result)
+        elif isinstance(data, list):
+            # 如果是列表,遍历每个元素
+            for index, item in enumerate(data):
+                current_path = f"{path}[{index}]"
+                CommonUti.get_leaf_paths(item, current_path, result)
+        else:
+            # 如果是终节点,将当前路径和值保存到字典中
+            if path not in ignore_data:
+                result[path] = data
+
+        return result
+
+    @staticmethod
+    def generate_dict_from_json(file_path):
+        data = CommonUti.read_from_json(file_path)
+        # 生成全路径的字典
+        leaf_paths_dict = CommonUti.get_leaf_paths(data)
+        return leaf_paths_dict
+
+    @staticmethod
+    def generate_excle_by_oem(oem, output_folder, zh_data, en_data, th_data, tw_data):
+        df = pd.DataFrame.from_dict(zh_data, orient='index', columns=['zh-CN'])
+
+        # 将 其它 的值合并到 DataFrame 中
+        df['en-US'] = df.index.map(en_data).fillna('')
+        df['th-TH'] = df.index.map(th_data).fillna('')
+        df['zh-TW'] = df.index.map(tw_data).fillna('')
+
+        # 重置索引,以便将索引变为一列
+        df.reset_index(inplace=True)
+        df.rename(columns={'index': 'Key'}, inplace=True)  # 将索引列重命名为 'Key'
+
+        # 导出到 Excel 文件
+        output_file = output_folder + '/excels/' + oem + '.xlsx'
+        df.to_excel(output_file, index=False)
+
+        # 使用 openpyxl 设置列宽
+        wb = load_workbook(output_file)
+        ws = wb.active
+
+        # 指定列宽
+        ws.column_dimensions['A'].width = 60  # 设置第一列宽度
+        ws.column_dimensions['B'].width = 50  # 设置第二列宽度
+        ws.column_dimensions['C'].width = 50  # 设置第三列宽度
+        ws.column_dimensions['D'].width = 50  # 设置第四列宽度
+        ws.column_dimensions['E'].width = 50  # 设置第五列宽度
+
+        # 保存更改
+        wb.save(output_file)
+
+        print("字典已成功输出到 " + output_file)
+
+    @staticmethod
+    def update_excel_by_inc(oem, output_folder, inc_name):
+        ''' 增量更新,在生成excel里标注新增的key为黄色 '''
+        # 读取输出的Excel文件, 格式:key(A)	zh-CN(B)	en-US(C)	th-TH(C)    zh-TW(D)
+        inc_file = output_folder + "/excels/" + inc_name + ".xlsx"
+        if not os.path.exists(inc_file):
+            return
+
+        df = pd.read_excel(inc_file)
+        # 将 A 列和 C 列转换为zh-CN字典
+        old_dic = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
+
+        new_file = output_folder + "/excels/" + oem + ".xlsx"
+        wb = load_workbook(new_file)
+        sheet = wb.active
+
+        # 定义黄色填充样式
+        yellow_fill = PatternFill(
+            start_color="FFFF00", end_color="FFFF00", fill_type="solid")
+
+        # 遍历第一列
+        for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1):
+            cell = row[0]
+            if cell.value not in old_dic:
+                cell.fill = yellow_fill
+
+        # 保存文件
+        update_file = output_folder + "/excels/" + oem + "_inc.xlsx"
+        wb.save(update_file)  # 保存为新文件,避免覆盖原文件
+
+        print("增量字典已成功输出到 " + update_file)
+
+    @staticmethod
+    def generate_excle(oem, output_folder, json_folder, oem_json_folder):
+        # 据JSON生成excel文件
+        zh_data = CommonUti.read_data_from_json(
+            oem, json_folder, oem_json_folder, 'zh-CN')
+        en_data = CommonUti.read_data_from_json(
+            oem, json_folder, oem_json_folder, 'en-US')
+        th_data = CommonUti.read_data_from_json(
+            oem, json_folder, oem_json_folder, 'th-TH')
+        tw_data = CommonUti.read_data_from_json(
+            oem, json_folder, oem_json_folder, 'zh-TW')
+
+        CommonUti.generate_excle_by_oem(
+            oem, output_folder, zh_data, en_data, th_data, tw_data)
+
+    @staticmethod
+    def read_data_from_json(oem, json_folder, oem_json_folder, lang):
+        common_file = json_folder + lang + '.json'
+        common_data = CommonUti.generate_dict_from_json(common_file)
+
+        # 处理OEM数据
+        oem_file = oem_json_folder + oem + '/locales/extras/' + lang + '.json'
+        if os.path.exists(oem_file):
+            oem_data = CommonUti.generate_dict_from_json(oem_file)
+
+            if oem_data:
+                for key, value in oem_data.items():
+                    # 更新通用字典的oem个性化值(有则更新,无则添加)
+                    common_data[key] = value
+
+        return common_data
+
+    @staticmethod
+    def read_from_excel(oem, output_folder):
+        # 读取输出的Excel文件, 格式:key(A)	zh-CN(B)	en-US(C)	th-TH(C)    zh-TW(D)
+        excel_filename = output_folder + "/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
+
+    @staticmethod
+    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)):  # 如果值是字典或列表,递归调用
+                    CommonUti.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)):
+                    CommonUti.modify_json_values(item, modify_func,
+                                                 new_path, extra_data, exclude_data)
+                else:
+                    # 修改列表中的值,并传递完整路径
+                    data[index] = modify_func(item, new_path, extra_data)
+
+    @staticmethod
+    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 中,保持原值
+
+    @staticmethod
+    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)
+
+    @staticmethod
+    def generate_json(oem, excel_dic, lang, output_folder, json_folder, oem_json_folder):
+        # OEM的Lang文件
+        oem_input = oem_json_folder + oem + '/locales/extras/' + lang + '.json'
+        # OEM输出文件地址
+        oem_output = output_folder + '/jsons/' + oem + '/' + lang + '.json'
+        oem_dic = None
+        if os.path.exists(oem_input):
+            # 读取OEM的JSON文件
+            oem_data = CommonUti.read_from_json(oem_input)
+            # 更新 common_data
+            CommonUti.modify_json_values(
+                oem_data, CommonUti.modify_func, extra_data=excel_dic)
+            # 另存为OEM的json文件
+            CommonUti.save_to_json(oem_output, oem_data)
+            print("%s %s json save success" % (oem, lang))
+
+            # 通用JSON:oem中存在的Key,则不更新common的值
+            oem_dic = CommonUti.generate_dict_from_json(oem_input)
+
+        # 通用的Lang文件
+        common_input = json_folder + lang + '.json'
+        # 输出文件地址
+        common_output = output_folder + '/jsons/' + lang + '.json'
+        # 读取JSON文件
+        common_data = CommonUti.read_from_json(common_input)
+        # 更新 common_data
+        CommonUti.modify_json_values(common_data, CommonUti.modify_func,
+                                     extra_data=excel_dic, exclude_data=oem_dic)
+        # 另存为json文件
+        CommonUti.save_to_json(common_output, common_data)
+        print("%s json save success" % lang)

+ 33 - 0
src/tools.py

@@ -0,0 +1,33 @@
+'''
+Author: deng.yinping deng.yinping@muchinfo.cn
+Date: 2024-12-06 14:17:53
+LastEditors: deng.yinping deng.yinping@muchinfo.cn
+LastEditTime: 2024-12-06 14:26:22
+FilePath: \MTP20_WEB_GLOBAL_i18n_Tool\src\tools.py
+Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
+'''
+
+from src.common import CommonUti
+
+
+class Tools:
+        
+    @staticmethod
+    def generate_excel(oem, inc_file, output_folder, json_folder, oem_json_folder):
+        '''
+        根据JSON生成excel文件
+        '''
+        CommonUti.generate_excle(oem, output_folder, json_folder, oem_json_folder)
+        # 增量更新,在生成excel里标注新增的key为黄色
+        CommonUti.update_excel_by_inc(oem, output_folder, inc_file)
+        
+    
+    def generate_json(oem, output_folder, json_folder, oem_json_folder):
+        '''
+        根据excel生成JSON文件
+        '''
+        dic_cn, dic_en, dic_th, dic_tw = CommonUti.read_from_excel(oem, output_folder)
+        CommonUti.generate_json(oem, dic_cn, 'zh-CN', output_folder, json_folder, oem_json_folder)
+        CommonUti.generate_json(oem, dic_en, 'en-US', output_folder, json_folder, oem_json_folder)
+        CommonUti.generate_json(oem, dic_th, 'th-TH', output_folder, json_folder, oem_json_folder)
+        CommonUti.generate_json(oem, dic_tw, 'zh-TW', output_folder, json_folder, oem_json_folder)