-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_strings.py
89 lines (76 loc) · 3.44 KB
/
find_strings.py
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
#find . -name *.m -exec grep '.*@\".*\".*' {} \; | wc
import os
import re
index = 0
pattern = re.compile("(@\".*?[^\\\\]\")")
def is_uncode(s):
return True
# for double byte characters enable below line
#return not all(ord(c) < 128 for c in s)
def should_ignore_line(line):
line = line.strip()
return line.startswith("//") or line.startswith("NSLog") or line.startswith("@param")
def copy_file_in_mem(full_file_name):
file = open(full_file_name)
file_data = file.read()
return file_data
def find_hardcoded_string(full_file_name):
replace_count = 0
file_data = copy_file_in_mem(full_file_name)
write_file_name = full_file_name[full_file_name.rindex("/")+1:]
folder_path = full_file_name.replace(write_file_name, "")
filename = full_file_name[full_file_name.rindex("/")+1:full_file_name.rindex(".")]
for i, line in enumerate(open(full_file_name)):
if should_ignore_line(line):
continue
while True:
match = re.search(pattern, line)
if match:
if is_uncode(match.group(0)):
value = match.group(0)
global index
key_index = "\"%s_%d\"" % (filename, index)
local_value = "NSLocalizedString(@%s, @%s)" %(key_index.replace("@",""), value.replace("@",""))
file_data = file_data.replace(value, local_value)
index = index + 1
replace_count = replace_count + 1
print "%s = %s;" % (key_index.replace("@",""),value.replace("@",""))
line = line[match.end():]
else:
break
# New files will be written in tmp folder
if replace_count > 0:
if not os.path.exists("tmp/" + folder_path):
os.makedirs("tmp/"+ folder_path)
write_file = open("tmp/%s%s" % (folder_path, write_file_name), 'w+')
write_file.write(file_data)
write_file.close()
def check_if_valid_file_name(full_file_name):
return not ('.svn' in full_file_name
or "DS_Store" in full_file_name
or "gif" in full_file_name
or "png" in full_file_name
or "jpeg" in full_file_name
or "jpg" in full_file_name
or full_file_name.endswith(".a"))
def should_skip_dir(root):
return ("cocos2d" in root or
"chat_vshow.xcodeproj" in root or
".framework" in root or
".bundle" in root)
# TODO: Change HelloWorld to your ios project folder's name
print "---------------------------------------------------------------------------------------------------"
print "localized resource key value file will be shown in console only, keep backup before closing console"
print "copy these key value resources"
print "---------------------------------------------------------------------------------------------------"
for root, dirname, filename in os.walk("HelloWorld"):
if should_skip_dir(root):
continue
for fname in filename:
full_file_name = os.path.join(root, fname)
if check_if_valid_file_name(full_file_name):
#print full_file_name
find_hardcoded_string(full_file_name)
print "---------------------------------------------------------------------------------------------------"
print " new source files are placed in temp folder "
print "---------------------------------------------------------------------------------------------------"