#-*-coding:utf-8-*-ImportOSImportOs.pathImportReImportsysreload (SYS) sys.setdefaultencoding ("Utf-8") Root_path= OS.GETCWD () +Os.seplist_name=""str_list= []#Full pathdefcheck_file (file): DiC=os.path.splitext (file) File_n=os.path.split (file)ifDIC[1] = =". Lua" andFILE_N[1]! ="Language.lua": returnTruereturnFalsedefis_file (path):returnos.path.isfile (path)defreplace (match):ifRe.search (U'[\u4e00-\u9fa5]+', Match.group (0))! =None:GlobalList_nameifMatch.group (0)inchstr_list:return "Util"+ list_name+". Str"+Str (str_list.index (match.group (0)))Else: Str_list.append (match.group (0))return "Util"+ list_name+". Str"+str (Len (str_list)-1) Else: returnmatch.group (0)defReplace_china (path): Lua_file=Open (path) file_content=Lua_file.read ()#the name of Tabel in Lua Globallist_name, Str_list str_list=[] List_name= Os.path.split (path) [1][:-4].upper ()Try: Utf_content= File_content.decode ("UTF8") except: PrintPathreturnRe_str=u'"(.*?)"'pattern=Re.compile (RE_STR) Results=pattern.sub (replace, utf_content)ifResults! =utf_content:#Write Filewrite_file (path, results) Create_tabel (path)defwrite_file (Path, replace_str): Lua_file= Open (Path,"W") Lua_file.write (REPLACE_STR) lua_file.close ()#create a tabel in LuadefCreate_tabel (path):GlobalList_name Lua_tabel= Open ("Common\utils\language.lua","a") Lua_tabel.write ("\ n") Lua_tabel.write ("Util"+ List_name +"= {\ n") forIndex, CHINA_STRinchEnumerate (str_list): Lua_tabel.write ("Str"+ STR (index) +" = "+ China_str +", \ n") Lua_tabel.write ("}") Lua_tabel.close ()defWalk_dir (): forRoot, Dir_names, file_namesinchOs.walk (root_path): forfile_nameinchFile_names:full_path=Os.path.join (Root, file_name)ifIs_file (full_path):ifCheck_file (Full_path): Replace_china (Full_path)Else: Walk_dir (Full_path)if __name__=="__main__": Walk_dir ()#Replace_china (sys.argv[1])
Python script automatically extracts and replaces Chinese in code