Blame view

HDFwear/RunScript/AutoGenStrings.py 7.49 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  #!/usr/bin/env python
  # encoding: utf-8
  
  """
  AutoGenStrings.py
  Created by linyu on 2015-02-13.
  Modify by wz on 2017-06-02.
  Copyright (c) 2017 __MyCompanyName__. All rights reserved.
  
  """
  
  import imp
  import sys
  import os
  import glob
  import string
  import re
  import time
  
  imp.reload(sys)
  sys.setdefaultencoding('utf-8') #设置默认编码,只能是utf-8,下面\u4e00-\u9fa5要求的
  
  KTargetFile = '*.lproj/*.strings'
  
  KGenerateStringsFile = 'TempfileOfStoryboardNew.strings'
  
  ColonRegex = ur'["](.*?)["]'
  
  KeyParamRegex = ur'["](.*?)["](\s*)=(\s*)["](.*?)["];'
  
  AnotationRegexPrefix = ur'/(.*?)/'
  
  def getCharaset(string_txt):
  	filedata = bytearray(string_txt[:4])
  	if len(filedata) < 4 :
  		return 0
  	if  (filedata[0] == 0xEF) and (filedata[1] == 0xBB) and (filedata[2] == 0xBF):
  		print 'utf-8'
  		return 1
  	elif (filedata[0] == 0xFF) and (filedata[1] == 0xFE) and (filedata[2] == 0x00) and (filedata[3] == 0x00):
  		print 'utf-32/UCS-4,little endian'
  		return 3
  	elif (filedata[0] == 0x00) and (filedata[1] == 0x00) and (filedata[2] == 0xFE) and (filedata[3] == 0xFF):
  		print 'utf-32/UCS-4,big endian'
  		return 3
  	elif (filedata[0] == 0xFE) and (filedata[1] == 0xFF):
  		print 'utf-16/UCS-2,little endian'
  		return 2
  	elif (filedata[0] == 0xFF) and (filedata[1] == 0xFE):
  		print 'utf-16/UCS-2,big endian'
  		return 2
  	else:
  		print 'can not recognize!'
  		return 0
  
  def decoder(string_txt):
  	var  = getCharaset(string_txt)
  	if var == 1:
  		return string_txt.decode("utf-8")
  	elif var == 2:
  		return string_txt.decode("utf-16")
  	elif var == 3:
  		return string_txt.decode("utf-32")
  	else:
  		return string_txt
  
  def constructAnotationRegex(str):
  	return AnotationRegexPrefix + '\n' + str
  
  def getAnotationOfString(string_txt,suffix):
  	anotationRegex = constructAnotationRegex(suffix)
  	anotationMatch = re.search(anotationRegex,string_txt)
  	anotationString = ''
  	if anotationMatch:
  		match = re.search(AnotationRegexPrefix,anotationMatch.group(0))
  		if match:
  			anotationString = match.group(0)
  	return anotationString
  
  def compareWithFilePath(newStringPath,originalStringPath):
  	print(newStringPath)
  	print(originalStringPath)
  	#read newStringfile
  	nspf=open(newStringPath,"r")
  	#newString_txt =  str(nspf.read(5000000)).decode("utf-16")
  	newString_txt = decoder(str(nspf.read(5000000)))
  	nspf.close()
  	newString_dic = {}
  	anotation_dic = {}
  	for stfmatch in re.finditer(KeyParamRegex , newString_txt):
  		linestr = stfmatch.group(0)
  		anotationString = getAnotationOfString(newString_txt,linestr)
  		linematchs = re.findall(ColonRegex, linestr)
  		if len(linematchs) == 2:
  			leftvalue = linematchs[0]
  			rightvalue = linematchs[1]
  			newString_dic[leftvalue] = rightvalue
  			anotation_dic[leftvalue] = anotationString
  	#read originalStringfile
  	ospf=open(originalStringPath,"r")
  	originalString_txt =  decoder(str(ospf.read(5000000)))
  	ospf.close()
  	originalString_dic = {}
  	for stfmatch in re.finditer(KeyParamRegex , originalString_txt):
  		linestr = stfmatch.group(0)
  		linematchs = re.findall(ColonRegex, linestr)
  		if len(linematchs) == 2:
  			leftvalue = linematchs[0]
  			rightvalue = linematchs[1]
  			originalString_dic[leftvalue] = rightvalue
  	#compare and remove the useless param in original string
  	for key in originalString_dic:
  		if(key not in newString_dic):
  			keystr = '"%s"'%key
  			replacestr = '//'+keystr
  			match = re.search(replacestr , originalString_txt)
  			if match is None:
  				originalString_txt = originalString_txt.replace(keystr,replacestr)
  	#compare and add new param to original string
  	executeOnce = 1
  	for key in newString_dic:
  		values = (key, newString_dic[key])
  		if(key not in originalString_dic):
  			newline = ''
  			if executeOnce == 1:
  				timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  				newline = '\n//##################################################################################\n'
  				newline	+='//#           AutoGenStrings            '+timestamp+'\n'
  				newline	+='//##################################################################################\n'
  				executeOnce = 0
  			newline += '\n'+anotation_dic[key]
  			newline += '\n"%s" = "%s";\n'%values
  			originalString_txt += newline
  	#write into origial file
  	sbfw=open(originalStringPath,"w")
  	sbfw.write(originalString_txt)
  	sbfw.close()
  
  def extractFileName(file_path):
  	seg = file_path.split('/')
  	lastindex = len(seg) - 1
  	return seg[lastindex]
  
  def extractFilePrefix(file_path):
  	seg = file_path.split('/')
  	lastindex = len(seg) - 1
  	prefix =  seg[lastindex].split('.')[0]
  	return prefix
  
  def generateStoryboardStringsfile(storyboard_path,tempstrings_path):
  	cmdstring = 'ibtool '+storyboard_path+' --generate-strings-file '+tempstrings_path
  	if os.system(cmdstring) == 0:
  		return 1
  
  def generateLocalizableFiles(filePath ,sourceFilePath):
  	print ('------->  sourceFilePath: ' + sourceFilePath + '  filePath: ' + filePath)
  	sourceFile_list = glob.glob(sourceFilePath)
  	if len(sourceFile_list) == 0:
  		print 'error dictionary,you should choose the dic upper the Base.lproj'
  		return
  	targetFilePath = filePath + '/' + KTargetFile
  	targetFile_list = glob.glob(targetFilePath)
  	tempFile_Path = filePath + '/' + KGenerateStringsFile
  	if len(targetFile_list) == 0:
  		print 'error framework , no .lproj dic was found'
  		return
  	for sourcePath in sourceFile_list:
  		sourceprefix = extractFilePrefix(sourcePath)
  		sourcename = extractFileName(sourcePath)
  		print 'init with %s'%sourcename
  		if generateStoryboardStringsfile(sourcePath,tempFile_Path) == 1:
  			print '- - genstrings %s successfully'%sourcename
  			for targetPath in targetFile_list:
  				targetprefix = extractFilePrefix(targetPath)
  				targetname = extractFileName(targetPath)
  				if cmp(sourceprefix,targetprefix) == 0:
  					print '- - dealing with %s'%targetPath
  					compareWithFilePath(tempFile_Path,targetPath)
  			print 'finish with %s'%sourcename
  			os.remove(tempFile_Path)
  		else:
  			print '- - genstrings %s error'%sourcename
  
  
  
  #根据项目根目录遍历所有的xib和storyboard的文件路径
  def getAllNibSrcPathFor(dir):
  	sourceFilePaths = []
  	#三个参数:1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
  	for parent,dirnames,filenames in os.walk(dir):
  		for filename in filenames: #输出文件信息
  			print filename
  			if (('.xib' in filename) | ('.storyboard' in filename)):
  				filePath = os.path.join(parent)
  				if filePath not in sourceFilePaths:
  					sourceFilePaths.append(filePath)
  	#print sourceFilePaths
  	return sourceFilePaths
  
  
  def main():
  	print(sys.argv)
  	if len(sys.argv) == 1 :
          #如果在终端运行,注意要修改自己需要国际化的项目文件夹的路径!
  		filePath = '/Users/wz/Documents/git/AutoLocalization/AutoLocalization/'
  	else:
  		filePath = sys.argv[1]
  	# if filePath == None or filePath == '':
  	# 	print('[Error] filePath can not None!')
  	# 	exit(1)
  	sourceFilePaths = getAllNibSrcPathFor(filePath)
  
  	# *.storyboard 国际化
  	for sourceFilePath in sourceFilePaths:
  		baseStrIdx = 0
  		try:
  			baseStrIdx = sourceFilePath.index('Base.lproj')
  		except Exception as e:
  			pass
  		else:
  			sourceFilePathName = sourceFilePath + '/*.storyboard'
  			upperFilePath = sourceFilePath[0:(baseStrIdx-1)]
  			#print upperFilePath
  			generateLocalizableFiles(upperFilePath, sourceFilePathName)
  	# *.xib 国际化
  	for sourceFilePath in sourceFilePaths:
  		baseStrIdx = 0
  		try:
  			baseStrIdx = sourceFilePath.index('Base.lproj')
  		except Exception as e:
  			pass
  		else:
  			sourceFilePathName = sourceFilePath + '/*.xib'
  			upperFilePath = sourceFilePath[0:(baseStrIdx-1)]
  			#print upperFilePath
  			generateLocalizableFiles(upperFilePath, sourceFilePathName)
  
  
  
  
  if __name__ == '__main__':
  	main()