You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
9.5 KiB

1 month ago
# -*- coding: UTF-8 -*-
import math
import xlrd
import os
import shutil
import re
import sys
import json
INT_KEY_TYPE = "int_key"
UINT_KEY_TYPE = "uint_key"
STRING_KEY_TYPE = "string_key"
INT_TYPE = "int"
FLOAT_TYPE = "float"
UINI_TYPE = "uint"
STRING_TYPE = "string"
STRING_LAN_TYPE = "string_lan"
STRING_LIST_TYPE = "string[]"
INT_LSIT_TYPE = "int[]"
UINT_LSIT_TYPE = "uint[]"
LONG_LSIT_TYPE = "long[]"
LONG_TYPE = "long"
NONE_TYPE = "none"
LUA_WORD = "(LUA)"
excel_dir = "./Excel"
lang_excel_file = "LanguageDesc.xlsx"
lang_params_excel_file = "LanguageParams.xlsx"
generate_lang_dir = "../../client/client/Assets/RawResources/Language"
def get_cell_value(value_type, value):
if value is None or value == "":
return ""
elif value_type == INT_KEY_TYPE or value_type == INT_TYPE or value_type == UINT_KEY_TYPE or value_type == UINI_TYPE or value_type == LONG_TYPE:
return int(value)
elif value_type.startswith(INT_LSIT_TYPE):
# if type(value) is str or type(value) is str:
# value = value.encode("utf8")
value = str(value)
value = value.split(',')
value = [int(float(x)) for x in value]
return value
elif value_type.startswith(UINT_LSIT_TYPE) or value_type.startswith(LONG_LSIT_TYPE):
# if type(value) is str or type(value) is str:
# value = value.encode("utf8")
value = str(value)
value = value.split(',')
value = [int(float(x)) for x in value]
return value
elif value_type == FLOAT_TYPE:
value = float(value)
fractional_part,_ = math.modf(value)
if fractional_part < 0.000001 :
value = int(value)
return value
elif value_type.startswith(STRING_LIST_TYPE):
# if type(value) is str or type(value) is str:
# value = value.encode("utf8")
value = str(value)
value = value.split(',')
return value
elif value_type == STRING_KEY_TYPE or value_type == STRING_TYPE or value_type == STRING_LAN_TYPE:
# if type(value) is str or type(value) is str:
# value = value.encode("utf8")
value = str(value)
#语言表
value = value.replace("\n", "\\n")
# value = value.replace("\"", "\\\"")
value = value.replace("<String>", "%s")
value = value.replace("<Number>", "%s")
value = value.replace("<string>", "%s")
value = value.replace("<number>", "%s")
return value
# elif value_type == STRING_LAN_TYPE:
# value = value.encode("utf8")
# lang_key = str(value).replace("#", "")
# return LUA_WORD+"GM and GM.Lang(\"%s\") or \"%s\"" % (lang_key, lang_key)
return None
def remove_blank(key,text):
if key.startswith("HeroStory"):
lines = text.split("^")
if len(lines) > 1:
new_lines=[]
for line in lines:
match = re.search(r"\S",line)
if match :
index = match.start()
new_lines.append(line[index:])
else:
new_lines.append(line)
return "^".join(new_lines)
else:
return text
else:
return text
def load_lang_params(excel_path):
if os.path.exists(excel_path):
xml_data = xlrd.open_workbook(excel_path) # 打开xls文件
sheet = xml_data.sheets()[0] # 打开第一张表
# 先判断key的数量
row_num = sheet.nrows # 获取表的行数
col_num = sheet.ncols # 获取表的列数
row_type_values = sheet.row_values(1) # 获取表的第二行的所有数据
row_cs_values = sheet.row_values(2) # 获取表的第三行的所有数据
index_list = [] # 需要导出的列索引
key_col = -1
for i in range(0, col_num):
type_value = row_type_values[i]
cs_value = row_cs_values[i]
if type_value == STRING_KEY_TYPE:
key_col = i
elif type_value == STRING_TYPE and cs_value != "skip" and cs_value.find("c") != -1:
index_list.append(i)
lang_param_dic = {}
for row in range(4, row_num):
row_values = sheet.row_values(row)
key = get_cell_value(STRING_TYPE, row_values[key_col])
lang_param_dic[key] = []
for index in index_list:
row_value = row_values[index]
param_text = None
if type(row_value) == float:
param_text = get_cell_value(FLOAT_TYPE, row_value)
else:
param_text = get_cell_value(STRING_TYPE, row_value)
lang_param_dic[key].append(param_text)
return lang_param_dic
else:
return {}
def combine_param_to_lang_text(lang_type,lang_key,lang_text,params):
signs = ["<p1>","<p2>","<p3>","<p4>","<p5>","<p6>","<p7>","<p8>","<p9>","<p10>",
"<p11>","<p12>","<p13>","<p14>","<p15>","<p16>","<p17>","<p18>","<p19>","<p20>",]
combine_count = 0
for i in range(0,len(signs)):
if lang_text.find(signs[i])!=-1:
if params is None:
print(f"语言表配置错误!多语言key[{lang_key}]的[{lang_type}]语种的文本内容有参数化通配符,但是LanguageParams.xlsx表中没有对应的参数行配置!")
return lang_text
if params[i] is None or params[i] == "":
print(f"语言表配置错误!多语言key[{lang_key}]的[{lang_type}]语种的文本内容的第[{i+1}]个参数在LanguageParams.xlsx表中配置为空!")
return lang_text
lang_text = lang_text.replace(signs[i],str(params[i]))
return lang_text
def excel_lang_to_text(excel_path, lang_params_path, lang_dir):
xml_data = xlrd.open_workbook(excel_path) # 打开xls文件
sheet = xml_data.sheets()[0] # 打开第一张表
# 先判断key的数量
row_num = sheet.nrows # 获取表的行数
col_num = sheet.ncols # 获取表的列数
row_lang_values = sheet.row_values(0) # 获取表的第一行的所有数据
row_type_values = sheet.row_values(1) # 获取表的第二行的所有数据
row_cs_values = sheet.row_values(2) # 获取表的第三行的所有数据
row_desc_values = sheet.row_values(3) # 获取表的第四行的所有数据
index_list = [] # 需要导出的列索引
lang_list = [] # key的列表
keyCol = -1
for i in range(0, col_num):
lang_value = row_lang_values[i]
type_value = row_type_values[i]
cs_value = row_cs_values[i]
if type_value == STRING_KEY_TYPE:
keyCol = i
elif type_value == STRING_TYPE and cs_value != "skip" and cs_value.find("c") != -1:
index_list.append(i)
lang_list.append(lang_value.lower())
keyList=[]
table = {}
lang_param_dict = load_lang_params(lang_params_path)
for row in range(4, row_num):
row_values = sheet.row_values(row)
key = get_cell_value(STRING_TYPE, row_values[keyCol])
keyList.append(key)
table[key]=[]
for index in index_list:
langText = get_cell_value(STRING_TYPE, row_values[index])
table[key].append(langText)
for index in range(len(lang_list)):
lang = lang_list[index]
file_name = lang + ".txt"
file_path = os.path.join(lang_dir, file_name)
f = open(file_path, "w", encoding="UTF-8",newline="\n")
for key in keyList:
text = table[key][index]
params = lang_param_dict.get(key)
text = combine_param_to_lang_text(lang,key,text,params)
text = remove_blank(key,text)
f.write(key+"="+text+"\n")
f.close()
if __name__ == "__main__":
lang_excel_file_path = excel_dir + os.sep + lang_excel_file
lang_params_excel_file_path = excel_dir + os.sep + lang_params_excel_file
if not os.path.exists(lang_excel_file_path):
print("error! Language excel not exists.", lang_excel_file_path)
sys.exit()
if not os.path.exists(generate_lang_dir):
os.makedirs(generate_lang_dir)
for file_name in os.listdir(generate_lang_dir):
if not file_name.endswith(".meta") and not file_name.endswith(".svn"):
file_path = os.path.join(generate_lang_dir, file_name)
os.remove(file_path)
excel_lang_to_text(lang_excel_file_path, lang_params_excel_file_path, generate_lang_dir)
change_region_dir="" #需要替换的区域目录
if (len(sys.argv) > 2 and sys.argv[1] == "region" ):
change_region_dir=sys.argv[2] #需要替换的区域目录
#区域目录xls文件覆盖,重新生成lua文件
if change_region_dir != "" :
region_file_path = excel_dir + os.sep + change_region_dir
if not os.path.exists(region_file_path):
print(region_file_path+" 目录不存在,没进行替换")
exit(-1)
lang_excel_file_path = region_file_path + os.sep + lang_excel_file
if not os.path.exists(lang_excel_file_path):
print(lang_excel_file_path+" 文件不存在,不需要替换")
exit(0)
print(lang_excel_file_path+" 文件进行覆盖处理")
tmp_lang_params_excel_file_path = region_file_path + os.sep + lang_params_excel_file
if not os.path.exists(tmp_lang_params_excel_file_path): #区域langugeParam不存在就拿主目录的
tmp_lang_params_excel_file_path = lang_params_excel_file_path
excel_lang_to_text(lang_excel_file_path, tmp_lang_params_excel_file_path, generate_lang_dir)