Skip to content

Commit

Permalink
support convert Localizable.strings to android strings.xml (Localizab…
Browse files Browse the repository at this point in the history
…leToStringXml.py)
  • Loading branch information
CatchZeng committed Dec 23, 2016
1 parent 93b8bab commit 4055218
Show file tree
Hide file tree
Showing 12 changed files with 808 additions and 32 deletions.
11 changes: 11 additions & 0 deletions .idea/Localizable.strings2Excel.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

613 changes: 613 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions Localizable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,46 +33,46 @@ 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"

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."
Expand All @@ -91,7 +90,6 @@ def main():

return


#Start
main()

29 changes: 14 additions & 15 deletions LocalizableBack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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."
Expand Down
108 changes: 108 additions & 0 deletions LocalizableToStringXml.py
Original file line number Diff line number Diff line change
@@ -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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\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 = " <string name=\"" + string0 + "\">" + string1 + "</string>\n"
fo.write(stringcontent.encode('utf-8'));

# Save to target file path
if additionalInfo is not None:
fo.write(additionalInfo)

fo.write("</resources>");

# 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()


File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions xml/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="PLAY">运行</string>
<string name="PAUSED">暂停</string>
<string name="DESIGN">编辑</string>
<string name="Feedback">反馈</string>
<string name="Groups">社区</string>
<string name="Comment">评论</string>
<string name="Instruction">说明</string>
<string name="Send Feedback">发送反馈</string>
<string name="Wechat">微信</string>
<string name="Enter your comment">请写上您的反馈意见</string>
<string name="Email">电子邮件地址</string>
<string name="Information Needed">需要您的信息</string>
<string name="Please fill in both your email and your feedback">请填写您的电子邮件和反馈信息</string>
<string name="Give your project a new name">请为项目命名</string>
<string name="Delete">删除</string>
<string name="No Name">未命名</string>
</resources>

0 comments on commit 4055218

Please sign in to comment.