1
1
#!/usr/bin/env python
2
2
import sys , argparse , logging , os , csv
3
3
4
+ PLATFORM = None
5
+ IN_PATH = None
6
+ OUT_PATH = None
7
+ LANG_KEYS = None # static will change later
8
+
4
9
# Gather our code in a main() function
5
10
def main (args , loglevel ):
6
11
logging .basicConfig (format = "%(message)s" , level = loglevel )
7
12
PLATFORM = args .platform
8
13
IN_PATH = args .input
9
14
OUT_PATH = args .output
10
- LANG_KEYS = None #static will change later
11
15
print '\n '
12
16
logging .info ("Start Localizing .... " )
13
17
print '\n '
14
18
logging .info ("------------------------------------" )
15
-
19
+
16
20
# check source path
17
21
logging .debug ("\n " )
18
22
logging .debug ("Validating source path ..." )
@@ -32,7 +36,7 @@ def main(args, loglevel):
32
36
return
33
37
logging .debug ("Valid target path, generating output directory ..." )
34
38
logging .debug ("\n " )
35
-
39
+
36
40
# generate output directory
37
41
OUTPUT_DIR = os .path .join (OUT_PATH , "output" )
38
42
if not os .path .exists (OUTPUT_DIR ):
@@ -42,23 +46,24 @@ def main(args, loglevel):
42
46
else :
43
47
logging .debug ("Using output directory: %s" % OUTPUT_DIR )
44
48
logging .debug ("\n " )
45
-
49
+
46
50
logging .debug ("\n " )
47
51
if PLATFORM == "ios" :
48
52
logging .debug ("Platform : %s" % PLATFORM )
49
53
elif PLATFORM == "android" :
50
54
logging .debug ("Platform : %s" % PLATFORM )
55
+ elif PLATFORM == "json" :
56
+ logging .debug ("Platform : %s" % PLATFORM )
51
57
else :
52
- logging .warn ("Invalid platform, platform should be ios or android only" )
58
+ logging .warn ("Invalid platform, platform should be ios, android, json only" )
53
59
logging .debug ("\n " )
54
- logging .error ('ERROR LOCALIZING.' )
60
+ logging .error ('ERROR LOCALIZING.\n ' )
55
61
return
56
62
57
- logging .info ("Generation output : %s" % OUTPUT_DIR )
63
+ logging .info ("Generated output directory : %s" % OUTPUT_DIR )
58
64
generate_keys (IN_PATH , OUTPUT_DIR , PLATFORM )
59
65
print '\n '
60
- logging .info ("DONE LOCALIZING." )
61
- print '\n '
66
+ logging .info ("DONE LOCALIZING.\n " )
62
67
63
68
def generate_keys (source_path , output , platform ):
64
69
base_out_dir = output
@@ -70,7 +75,7 @@ def generate_keys(source_path, output, platform):
70
75
filename , ext = os .path .splitext (f )
71
76
if ext != '.csv' :
72
77
continue
73
-
78
+
74
79
fullpath = os .path .join (dirname , f )
75
80
76
81
with open (fullpath , 'rb' ) as csvfile :
@@ -80,16 +85,19 @@ def generate_keys(source_path, output, platform):
80
85
if i == 0 :
81
86
line .remove (line [0 ])
82
87
LANG_KEYS = line # assign new value to key
83
-
88
+
84
89
# iterate each language
85
90
lang_path = ""
86
91
for lang in LANG_KEYS :
87
92
if platform == "ios" :
88
93
lang_path = os .path .join (base_out_dir , "{0}.lproj/" .format (lang ))
89
-
94
+
90
95
if platform == "android" :
91
96
lang_path = os .path .join (base_out_dir , "values-{0}/" .format (lang ))
92
-
97
+
98
+ if platform == "json" :
99
+ lang_path = os .path .join (base_out_dir , "{0}/" .format (lang ))
100
+
93
101
# Generate directory per language key
94
102
if not os .path .exists (lang_path ):
95
103
os .makedirs (lang_path )
@@ -98,14 +106,17 @@ def generate_keys(source_path, output, platform):
98
106
full_out_paths = [os .path .join (base_out_dir , "{0}.lproj/" .format (langKey ) + "Localizable.strings" ) for langKey in LANG_KEYS ]
99
107
if platform == "android" :
100
108
full_out_paths = [os .path .join (base_out_dir , "values-{0}/" .format (langKey ) + "strings.xml" ) for langKey in LANG_KEYS ]
101
-
109
+ if platform == "json" :
110
+ full_out_paths = [os .path .join (base_out_dir , "{0}/" .format (langKey ) + "{0}_message.json" .format (langKey )) for langKey in LANG_KEYS ]
111
+
102
112
allwrites = [open (out_path , 'w' ) for out_path in full_out_paths ]
103
-
113
+
104
114
if platform == "ios" :
105
115
start_localize_ios (source_path , allwrites , LANG_KEYS )
106
116
if platform == "android" :
107
117
start_localize_android (source_path , allwrites , LANG_KEYS )
108
-
118
+ if platform == "json" :
119
+ start_localize_json (source_path , allwrites , LANG_KEYS )
109
120
110
121
111
122
# =========================================================================
@@ -121,15 +132,15 @@ def start_localize_ios(source_path, all_writes, lang_keys):
121
132
continue
122
133
123
134
fullpath = os .path .join (dirname , f )
124
- logging .info ("Localizing: %s" , filename )
135
+ logging .info ("Localizing: %s to iOS " , filename )
125
136
126
137
with open (fullpath , 'rb' ) as csvfile :
127
138
[fwrite .write ('\n /* {0} */\n ' .format (filename )) for fwrite in allwrites ]
128
-
139
+
129
140
reader = csv .reader (csvfile , delimiter = ',' )
130
141
iterrows = iter (reader )
131
142
next (iterrows ) # skip first line (it is header).
132
-
143
+
133
144
for row in iterrows :
134
145
row_key = row [0 ].replace (" " , "" )
135
146
# comment
@@ -141,7 +152,8 @@ def start_localize_ios(source_path, all_writes, lang_keys):
141
152
if any ([value == "" for value in row_values ]):
142
153
[fwrite .write ('\n ' ) for idx , fwrite in enumerate (allwrites )]
143
154
else :
144
- [fwrite .write ('"{key}" = "{lang}";\n ' .format (key = row_key , lang = row_values [idx ])) for idx , fwrite in enumerate (allwrites )]
155
+ [fwrite .write ('"{key}" = "{lang}";\n ' .format (key = row_key , lang = row_values [idx ]))
156
+ for idx , fwrite in enumerate (allwrites )]
145
157
[fwrite .close () for fwrite in allwrites ]
146
158
147
159
@@ -153,23 +165,23 @@ def start_localize_android(source_path, all_writes, lang_keys):
153
165
154
166
[fwrite .write ('<?xml version="1.0" encoding="utf-8"?>\n ' ) for fwrite in allwrites ]
155
167
[fwrite .write ('<resources>' ) for fwrite in allwrites ]
156
-
168
+
157
169
for dirname , dirnames , filenames in os .walk (source_path ):
158
170
for f in filenames :
159
171
filename , ext = os .path .splitext (f )
160
172
if ext != '.csv' :
161
173
continue
162
-
174
+
163
175
fullpath = os .path .join (dirname , f )
164
- logging .info ("Localizing: %s" , filename )
176
+ logging .info ("Localizing: %s to Android " , filename )
165
177
166
178
with open (fullpath , 'rb' ) as csvfile :
167
179
[fwrite .write ('\n <!-- {0} -->\n ' .format (filename )) for fwrite in allwrites ]
168
-
180
+
169
181
reader = csv .reader (csvfile , delimiter = ',' )
170
182
iterrows = iter (reader )
171
183
next (iterrows ) # skip first line (it is header).
172
-
184
+
173
185
for row in iterrows :
174
186
row_key = row [0 ].replace (" " , "" )
175
187
# comment
@@ -181,26 +193,75 @@ def start_localize_android(source_path, all_writes, lang_keys):
181
193
if any ([value == "" for value in row_values ]):
182
194
[fwrite .write ('\n ' ) for idx , fwrite in enumerate (allwrites )]
183
195
else :
184
- [fwrite .write ('\t <string name="{key}">{lang}</string>\n ' .format (key = row_key , lang = row_values [idx ])) for idx , fwrite in enumerate (allwrites )]
196
+ [fwrite .write ('\t <string name="{key}">{lang}</string>\n ' .format (key = row_key , lang = row_values [idx ]))
197
+ for idx , fwrite in enumerate (allwrites )]
185
198
[fwrite .write ('</resources>' ) for fwrite in allwrites ]
186
199
[fwrite .close () for fwrite in allwrites ]
187
200
188
201
189
202
# =========================================================================
190
- # +++++++ Standard boilerplate to call the main() function to begin +++++++
203
+ # ++++++++++++++++++++++++++++++ iOS +++++++++++++++++++++++++++++++ +++++++
191
204
# =========================================================================
205
+ def start_localize_json (source_path , all_writes , lang_keys ):
206
+ allwrites = all_writes
207
+ [fwrite .write ('{' ) for fwrite in allwrites ]
208
+
209
+ for dirname , dirnames , filenames in os .walk (source_path ):
210
+ for f in filenames :
211
+ filename , ext = os .path .splitext (f )
212
+ if ext != '.csv' :
213
+ continue
214
+
215
+ fullpath = os .path .join (dirname , f )
216
+ logging .info ("Localizing: %s to JSON" , filename )
217
+
218
+ current_row = 0
219
+
220
+ with open (fullpath , 'rb' ) as csvfile :
221
+ [fwrite .write ('\n /* {0} */\n ' .format (filename )) for fwrite in allwrites ]
222
+
223
+ reader = csv .reader (csvfile , delimiter = ',' )
224
+ iterrows = iter (reader )
225
+ next (iterrows ) # skip first line (it is header).
226
+ list_data = list (iterrows )
227
+
228
+ for row in list_data :
229
+ row_key = row [0 ].replace (" " , "" )
230
+ # comment
231
+ if row_key [:2 ] == '//' :
232
+ continue
233
+ row_values = [row [i + 1 ] for i in range (len (lang_keys ))]
234
+ separator = ", \n "
192
235
193
- parser = argparse .ArgumentParser (description = "Locatization commands" )
194
- parser .add_argument ("-p" ,help = "Specify Platform (iOS, Android)" ,dest = "platform" , type = str , required = True )
195
- parser .add_argument ("-i" ,help = "Input source, CSV file path" ,dest = "input" , type = str , required = True )
196
- parser .add_argument ("-o" ,help = "Generated output path for localizable files" ,dest = "output" , type = str , required = True )
236
+ # check if last row then remove ';'
237
+ if current_row == len (list_data ) - 1 :
238
+ separator = "\n "
239
+ current_row += 1 # iterate row count
240
+
241
+ # if any row is empty, skip it!
242
+ if any ([value == "" for value in row_values ]):
243
+ [fwrite .write ('\n ' ) for idx , fwrite in enumerate (allwrites )]
244
+ else :
245
+ [fwrite .write ('"{key}": "{lang}"{separator}' .format (key = row_key , lang = row_values [idx ], separator = separator ))
246
+ for idx , fwrite in enumerate (allwrites )]
247
+ [fwrite .write ('}' ) for fwrite in allwrites ]
248
+ [fwrite .close () for fwrite in allwrites ]
249
+
250
+
251
+ # =========================================================================
252
+ # +++++++ Standard boilerplate to call the main() function to begin +++++++
253
+ # =========================================================================
254
+ parser = argparse .ArgumentParser (description = "Locatization commands" )
255
+ parser .add_argument ("-p" , help = "Specify Platform (iOS, Android)" , dest = "platform" , type = str , required = True )
256
+ parser .add_argument ("-i" , help = "Input source, CSV file path" , dest = "input" , type = str , required = True )
257
+ parser .add_argument ("-o" , help = "Generated output path for localizable files" , dest = "output" , type = str , required = True )
197
258
198
259
parser .add_argument ("-v" ,
199
260
"--verbose" ,
200
261
help = "increase output verbosity" ,
201
262
action = "store_true" )
202
263
args = parser .parse_args ()
203
-
264
+
204
265
# Setup logging
205
266
if args .verbose :
206
267
loglevel = logging .DEBUG
0 commit comments