-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.php
346 lines (310 loc) · 9.75 KB
/
parse.php
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
//column indexes
// 0 column adds headings
define("kDESC", 1); // description
define("kTYPE", 2); // type
define("kINDEX", 3); // index
define("kKEY", 4); // key
define("kFL", 5); // first language column
//replace array
$replace = array("ios" => array("int" => "%ld", "string" => "%@", "float" => "%.2f"),
"android" => array("int" => "%d", "string" => "%s", "float" => "%.2f"),
"laravel" => array("int" => "%d", "string" => "%s", "float" => "%.2f"),
);
// helper functions
function fix_string($string,$os)
{
global $replace;
if (preg_match_all("/{(.+?)}/",$string,$match))
{
for ($t = 0; $t < count($match[0]);$t++) {
$string = str_replace($match[0][$t], $replace[$os][$match[1][$t]], $string);
}
}
if ($os == "android") {
$string = htmlspecialchars($string,ENT_NOQUOTES);
}
if ($os == "laravel") {
$string = htmlspecialchars($string,ENT_QUOTES);
}
$fix_new_line = str_replace("\\\\n", "\\n", addslashes($string));
return $fix_new_line;
}
function file_force_contents($dir, $contents)
{
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach($parts as $part) {
if(!is_dir($dir .= "$part/")) {
mkdir($dir);
}
}
file_put_contents("$dir/$file", $contents);
}
//get arguments
$os = "all";
$file = "";
$output = "./";
$separator = "\t";
$mf = FALSE;
$force = FALSE;
for ($i = 1; $i < count($argv);$i++) {
$option = strstr($argv[$i],"-");
if (strlen($option) > 0){
switch ($option) {
case "-ios" : {
$os = "ios";
break;
}
case "-android" : {
$os = "android";
break;
}
case "-laravel" : {
$os = "laravel";
break;
}
case "-all" : {
$os = "all";
break;
}
case "-i" : {
$file = $argv[$i+1];
break;
}
case "-o" : {
if (strrpos($argv[$i+1],"/") != strlen($argv[$i+1])-1) {
$output = $argv[$i+1]."/";
} else {
$output = $argv[$i+1];
}
break;
}
case "-separator" : {
$separator = $argv[$i+1];
break;
}
case "-multifile" : {
$mf = TRUE;
break;
}
case "-force" : {
$force = TRUE;
break;
}
case "-of" : {
$output_file_name = $argv[$i+1];
break;
}
case "-link" : {
$link = $argv[$i+1];
break;
}
case "-linkfile" : {
$linkfile = $argv[$i+1];
break;
}
}
}
}
if (file_exists($file) || isset($link) || isset($linkfile)) {
echo "input file/link/linkFile: ".$file.$link.$linkfile."\n";
if (isset($linkfile)) {
if ($os != "laravel") {
echo "ERROR: linkfile only supported for Laravel (php arrays)".PHP_EOL;
exit;
}
require($linkfile);
} else if (isset($link)) {
$fullfile = file($link);
} else {
$fullfile = file($file);
}
} else {
echo "\nERROR: No input file. Use -i <file>, -link <link> or -linkfile <file> option.\n";
exit;
}
if (isset($fileArray)) {
foreach ($fileArray as $key => $value) {
unset($output_file_name);
if (strlen($value) >= 100+3) {
echo "-> key: ".$key." value: ".substr($value, 0, 50). "..." . substr($value, -50).PHP_EOL;
}
else {
echo "-> key: ".$key." value: ".$value.PHP_EOL;
}
parse(file($fileArray[$key]));
}
} else {
parse($fullfile);
}
function parse($fullfile) {
global $separator;
global $os;
global $file;
global $output;
global $separator;
global $mf;
global $force;
global $output_file_name;
$count = count($fullfile);
//parse first line to get # languages
$firstline = preg_split('/'.$separator.'/', $fullfile[0]);
$languages_count = count($firstline) - constant("kFL");
//get output filename for Laravel
if (!isset($output_file_name) && ($os == "laravel" || $os == "all")) {
$output_file_name = strtolower($firstline[0]);
if (strlen($output_file_name) < 1) {
echo "ERROR: No output file name! Use '-of <name>'' option or add file name to first cell of the input file".PHP_EOL;
exit;
}
} else {
if (strlen($output_file_name) < 1 && ($os == "laravel" || $os == "all")) {
echo "ERROR: No output file name! Use '-of <name>'' option or add file name to first cell of the input file".PHP_EOL;
exit;
}
}
//create language array
for ($k = 1; $k < $count;$k++) {
$linearray = preg_split('/'.$separator.'/', $fullfile[$k]);
$header[$k] = $linearray[0];
$info[$k] = $linearray[kDESC];
$type[$k] = $linearray[kTYPE];
$key[$k] = $linearray[kKEY];
$index[$k] = $linearray[kINDEX];
for ($h = 0; $h < $languages_count; $h++){
$language[trim(strtolower($firstline[$h+kFL]))][$k] = trim($linearray[$h+kFL]);
}
}
//generation date
date_default_timezone_set("UTC");
$generated = "File generated: ".date("j.n.Y - H:i:s e",time());
$ignoreLanguage = array();
//ios file generation
if ($os == "ios" | $os == "all") {
$count = count($key);
foreach ($language as $kk => $vv) {
for ($j = 1; $j < $count+1; $j++) {
if (strlen($key[$j]) > 0) {
if (!strlen($language[$kk][$j]) > 0 && !$force) {
$ignoreLanguage[$kk] = 1;
}
if (strlen($language[$kk][$j]) > 0) {
if ($type[$j] == "Array") {
$to_file[$kk][$j] = "/*".$info[$j]."*/\n"."\"".trim($key[$j])."[".$index[$j]."]\""." = "."\"".fix_string($language[$kk][$j],"ios")."\";\n";
} else {
$to_file[$kk][$j] = "/*".$info[$j]."*/\n"."\"".trim($key[$j])."\""." = "."\"".fix_string($language[$kk][$j],"ios")."\";\n";
}
}
} else {
$to_file[$kk][$j] = "\n/* ".$header[$j]." */\n";
}
}
}
//create files
$genstr = "/*".$generated."*/\n";
foreach ($to_file as $keys => $value) {
if (!isset($ignoreLanguage[$keys])) {
file_force_contents($output.$keys.".lproj/Localizable.strings",$genstr.implode($value));
}
}
unset($value);
unset($keys);
unset($to_file);
unset($ignoreLanguage);
}
//android file generation
if ($os == "android" | $os == "all") {
$count = count($key);
foreach ($language as $kk => $vv) {
for ($j = 1; $j < $count+1; $j++) {
if (strlen($key[$j]) > 0) {
if (!strlen($language[$kk][$j]) > 0 && !$force) {
$ignoreLanguage[$kk] = 1;
}
if (strlen($language[$kk][$j]) > 0) {
if ($type[$j] == "Array") {
$array_key = addslashes(trim($key[$j]));
$to_file[$kk][$headers][$j] = " <!-- ".$info[$j]." --> \n <string-array name=\"".trim($key[$j])."\">\n <item>".fix_string($language[$kk][$j],"android")."</item>\n";
$j++;
while ($type[$j] == "Array" && $array_key == addslashes(trim($key[$j]))) {
$to_file[$kk][$headers][$j] = " <item>".fix_string($language[$kk][$j],"android")."</item>\n";
$j++;
}
$j--;
$to_file[$kk][$headers][$j] .= " </string-array>\n";
} else {
$to_file[$kk][$headers][$j] = " <!-- ".$info[$j]." --> \n"." <string name=\"".trim($key[$j])."\">".fix_string($language[$kk][$j],"android")."</string>\n";
}
}
} else {
$headers = $header[$j];
$to_file[$kk][$headers][$j] = "\n <!-- ".$header[$j]." --> \n";
}
}
}
//create files
$android_file_start = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n <!--".$generated."-->\n";
$android_file_end = "</resources>\n";
if ($mf) {
foreach ($to_file as $keys => $values) {
if (!isset($ignoreLanguage[$keys])) {
foreach ($values as $key => $value) {
file_force_contents($output."values-".$keys."/".strtolower($key)."-strings.xml",$android_file_start.implode($value).$android_file_end);
}
}
}
} else {
foreach ($to_file as $keys => $values) {
if (!isset($ignoreLanguage[$keys])) {
$gen_string = $android_file_start;
foreach ($values as $key => $value) {
$gen_string .=implode($value);
}
$gen_string .= $android_file_end;
file_force_contents($output."values-".$keys."/strings.xml",$gen_string);
}
}
}
unset($value);
unset($keys);
unset($to_file);
unset($ignoreLanguage);
}
//laravel file generation
if ($os == "laravel" | $os == "all") {
$count = count($key);
foreach ($language as $kk => $vv) {
for ($j = 1; $j < $count+1; $j++) {
if (strlen($key[$j]) > 0) {
if (!strlen($language[$kk][$j]) > 0 && !$force) {
$ignoreLanguage[$kk] = 1;
}
if (strlen($language[$kk][$j]) > 0) {
if ($type[$j] == "Array") {
$to_file[$kk][$j] = " /*".$info[$j]."*/".PHP_EOL." \"".trim($key[$j])."_".$index[$j]."\""." => "."\"".fix_string($language[$kk][$j],"laravel")."\",".PHP_EOL;
} else {
$to_file[$kk][$j] = " /*".$info[$j]."*/".PHP_EOL." \"".trim($key[$j])."\""." => "."\"".fix_string($language[$kk][$j],"laravel")."\",".PHP_EOL;
}
}
} else {
$to_file[$kk][$j] = PHP_EOL." /* ".$header[$j]." */".PHP_EOL;
}
}
}
//create files
$laravel_file_start = "<?php".PHP_EOL." /*".$generated."*/".PHP_EOL." return [";
$laravel_file_end = " ];".PHP_EOL."?>".PHP_EOL;
foreach ($to_file as $keys => $value) {
if (!isset($ignoreLanguage[$keys])) {
file_force_contents($output.$keys."/".$output_file_name.".php",$laravel_file_start.implode($value).$laravel_file_end);
}
}
unset($value);
unset($keys);
unset($to_file);
unset($ignoreLanguage);
}
}
?>