iOS开发-国际化 使用python脚本将Localizable.strings转Excel
前言
- iOS开发中需要把Localizable.strings转为Excel表格,方便翻译。
需要环境
检查 python 版本python 版本必须是 2.x
- 终端输入
python --version
Python 2.7.10
检查 pip(python 包管理器)
- 终端输入
pip --version
pip 19.0 from /Library/Python/2.7/site-packages/pip (python 2.7)
- 如果没有安装 pip
- 终端输入
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
- 终端输入
sudo python get-pip.py
安装 pyexcelerator
sudo pip install pyExcelerator
安装 xlrd
sudo pip install xlrd
工具代码
- Strings2Xls.py
# -*- coding:utf-8 -*-
import os
from optparse import OptionParser
from StringsFileUtil import StringsFileUtil
import pyExcelerator
import time
# Add command option
def addParser():
parser = OptionParser()
parser.add_option("-f", "--stringsDir",
help=".strings files directory.",
metavar="stringsDir")
parser.add_option("-t", "--targetDir",
help="The directory where the excel(.xls) files will be saved.",
metavar="targetDir")
parser.add_option("-e", "--excelStorageForm",
type="string",
default="multiple",
help="The excel(.xls) file storage forms including single(single file), multiple(multiple files), default is multiple.",
metavar="excelStorageForm")
(options, _) = parser.parse_args()
return options
# convert .strings files to single xls file
def convertToSingleFile(stringsDir, targetDir):
destDir = targetDir + "/strings-files-to-xls_" + \
time.strftime("%Y%m%d_%H%M%S")
if not os.path.exists(destDir):
os.makedirs(destDir)
# Create xls sheet
for _, dirnames, _ in os.walk(stringsDir):
lprojDirs = [di for di in dirnames if di.endswith(".lproj")]
for dirname in lprojDirs:
for _, _, filenames in os.walk(stringsDir+'/'+dirname):
stringsFiles = [
fi for fi in filenames if fi.endswith(".strings")]
for stringfile in stringsFiles:
fileName = stringfile.replace(".strings", "")
filePath = destDir + "/" + fileName + ".xls"
if not os.path.exists(filePath):
workbook = pyExcelerator.Workbook()
ws = workbook.add_sheet(fileName)
index = 0
for dirname in dirnames:
if index == 0:
ws.write(0, 0, 'keyName')
countryCode = dirname.replace(".lproj", "")
ws.write(0, index+1, countryCode)
path = stringsDir+'/' + dirname + '/' + stringfile
(keys, values) = StringsFileUtil.getKeysAndValues(
path)
for x in range(len(keys)):
key = keys[x]
value = values[x]
if (index == 0):
ws.write(x+1, 0, key)
ws.write(x+1, 1, value)
else:
ws.write(x+1, index + 1, value)
index += 1
workbook.save(filePath)
print "Convert %s successfully! you can see xls file in %s" % (
stringsDir, destDir)
# convert .strings files to multiple xls files
def convertToMultipleFiles(stringsDir, targetDir):
destDir = targetDir + "/strings-files-to-xls_" + \
time.strftime("%Y%m%d_%H%M%S")
if not os.path.exists(destDir):
os.makedirs(destDir)
for _, dirnames, _ in os.walk(stringsDir):
lprojDirs = [di for di in dirnames if di.endswith(".lproj")]
for dirname in lprojDirs:
workbook = pyExcelerator.Workbook()
for _, _, filenames in os.walk(stringsDir+'/'+dirname):
stringsFiles = [
fi for fi in filenames if fi.endswith(".strings")]
for stringfile in stringsFiles:
ws = workbook.add_sheet(stringfile)
path = stringsDir+dirname+'/' + stringfile
(keys, values) = StringsFileUtil.getKeysAndValues(
path)
for keyIndex in range(len(keys)):
key = keys[keyIndex]
value = values[keyIndex]
ws.write(keyIndex, 0, key)
ws.write(keyIndex, 1, value)
filePath = destDir + "/" + dirname.replace(".lproj", "") + ".xls"
workbook.save(filePath)
print "Convert %s successfully! you can see xls file in %s" % (
stringsDir, destDir)
# Start convert .strings files to xls
def startConvert(options):
stringsDir = options.stringsDir
targetDir = options.targetDir
print "Start converting"
if stringsDir is None:
print ".strings files directory can not be empty! try -h for help."
return
if targetDir is None:
print "Target file directory can not be empty! try -h for help."
return
if options.excelStorageForm == "single":
convertToSingleFile(stringsDir, targetDir)
else:
convertToMultipleFiles(stringsDir, targetDir)
def main():
options = addParser()
startConvert(options)
main()
使用
- 将自己的Localizable.strings文件拖入到ios路径下
- 打开终端
cd /Users/xxx/Desktop/Demo
- 再次输入
python py/Strings2Xls.py -f /Users/lindashuai/Desktop/Demo/ios/ -t /Users/lindashuai/Desktop/Demo/output/