需求:项目做国际化语言,产品将项目里边的显示整理成多国语言放入 excel 表中,通过 python 将 excel 直接转出 oc 文件
python代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xlrd #导入解析excel的第三方库
def addTableValues():
data = xlrd.open_workbook('123.xlsx'); #第一列为Key 其他列为翻译的多种语言
table = data.sheet_by_name("工作表1") #存放翻译的 excel 名称
return table
def createOcFileH(table):
values = table.col_values(0) #获取第一列的key
counts = table.nrows #一共多少行
outfileName = "%s.h" % "IBFile" #IBFile.h
foc = open(outfileName,"wb+")
ocString = "\n"
ocString = ocString + "#import <Foundation/Foundation.h>"
for i in range(counts):
if i == 0:
continue
classname = "NSString *"
str = "\n" + classname + values[i] + ";"
ocString = ocString + str
ocString = ocString + "\n\n@end"
foc.write(ocString.encode("utf-8"))
foc.close()
def createOcFileM(table):
keys = table.col_values(0)
values = table.col_values(1)
counts = table.nrows
outfileName = "%s.m" % "IBFile"
fom = open(outfileName,"wb+")
ocString = ""
for i in range(counts):
if i == 0:
continue
classname = "NSString *" + keys[i] + "() {"
content = "\n NSDictionry *dic = {" + "\n" + " @\"EN\""+":"+"@"+"\""+values[i]+"\""+","+"\n };"
other = "\n NSString *result = dic[[UIDevice LanguageInter]];"
string = "\n return result;"+"\n}"+"\n\n"
ocString = ocString + classname + content + other + string
ocString = ocString + "\n\n@end"
fom.write(ocString.encode("utf-8"))
fom.close()
def main():
table = addTableValues()
createOcFileH(table)
createOcFileM(table)
main()
生成的OC文件:
.h
#import <Foundation/Foundation.h>
NSString *Tabbar_Home;
NSString *Tabbar_Trade;
NSString *Tabbar_Position;
NSString *Tabbar_Me;
NSString *Home_Market_Title;
NSString *Home_User_MyBalance;
NSString *Home_User_LogInToView;
NSString *Home_User_LogIn;
NSString *Home_User_Deposit;
NSString *Home_User_UserGuide;
NSString *Home_User_HowToBeginTrading;
NSString *Home_User_HereIsTheAnswer;
.
.
.
@end
.m
NSString *Tabbar_Home() {
NSDictionry *dic = {
@"EN":@"Home",
};
NSString *result = dic[[UIDevice LanguageInter]];//语言开关
return result;
}
NSString *Tabbar_Trade() {
NSDictionry *dic = {
@"EN":@"Trade",
};
NSString *result = dic[[UIDevice LanguageInter]];
return result;
}
NSString *Tabbar_Position() {
NSDictionry *dic = {
@"EN":@"Position",
};
NSString *result = dic[[UIDevice LanguageInter]];
return result;
}
.
.
.
@end
注意!可能遇到的问题 No module named xlrd,解决办法:
版本:python3, 终端:pip3 install xlrd ,运行:python3 ./name.py,
版本:python2 ,终端:pip install xlrd ,运行:./name.py
网友评论