Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Gutierrez Blanco (Nologis) committed May 3, 2018
1 parent 1d5d1ee commit 7fa522d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 37 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
## Localizable.strings2Excel
# Localizable.strings2Excel
Python command line tool for conversion between iOS Localizable.strings and excel file & Localizable.strings to android strings.xml file.

## Buguibu's clone
My version works in this way:
```
python ~/Projects/Localizable.strings2Excel/python/Localizable.py -f ~/Projects/app-root/folder-that-contains-all-*.lprjoj-directories/ -t ~/Desktop
```
This generates the following structure:

* Desktop/strings-files-to-xls_YYYYmmmdd_hhmmss
* ex.xls _whithin this file you will have a sheet tab for each file, for example:_
* Localizable.string
* Main.string
* InfoPlist.strings

[中文请点击](https://github.com/CatchZeng/Localizable.strings2Excel/blob/master/README-CN.md)

#### Convert iOS Localizable.strings files to excel.
Expand Down
56 changes: 26 additions & 30 deletions python/Localizable.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from optparse import OptionParser
from LocalizableStringsFileUtil import LocalizableStringsFileUtil
import pyExcelerator
import time

#Add command option
def addParser():
Expand All @@ -28,36 +29,31 @@ def startConvert(options):
targetFilePath = options.targetFilePath

if directory is not None:
index = 0
if targetFilePath is not None:
workbook = pyExcelerator.Workbook()
ws = workbook.add_sheet('Localizable.strings')

for parent, dirnames, filenames in os.walk(directory):
for dirname in dirnames:
# KeyName & CountryCode
if index == 0:
ws.write(0,0,'keyName')
conturyCode = dirname.split('.')[0]
ws.write(0,index+1,conturyCode)

# Key & Value
path = directory+'/'+dirname+'/Localizable.strings'
(keys, values) = LocalizableStringsFileUtil.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

filePath = targetFilePath + "/Localizable.xls"
workbook.save(filePath)
print "Convert successfully! you can see xls file in %s" % (filePath)

destinyDir = targetFilePath + "/strings-files-to-xls_"+ time.strftime("%Y%m%d_%H%M%S")
if not os.path.exists(destinyDir):
os.makedirs(destinyDir)
for parent, dirnames, unusedfilenames in os.walk(directory):
lprogDirs = [di for di in dirnames if di.endswith(".lproj")]
for dirname in lprogDirs:
workbook = pyExcelerator.Workbook()
for parent, dirnames2, filenames in os.walk(directory+'/'+dirname):
stringFiles = [fi for fi in filenames if fi.endswith(".strings")]
for stringfile in stringFiles:
ws = workbook.add_sheet(stringfile)

# Key & Value
path = directory+dirname+'/' + stringfile
(keys, values) = LocalizableStringsFileUtil.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 = destinyDir +"/" + dirname.replace(".lproj", "") + ".xls"
workbook.save(filePath)
print "Convert %s successfully! you can see xls file in %s" % (directory ,filePath)
else:
print "Target file path can not be empty! try -h for help."
else:
Expand All @@ -68,4 +64,4 @@ def main():
options = addParser()
startConvert(options)

main()
main()
33 changes: 27 additions & 6 deletions python/LocalizableStringsFileUtil.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import os
from Log import Log
import codecs
import re
import io

def removeComments(s):
for x in re.findall(r'("[^\n]*"(?!\\))|(//[^\n]*$|/(?!\\)\*[\s\S]*?\*(?!\\)/)',s,8):s=s.replace(x[1],'')
return s

class LocalizableStringsFileUtil:
'iOS Localizable.strings file util'
Expand Down Expand Up @@ -40,15 +46,30 @@ def getKeysAndValues(path):
return

# 1.Read localizable.strings
file = codecs.open(path, 'r', 'utf-8')
string = file.read()
file.close()
#file = codecs.open(path, 'r', 'utf-16')
#file = io.open(path, 'r', encoding='utf-8')
encodings = ['utf-8', 'utf-16']
for e in encodings:
try:
file = codecs.open(path, 'r', encoding=e)
string = file.read()
file.close()
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
print('opening the file with encoding: %s ' % e)
break
# string = file.read()
# file.close()

# 2.Remove comments
string = removeComments(string)

# 2.Split by ";
# 3.Split by ";
localStringList = string.split('\";')
list = [x.split(' = ') for x in localStringList]

# 3.Get keys & values
# 4.Get keys & values
keys = []
values = []
for x in range(len(list)):
Expand All @@ -59,4 +80,4 @@ def getKeysAndValues(path):
keys.append(key)
values.append(value)

return (keys,values)
return (keys,values)
Binary file modified python/LocalizableStringsFileUtil.pyc
Binary file not shown.
Binary file modified python/Log.pyc
Binary file not shown.

0 comments on commit 7fa522d

Please sign in to comment.