相關(guān)國(guó)際化處理:
iOS【語(yǔ)言國(guó)際化處理】python腳本讀取Excel內(nèi)容批量導(dǎo)入國(guó)際化語(yǔ)言文件中(一)
iOS【語(yǔ)言國(guó)際化處理】python腳本將國(guó)際化語(yǔ)言文件批量導(dǎo)入Excel文件中(二)
iOS【圖片國(guó)際化處理】python腳本Assets.xcassets圖片名稱(chēng)及MD5批量處理(三)
接上一個(gè)處理后續(xù):將項(xiàng)目中的國(guó)際化語(yǔ)言批量導(dǎo)入到Excel中
注意文案格式與key中有=號(hào)
1、"key"="abc def"; //注釋
2、"key"="abc=def";
3、"key"="這是一句
話,只是換行了";4、"key=key"="abc def";
1、2、3、處理了,第4種沒(méi)處理,一般調(diào)整key
文件示例目錄
|--------Language---ar.lproj-------------Localizable.strings----|
|---------------------en.lproj------------Localizable.strings----|
|---------------------es.lproj------------Localizable.strings----|
|---------------------Language.xlsx----------------------------|
Excel結(jié)構(gòu)示意圖
Excel第一行內(nèi)容:key | ar.lproj | en.lproj | es.lproj |
Excel后面行內(nèi)容:第一列為Key, 后面列為對(duì)應(yīng)語(yǔ)言的翻譯
[key]-----[ar.lporj]------[en.lproj]-----[es.lproj]
[key1]----[test_ar]------[test_en]-----[test_es]
[key2]----[test2_ar]----[test2_en]----[test2_es]
Localizable.strings結(jié)構(gòu)示意圖
"kTest_1" = "test_ar_1 \n 11";
"kTest_2" = "test_ar_2";
"kTest_3" = "test_ar_3 %@ 33";

文件目錄截圖

翻譯Excel截圖

國(guó)際化處理截圖
Python3, 需要安裝xlrd讀Excel,xlwt寫(xiě)Excel
python里面的xlrd模塊(python3.9)表示對(duì)應(yīng)版本
curl https://bootstrap.pypa.io/get-pip.py | python3.9
pip install xlrd
pip install xlwt
pip install pyexcel-xls
終端使用示例
python3.9 /Users/odd/Documents/Project/測(cè)試國(guó)際化/TestPython/CCLocalizationToExcelTool.py
配置使用:
print("可配置調(diào)整")
# /Users/odd/Documents/Project/測(cè)試國(guó)際化/Language/en.lproj/Localizable.strings
# 國(guó)際化語(yǔ)言文件
languageFile = "/Users/odd/Documents/Project/測(cè)試國(guó)際化/Language"
# 國(guó)際化的語(yǔ)言文件名
languageLocalizableName = "Localizable.strings"
# 存儲(chǔ)excel文件路徑
languageExcefilePath = languageFile + "/Language.xlsx"
完整腳本:CCLocalizationToExcelTool.py
#-*-coding:utf-8-*-
import xlrd,xlwt,sys,os
"""讀取本地國(guó)際化語(yǔ)言文件,導(dǎo)出到Excel表格中的數(shù)據(jù)"""
def read_localizable_to_excel():
print("可配置調(diào)整")
# /Users/odd/Documents/Project/測(cè)試國(guó)際化/Language/en.lproj/Localizable.strings
# 國(guó)際化語(yǔ)言文件
languageFile = "/Users/odd/Documents/Project/測(cè)試國(guó)際化/Language"
# 國(guó)際化的語(yǔ)言文件名
languageLocalizableName = "Localizable.strings"
# 存儲(chǔ)excel文件路徑
languageExcefilePath = languageFile + "/Language.xlsx"
# 獲取語(yǔ)言文件夾下對(duì)應(yīng)的語(yǔ)言文件名集合
dirSubDirNames = dir_first_all_subDirs(languageFile)
if len(dirSubDirNames) == 0:
print("\n錯(cuò)誤>>>>> 該目錄下不存在.lproj文件夾")
return
print("\n語(yǔ)言文件夾里各種語(yǔ)言文件名")
print(dirSubDirNames)
# 創(chuàng)建工作簿
excleWorlk = xlwt.Workbook()
sheet1 = excleWorlk.add_sheet(u'翻譯',cell_overwrite_ok=True) #創(chuàng)建sheet
row0 = [u'key']
row0.extend(dirSubDirNames)
print("\nexcel 第一行數(shù)據(jù):")
print(row0)
print("\n")
for i in range(0,len(row0)):
sheet1.write(0,i,row0[i],set_style('Times New Roman',420,True))
# 如果 i>0 ? 12000 : 6000
sheet1.col(i).width = 12000 if(i>0) else 6000
# 存儲(chǔ)key集合,以及key對(duì)應(yīng)的數(shù)據(jù):kye,value,row
# firstKeyArray = ["key1","key2"]
# firstKeyValueArray = [{"key":"key1","value":"key對(duì)應(yīng)的內(nèi)容","row":"1(寫(xiě)入表中的第幾行)"}]
firstKeyArray = []
firstKeyValueArray = []
# ['ar.lproj', 'en.lproj', '.DS_Store', 'es.lproj']
for subIndex, subDirName in enumerate(dirSubDirNames):
languageLocalizableFilePath = languageFile + "/" + subDirName + "/" + languageLocalizableName
print("翻譯文件序號(hào):" + str(subIndex) +" 路徑:"+languageLocalizableFilePath)
# 讀取國(guó)際化語(yǔ)言
languaeTxtArray = readTxtToArray(languageLocalizableFilePath)
print("\n=====================================")
print("\n開(kāi)始寫(xiě): " + subDirName + " 到表第: " + str(subIndex) + "列")
print("\n=====================================")
if subIndex == 0:
for txtRowIndex, rowDic in enumerate(languaeTxtArray):
txtRowKey = rowDic["key"]
txtRowValue = rowDic["value"]
writeRow = txtRowIndex+1
# 持有已經(jīng)寫(xiě)入的key
firstKeyValueArray.append({"key":txtRowKey,"value":txtRowValue,"row":str(writeRow)})
firstKeyArray.append(txtRowKey)
print("寫(xiě)入列:" + str(subIndex+1) + " 行:" + str(writeRow) + " 值:" + txtRowValue)
# 寫(xiě)入第一列Key
sheet1.write(writeRow,0,txtRowKey)
# 寫(xiě)入Key對(duì)應(yīng)的value
sheet1.write(writeRow,subIndex+1,txtRowValue)
else:
for txtRowIndex, rowDic in enumerate(languaeTxtArray):
txtRowKey = rowDic["key"]
txtRowValue = rowDic["value"]
if txtRowKey in firstKeyArray:
# 取出已有key對(duì)應(yīng)的數(shù)據(jù)
currentDic = dicFilterKeyForArray(firstKeyValueArray,txtRowKey)
# key對(duì)應(yīng)的行
writeRow = int(currentDic["row"])
print(currentDic)
# 寫(xiě)入Key對(duì)應(yīng)的value
sheet1.write(writeRow,subIndex+1,txtRowValue)
print("\n包含 直接寫(xiě)列:" + str(subIndex+1) + " 行:" + str(writeRow) + " 值: " + txtRowValue)
else:
firstKeyArray.append(txtRowKey)
firstKeyValueArray.append({"key":txtRowKey,"value":txtRowValue,"row":str(len(firstKeyArray))})
writeRow = len(firstKeyArray)
print("\n追加到最后:" + str(writeRow))
# 寫(xiě)入第一列Key
sheet1.write(writeRow,0,txtRowKey)
# 寫(xiě)入Key對(duì)應(yīng)的value
sheet1.write(writeRow,subIndex+1,txtRowValue)
print("寫(xiě)入列:" + str(subIndex+1) + " 行:" + str(writeRow) + " 值:" + txtRowValue)
excleWorlk.save(languageExcefilePath) #保存文件
print("\n===========導(dǎo)出文件路徑=========")
print("\n路徑:"+languageExcefilePath)
print("\n===========導(dǎo)出文件路徑=========")
print("\n\n")
# en.lproj 文件名處理
def langFieleName(nameString):
result = ""
#小寫(xiě)處理
nameString = nameString.lower()
#方式一:截取 nameString[-5:] 輸出右5位:lproj
# if len(nameString) > 1 :
# #獲取:en
# result = nameString[0:2]
#方式二:分割
#nameList = nameString.split(".")
#result = nameList[0]
#方式三:查找截取 變量.find("要查找的內(nèi)容"[,開(kāi)始位置,結(jié)束位置])
#lprojIndex = nameString.find('lproj') #3
#result = nameString[0:lprojIndex]
#方法四:替換
result = nameString.replace('.lproj','')
print(result)
return result
def languageFileName(nameString):
#小寫(xiě)處理
result = nameString.lower()
return result
# 判斷目錄是否存在,及創(chuàng)建
def isPathExitsDir(dirs):
if dirs == '':
print("不是文件目錄路徑")
return
if not os.path.exists(dirs):
print("不存在目錄,創(chuàng)建目錄")
os.makedirs(dirs)
# 判斷文件是否存在,及創(chuàng)建
def isPathExitsFile(filePath):
if filePath == '':
print("不是文件路徑")
return
if not os.path.exists(filePath):
#調(diào)用系統(tǒng)命令行來(lái)創(chuàng)建文件
os.system(r"touch {}".format(filePath))
# 讀取iOS國(guó)際化語(yǔ)言文件
"""
行樣式:"name" = "occc";
rowKey = "name"
rowValue = "occc"
txtArray = [{"key":rowKey,"value":rowValue}]
"""
def readTxtToArray(txtPath):
#item.endswith('.mp4')
# line.startswith('#')
#b = True a = bool(1-b)
#bool()函數(shù)中的1-bool值 就是取bool值的反值了。
if not os.path.exists(txtPath):
print("讀取國(guó)際化語(yǔ)言文件不存在:" + txtPath)
return {}
txtArray = []
with open(txtPath,"r") as f:
txtData = f.readlines()
# print("\ntxt內(nèi)容:讀取所以內(nèi)容,并以數(shù)組格式返回,會(huì)讀取到'\\n'")
#print(txtData)
#print("\n")
#['"cancel"="CANCEL11"\n', '"done"="Done222"\n']
tempStr = ''
for line in txtData:
#移除 \\n
line = line.strip('\n')
#print(line)
#判斷以"開(kāi)頭,
print('=====:'+line)
if line.startswith("\""):
#查找先替換最后的//xxx
replaceStr = re.sub('//.*$', '', line)
if replaceStr.endswith('";'):
tempStr = tempStr+replaceStr
rowDataList = tempStr.split('=')
# key先移除首尾空,在移除"符號(hào)
key = clearTextBeginEnd(rowDataList[0],"\"")
rowValue = ''
value = ''
# = 分割可能有多個(gè),后面的最后拼接
if len(rowDataList) > 1:
nextString = '='.join(rowDataList[1:])
rowValue = nextString.strip(";")
value = clearTextBeginEnd(rowValue,"\"")
print("read 有效數(shù)據(jù) key: " + key + " value: " + value)
txtArray.append({"key":key,"value":value})
tempStr = ''
else:
tempStr = tempStr+replaceStr
elif len(tempStr):
#查找先替換最后的//xxx
replaceStr = re.sub('//.*$', '', line)
if replaceStr.endswith('";'):
tempStr = tempStr+replaceStr
rowDataList = tempStr.split('=')
# key先移除首尾空,在移除"符號(hào)
key = clearTextBeginEnd(rowDataList[0],"\"")
rowValue = ''
value = ''
# = 分割可能有多個(gè),后面的最后拼接
if len(rowDataList) > 1:
nextString = '='.join(rowDataList[1:])
rowValue = nextString.strip(";")
value = clearTextBeginEnd(rowValue,"\"")
print("read 有效數(shù)據(jù) key: " + key + " value: " + value)
txtArray.append({"key":key,"value":value})
tempStr = ''
else:
tempStr = tempStr+replaceStr
f.close()
print("數(shù)組集合:")
print("key==============\n")
#print(txtArray)
return txtArray
# 清空txt文件內(nèi)容
def clearTextContent(path):
# 清空txt文件內(nèi)容(方式一)
clearfile = open(path, 'w').close()
#清空txt文件內(nèi)容(方式二)
#with open(path, 'r+') as file:
# file.truncate(0)
# file.close()
print("清空文件內(nèi)容:"+path)
# 清除首尾空和"符號(hào)
def clearTextBeginEnd(sourceText,clearChart):
# isinstance(sourceText,str) 是否字符串,然后取反
if bool(1 - isinstance(sourceText,str)):
print("清除數(shù)據(jù)不是字符串")
return ""
sourceText = sourceText.strip()
sourceText = sourceText.strip(clearChart);
return sourceText
# 獲取文件夾里所有文件夾名
def dir_all_subDirs(file_dir):
for root, dirs, files in os.walk(file_dir):
print(root) #當(dāng)前目錄路徑
print(dirs) #當(dāng)前路徑下所有子目錄
print(files) #當(dāng)前路徑下所有非目錄子文件
print("\n")
# 獲取文件夾里子文件夾名.lproj
def dir_first_all_subDirs(file_dir):
results = []
if file_dir == '':
print("\n異常>>>>> 不是文件目錄路徑")
return results
if not os.path.exists(file_dir):
print("\n異常>>>>>不存在目錄,不創(chuàng)建目錄")
#os.makedirs(dirs)
return results
# for root, dirs, files in os.walk(file_dir):
# print(root) #當(dāng)前目錄路徑
# print(dirs) #當(dāng)前路徑下所有子目錄
# print("\n")
# return dirs
# 獲取文件夾里子文件夾名
pathDirNames = os.listdir(file_dir)
for dirName in pathDirNames:
if dirName.endswith(".lproj"):
results.append(dirName)
return results
# 返回對(duì)應(yīng)key的內(nèi)容
def dicFilterKeyForArray(keyArray,key):
for dic in keyArray:
if dic["key"] == key:
return dic
return {}
'''
設(shè)置單元格樣式
'''
def set_style(name,height,bold=False):
style = xlwt.XFStyle() # 初始化樣式
font = xlwt.Font() # 為樣式創(chuàng)建字體
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
# DASHED虛線
# NO_LINE沒(méi)有
# THIN實(shí)線
borders= xlwt.Borders()
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 42 # 給背景顏色賦值
style.font = font
style.pattern = pattern
style.borders = borders
return style
read_localizable_to_excel()