# coding=utf-8
import os
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
DESPATH = os.getcwd()
AllText = []
WDESPATH = os.getcwd()+"/unlocalized.strings"
BLACKDIRLIST = [
DESPATH + '/Frameworks',
DESPATH + '/Example',
'_Pods.xcodeproj',
'README.md',
'LICENSE',
'*.podspec'
'ci.rb',
]
SEPREATE = ' <=> '
def isInBlackList(filePath):
if os.path.isfile(filePath):
return fileNameAtPath(filePath) in BLACKDIRLIST
if filePath:
return filePath in BLACKDIRLIST
return False
def fileNameAtPath(filePath):
return os.path.split(filePath)[1]
def isSignalNote(str):
if '//' in str:
return True
if str.startswith('#pragma'):
return True
return False
def isLogMsg(str):
if str.startswith('NSLog') or str.startswith('FLOG') or str.startswith('TLOG_INFO') or str.startswith('TLOG') or str.startswith('TLOG_WARNING') or str.startswith('TLOG_DEBUG'):
return True
return False
def unlocalizedStrs(filePath):
f = open(filePath)
fileName = fileNameAtPath(filePath)
isMutliNote = False
isImportHeader = False
isHaveWriteFileName = False
file_data = []
importHeaderLineNum = 0
isNeedImport = False
for index, line in enumerate(f):
if 'TeIntenational' in line: isImportHeader = True
if '#import' in line: importHeaderLineNum = index
newLine = line
line = line.strip()
if '/*' in line:
isMutliNote = True
if '*/' in line:
isMutliNote = False
if isMutliNote:
file_data.append(newLine)
continue
if isSignalNote(line):
file_data.append(newLine)
continue
if isLogMsg(line):
file_data.append(newLine)
continue
tmpLine = re.sub(u'(?<!TStringWithKeyFromTable\()(@"[^"%]*[\u4E00-\u9FA5]+[^"%\n]*?")', 'TStringWithKeyFromTable(\\1, @"TJTranslateApp")', newLine.decode('utf-8'))
if tmpLine != newLine and not 'NSAssert' in tmpLine:
print tmpLine
newLine = tmpLine
isNeedImport = True
file_data.append(newLine)
matchList = re.findall(u'@"[^"%]*[\u4E00-\u9FA5]+[^"%\n]*?"', line.decode('utf-8'))
if matchList:
if not isHaveWriteFileName:
wf.write('\n//' + fileName + '\n')
isHaveWriteFileName = True
for item in matchList:
if not item in AllText and not 'NSAssert' in item:
wf.write('\"' + item[2 : len(item)] + ' = \"' + item[2 : len(item)] + ';\n')
AllText.append(item)
if not isImportHeader and isNeedImport:
file_data.insert(importHeaderLineNum+1, '#import <TJString/TJIntenational.h>\n')
with open(filePath,"w") as f:
f.write(''.join(file_data))
elif isImportHeader:
with open(filePath,"w") as f:
f.write(''.join(file_data))
def findFromFile(path):
paths = os.listdir(path)
for aCompent in paths:
aPath = os.path.join(path, aCompent)
if isInBlackList(aPath):
print('在黑名单中,被自动忽略' + aPath)
continue
if os.path.isdir(aPath):
findFromFile(aPath)
elif os.path.isfile(aPath) and os.path.splitext(aPath)[1]=='.m':
unlocalizedStrs(aPath)
if __name__ == '__main__':
wf = open(WDESPATH, 'w')
findFromFile(DESPATH)
wf.close()
print "执行完毕,共检索到" + str(len(AllText)) + "条"