-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphatt
executable file
·201 lines (168 loc) · 5.32 KB
/
phatt
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
#!/usr/bin/php -q
<?php
// Phatt - PHP Android Translation Tool - (http://www.github.com/nomego/Phatt)
// Version 1.0 - 2010-08-06
//
// Compares two res XML-files to find differences, and optionally helps
// the user to complete missing strings
//
// usage: ./strings-check values/base.xml values-lang/translated.xml
// or: ./strings-check <langCode>
// (from <project> or <project>/res directory for strings.xml)
// or: ./strings-check <langCode> i
// (to enable Google translation and interactive updates of
// strings in values-<langCode>/strings.xml)
//
// For interactive mode, requirements are:
// * GNU sed
// * py-translate (https://sourceforge.net/projects/py-translate/)
//
// Written by Patrik Kullman <patrik.kullman@gmail.com>
// Based on strings-check.php by Marcin Orlowski <carlos@wfmh.org.pl> 2010.07.03
function print_usage() {
$scriptName = $_SERVER['SCRIPT_NAME'];
echo <<<END
Usage: $scriptName values/strings.xml values-<langCode>/strings.xml
or: $scriptName <langCode>
Shorthand for the above, hardcoded to compare strings.xml
or: $scriptName <langCode> i
Same as above, but offers suggestions from Google Translate
and updates the values-<langCode>/strings.xml.
END;
}
function get_translation() {
echo "\tTranslation (? for help): ";
$input = stream_get_line(STDIN, 1024, PHP_EOL);
if (trim($input) == '?') {
echo <<<END
<blank> Skips current string
! Mark as non-translatable
g Accept Google translation
<anything else> Update language file with new text
END;
$input = get_translation();
}
return $input;
}
class Convert {
public function XmlToArray ($n) {
$xml_array = array();
foreach ($n->childNodes as $nc) {
if ($nc->hasChildNodes()) {
$childNodes = $nc->childNodes;
$children = $childNodes->length;
if ($children > 1) {
$xml_array[$nc->nodeName][] = $this->XmlToArray($nc);
} else {
$xml_array[$nc->nodeName][]['cdata'] = $nc->nodeValue;
}
$counter = count($xml_array[$nc->nodeName])-1;
} else if ($nc->hasAttributes()) {
$counter = 0;
} else {
continue;
}
$attrs = $nc->attributes;
for ($k = 0; $k < $attrs->length; $k++) {
$item = $attrs->item($k);
$xml_array[$nc->nodeName][$counter]['attribute'][$item->nodeName] = $item->nodeValue;
}
}
return ($xml_array);
}
public function GetLabels($fileName) {
$xml = new DOMDocument('1.0','UTF-8');
$xml->load($fileName);
$data = $this->XmlToArray($xml);
$result = array();
foreach (array("string", "string-array", "plurals") as $crap => $type) {
if (!isset($data['resources'][0][$type]))
continue;
foreach ($data['resources'][0][$type] AS $entry) {
if (isset($entry['attribute']['translatable']) && strtolower($entry['attribute']['translatable']) == "false")
continue;
if (isset($entry['attribute']['translate']) && strtolower($entry['attribute']['translate']) == "false")
continue;
$tmp = sprintf("%s", trim($entry['attribute']['name']));
$result[$type.": ".$tmp] = array(
'name' => $tmp,
"type" => $type,
"value" => isset($entry['cdata']) ? trim($entry['cdata']) : ""
);
}
}
return ($result);
}
}
$interactive = false;
if (strlen($_SERVER['argv'][1]) == 2) {
if (file_exists("values"))
$relPath = "values";
else if (file_exists("res/values"))
$relPath = "res/values";
else
die("Don't know where to find values!");
$fileEn = $relPath . '/strings.xml';
$langCode = $_SERVER['argv'][1];
$fileLang = $relPath . '-' . $langCode . '/strings.xml';
if ($_SERVER['argc'] == 3 && in_array(strtolower($_SERVER['argv'][2]), array('i', 'interactive'))) {
$interactive = true;
}
} else {
if ($_SERVER['argc'] != 3) {
print_usage();
die();
}
$fileEn = $_SERVER['argv'][1];
$fileLang = $_SERVER['argv'][2];
}
if (!file_exists($fileEn)) die( sprintf("*** Missing file '%s'\n", $fileEn));
if (!file_exists($fileLang)) die( sprintf("*** Missing file '%s'\n", $fileLang));
$convert = new Convert;
$dataLang = $convert->GetLabels($fileLang);
$dataEn = $convert->GetLabels($fileEn);
$cntLang = $cntEn = 0;
$lasttype = $lastTagName = "";
foreach ($dataEn AS $key => $item) {
if (isset($dataLang[$key]) == false) {
if ($lasttype != $item['type']) echo "Missing " . $item['type'] . ":\n";
printf("\t%s\n", $item['name']);
# printf("\t\tafter: %s\n", $lastTagName);
printf("\t\tvalue: %s\n", $item['value']);
if ($interactive && $item['type'] == "string") {
$gtrans = exec("translate -s en -d $langCode '".$item['value']."'");
printf("\t\ttranslation: %s\n", $gtrans);
$trans = get_translation();
if (strtolower(trim($trans)) == '!') {
$cmd = "sed -i -e 's/ \(name=.".$item['name'].".\)>/ ".'\1'." translatable=\"false\">/g' $fileEn";
system($cmd);
} else {
if (strtolower(trim($trans)) == 'g') {
$trans = $gtrans;
}
if (trim($trans) == "") {
echo "Skipping translation\n";
} else {
$cmd = "sed -i -e 's/\(name=.$lastTagName.*\)$/".'\1\n'." <string name=\"".$item['name']."\">$trans<\/string>/g' $fileLang";
system($cmd);
}
}
}
$lasttype = $item['type'];
$cntLang++;
}
$lastTagName = $item['name'];
}
foreach ($dataLang AS $key => $item) {
if (isset($dataEn[$key]) == false) {
if ($cntEn == 0) echo "\nObsolete:\n";
printf("\t%s\n", $item['name']);
$cntEn++;
}
}
if (($cntLang == 0) && ($cntEn == 0)) {
exit(0);
} else {
exit(1);
}
?>