From 405521807233225a9227af7d9e8301a1a9d5fc2f Mon Sep 17 00:00:00 2001 From: CatchZeng <891793848@qq.com> Date: Fri, 23 Dec 2016 15:51:47 +0800 Subject: [PATCH] support convert Localizable.strings to android strings.xml (LocalizableToStringXml.py) --- .idea/Localizable.strings2Excel.iml | 11 + .idea/misc.xml | 14 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 613 ++++++++++++++++++ Localizable.py | 32 +- LocalizableBack.py | 29 +- LocalizableToStringXml.py | 108 +++ .../Localizable.strings | 0 .../LocalizableBack.strings | 0 LocalizableBack.xls => xls/Localizable.xls | Bin xml/strings.xml | 19 + 12 files changed, 808 insertions(+), 32 deletions(-) create mode 100644 .idea/Localizable.strings2Excel.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 LocalizableToStringXml.py rename Localizable.strings => strings/Localizable.strings (100%) rename LocalizableBack.strings => strings/LocalizableBack.strings (100%) rename LocalizableBack.xls => xls/Localizable.xls (100%) create mode 100644 xml/strings.xml diff --git a/.idea/Localizable.strings2Excel.iml b/.idea/Localizable.strings2Excel.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/.idea/Localizable.strings2Excel.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9e6e353 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7e4714e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..a3cfd27 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,613 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1482475481953 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Localizable.py b/Localizable.py index 6a844d9..8a5c2bb 100644 --- a/Localizable.py +++ b/Localizable.py @@ -4,25 +4,24 @@ import pyExcelerator from optparse import OptionParser -#Add Command Option +#Add command option def addParser(): parser = OptionParser() parser.add_option("-f", "--filePath", - help="File (Localizable.strings) Path.", + help="Localizable.strings file path.", metavar="filePath") parser.add_option("-t", "--targetFilePath", - help="Target File (Localizable.xls) Path.", + help="Target(Localizable.xls) file path.", metavar="targetFilePath") (options, args) = parser.parse_args() - # print "options: %s, args: %s" % (options, args) return options -# Start Convert Localizable.strings To Localizable.xls +# Start convert localizable.strings to localizable.xls def startConvert(options): filePath = options.filePath targetFilePath = options.targetFilePath @@ -34,16 +33,16 @@ def startConvert(options): print "Read Localizable.strings from %s" % (filePath) - # 1.Read Localizable.strings + # 1.Read localizable.strings file = codecs.open(filePath, 'r', 'utf-8') string = file.read() file.close() - # 2.Split By "; + # 2.Split by "; localStringList = string.split('\";') list = [x.split(' = ') for x in localStringList] - print "Read Finish" + print "Read Localizable.strings finish" if targetFilePath is not None: print "Writing data to target file" @@ -51,29 +50,29 @@ def startConvert(options): workbook = pyExcelerator.Workbook() ws = workbook.add_sheet('Localizable.strings') - # Add Two Columns Data + # Add two columns data for x in range(len(list)): for y in range(len(list[x])): if list[x][y]: - string3 = list[x][y] + keyOrValue = list[x][y] if y == 0: - list3 = string3.split('\"') - if(len(list3) > 1): - ws.write(x, y, list3[1]) + keyString = keyOrValue.split('\"') + if(len(keyString) > 1): + ws.write(x, y, keyString[1]) else: print "This is an empty line" else: - value = string3[1:] + value = keyOrValue[1:] ws.write(x, y, value) - #Save To Target File Path + #Save to target file path filePath = targetFilePath if (not targetFilePath.endswith(".xls")): filePath = targetFilePath + "/Localizable.xls" workbook.save(filePath) - print "Success! you can see xls in %s" % (filePath) + print "Convert successfully! you can see xls in %s" % (filePath) else: print "Target file path can not be empty! try -h for help." @@ -91,7 +90,6 @@ def main(): return - #Start main() diff --git a/LocalizableBack.py b/LocalizableBack.py index 37323d8..a1652b2 100644 --- a/LocalizableBack.py +++ b/LocalizableBack.py @@ -4,24 +4,23 @@ import sys from optparse import OptionParser -#Add Command Option +#Add command option def addParser(): parser = OptionParser() parser.add_option("-f", "--filePath", - help="File (LocalizableBack.xls) Path.", + help="Localizable.xls file path.", metavar="filePath") parser.add_option("-t", "--targetFilePath", - help="Target File (Localizable.strings) Path.", + help="Localizable.strings file path.", metavar="targetFilePath") (options, args) = parser.parse_args() - # print "options: %s, args: %s" % (options, args) return options -# Start Convert Localizable.xls To Localizable.strings +# Start convert Localizable.xls to Localizable.strings def startConvert(options): filePath = options.filePath targetFilePath = options.targetFilePath @@ -31,40 +30,40 @@ def startConvert(options): print "File path %s is not correct,Please check it!" % (filePath) return - print "Read localizable back xls file from %s" % (filePath) + print "Read Localizable.xls file from %s" % (filePath) - # Get All Sheets + # Get all sheets sheets = pyExcelerator.parse_xls(filePath) - # Get First Sheet All Data + # Get first sheet all data sheet = sheets[0] tuple = sheet[1] length = len(tuple) / 2 - # Record First Column Data + # Record first column data list0 = [] for x in range(length): if tuple[x, 0]: string = tuple[x, 0] list0.append(string) - print("First Column:\n") + print("First column:\n") print(list0) - # Record Second Column Data + # Record second column data list1 = [] for x in range(length): if tuple[x, 1]: string = tuple[x, 1] list1.append(string) - print("Second Column:\n") + print("Second column:\n") print(list1) if targetFilePath is not None: print "Writing data to target file" - # Output To Localizable.strings + # Output to Localizable.strings wirtePath = targetFilePath if (not targetFilePath.endswith(".strings")): wirtePath = targetFilePath + "/LocalizableBack.strings" @@ -78,9 +77,9 @@ def startConvert(options): stringcontent = "\"" + string0 + "\" " + "= " + "\"" + string1 + "\";\n" fo.write(stringcontent); - # Close File + # Close file fo.close() - print "Success! you can see strings file in %s" % (wirtePath) + print "Convert successfully! you can see strings file in %s" % (wirtePath) else: print "Target file path can not be empty! try -h for help." diff --git a/LocalizableToStringXml.py b/LocalizableToStringXml.py new file mode 100644 index 0000000..366de4c --- /dev/null +++ b/LocalizableToStringXml.py @@ -0,0 +1,108 @@ +# -*- coding:utf-8 -*- + +import codecs +import pyExcelerator +import sys +import os +from optparse import OptionParser + +#Add command option +def addParser(): + parser = OptionParser() + + parser.add_option("-f", "--filePath", + help="Localizable.strings file path.", + metavar="filePath") + + parser.add_option("-t", "--targetFilePath", + help="strings.xml file Path.", + metavar="targetFilePath") + + parser.add_option("-a", "--additionalInfo", + help="Additional info.", + metavar="additionalInfo") + + (options, args) = parser.parse_args() + + return options + + +# Start convert Localizable.strings to android strings.xml +def startConvert(options): + filePath = options.filePath + targetFilePath = options.targetFilePath + additionalInfo = options.additionalInfo + + if filePath is not None: + # ------------------------------ iOS --------------------------- + print "Read Localizable.strings file from %s" % (filePath) + + # 1.Read localizable.strings + file = codecs.open(filePath, 'r', 'utf-8') + string = file.read() + file.close() + + # 2.Split by "; + localStringList = string.split('\";') + list = [x.split(' = ') for x in localStringList] + + print "Read Localizable.strings finish" + + if targetFilePath is not None: + filePath = targetFilePath + if (not targetFilePath.endswith(".xml")): + filePath = targetFilePath + "/strings.xml" + + # ------------------------------ Android --------------------------- + print "Writing data to target file" + + # 1.open file + fo = open(filePath, "wb") + + # 2.write header data + xmlHeader = "\n\n" + fo.write(xmlHeader) + + # Add two columns data + for x in range(len(list)): + keyValue = list[x]; + if len(keyValue) > 1: + string0 = keyValue[0] + string1 = keyValue[1] + string0 = string0.split('\"')[1] + string1 = string1[1:] + stringcontent = " " + string1 + "\n" + fo.write(stringcontent.encode('utf-8')); + + # Save to target file path + if additionalInfo is not None: + fo.write(additionalInfo) + + fo.write(""); + + # Close file + fo.close() + + print "Convert successfully! you can see strings.xml in %s" % (filePath) + + else: + print "Target file path can not be empty! try -h for help." + + else: + print "File path can not be empty! try -h for help." + + return + + +# Mian +def main(): + options = addParser() + startConvert(options) + + return + + +#Start +main() + + diff --git a/Localizable.strings b/strings/Localizable.strings similarity index 100% rename from Localizable.strings rename to strings/Localizable.strings diff --git a/LocalizableBack.strings b/strings/LocalizableBack.strings similarity index 100% rename from LocalizableBack.strings rename to strings/LocalizableBack.strings diff --git a/LocalizableBack.xls b/xls/Localizable.xls similarity index 100% rename from LocalizableBack.xls rename to xls/Localizable.xls diff --git a/xml/strings.xml b/xml/strings.xml new file mode 100644 index 0000000..fc86bed --- /dev/null +++ b/xml/strings.xml @@ -0,0 +1,19 @@ + + + 运行 + 暂停 + 编辑 + 反馈 + 社区 + 评论 + 说明 + 发送反馈 + 微信 + 请写上您的反馈意见 + 电子邮件地址 + 需要您的信息 + 请填写您的电子邮件和反馈信息 + 请为项目命名 + 删除 + 未命名 + \ No newline at end of file