98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
import os
|
|
import sys
|
|
import tempfile
|
|
import subprocess
|
|
|
|
def install_missing_packages():
|
|
"""安装缺失的Python包"""
|
|
try:
|
|
import pandas
|
|
import openpyxl
|
|
except ImportError as e:
|
|
print(f"缺少必要的Python包: {e.name},正在尝试安装...")
|
|
subprocess.check_call([sys.executable, "-m", "pip", "install", "pandas", "openpyxl"])
|
|
|
|
def main():
|
|
import pandas
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
file_path = os.path.join(script_dir, 'language.xlsx')
|
|
|
|
try:
|
|
df = pandas.read_excel(file_path, index_col='STRING_ID')
|
|
except FileNotFoundError:
|
|
print(f"File not found: {file_path}")
|
|
return
|
|
except Exception as e:
|
|
print(f"Error reading Excel file: {e}")
|
|
return
|
|
|
|
languages = df.columns.tolist()
|
|
en_column = languages[0]
|
|
|
|
header_file_path = os.path.join(script_dir, 'lang_define.h')
|
|
write_if_changed(header_file_path, lambda f: write_header_file(f, df.index, languages))
|
|
|
|
generated_c_files = 0
|
|
max_lang_id_len = max(len(str(lang_id)) for lang_id in df.index)
|
|
for lang in languages:
|
|
c_file_path = os.path.join(script_dir, f'{lang}.c')
|
|
write_if_changed(c_file_path, lambda f: write_c_file(f, df, lang, en_column, max_lang_id_len))
|
|
generated_c_files += 1
|
|
# print(f"{c_file_path} generated complete")
|
|
|
|
print(f"Generated Language files total: {generated_c_files}")
|
|
print("Translation generated successfully...")
|
|
|
|
def write_if_changed(file_path, write_func):
|
|
with tempfile.NamedTemporaryFile(delete=False, mode='w', encoding='utf-8') as temp_file:
|
|
write_func(temp_file)
|
|
temp_file.flush()
|
|
|
|
with open(temp_file.name, 'r', encoding='utf-8') as temp_file_read:
|
|
temp_content = temp_file_read.read()
|
|
|
|
orig_content = None
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
orig_content = file.read()
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
if orig_content != temp_content:
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.write(temp_content)
|
|
|
|
os.remove(temp_file.name)
|
|
|
|
def write_header_file(f, indices, languages):
|
|
f.write('#ifndef __LANG_H__\n')
|
|
f.write('#define __LANG_H__\n\n')
|
|
f.write('/* STRING ENUMERATION DEFINITION */\n')
|
|
f.write('typedef enum {\n')
|
|
for idx, lang_id in enumerate(indices):
|
|
f.write(f' {lang_id},')
|
|
f.write('\n')
|
|
f.write('\n STR_MAX\n')
|
|
f.write('} LANG_STRING_ID_E;\n\n')
|
|
|
|
f.write('/* MULTILINGUAL TRANSLATION EXTERN */\n')
|
|
for lang in languages:
|
|
f.write(f'extern const char * const {lang}[STR_MAX];\n')
|
|
f.write('\n#endif /* __LANG_H__ */\n')
|
|
|
|
def write_c_file(f, df, lang, en_column, max_lang_id_len):
|
|
import pandas
|
|
f.write(f'#include "lang_define.h"\n\n')
|
|
f.write(f'const char * const {lang}[STR_MAX] = {{\n')
|
|
for idx, lang_id in enumerate(df.index):
|
|
translation = df.loc[lang_id, lang]
|
|
if pandas.isna(translation):
|
|
translation = df.loc[lang_id, en_column]
|
|
|
|
padding = ' ' * (max_lang_id_len - len(str(lang_id)) + 1)
|
|
f.write(f' [{lang_id}]{padding} = "{translation}",\n')
|
|
f.write('};\n')
|
|
|
|
if __name__ == "__main__":
|
|
install_missing_packages()
|
|
main() |