97
113
$data = $fileSize == 0 ? '' : fread($fd, $fileSize);
102
# grab phrases for translate( or i18n( or _( calls; capture string parameter enclosed
103
# in single or double quotes including concatenated strings like 'one' . "two"
118
* grab phrases for translate( or i18n( or _( calls; capture string parameter enclosed
119
* in single or double quotes including concatenated strings like 'one' . "two"
104
121
if (preg_match_all("/(translate|i18n|_)\(\s*(((\s*\.\s*)?('((\\')?[^']*?)*[^\\\]'|\"((\")?[^\"]*?)*[^\\\]\"))+)\s*\)/s",
105
122
$data, $matches, PREG_SET_ORDER)) {
106
123
foreach ($matches as $match) {
107
124
$text = $match[2];
108
125
$cmd = sprintf('return %s;', $text);
109
126
$text = eval($cmd);
110
$text = str_replace('"', '\\"', $text); # escape double-quotes
127
$text = str_replace('"', '\\"', $text); /* escape double-quotes */
128
if (isset($skip[$text])) {
111
131
$string = sprintf('gettext("%s")', $text);
112
$strings[$string] = TRUE;
132
if (!isset($strings[$string])) {
133
$strings[$string] = array();
134
} else if (!isset($localStrings[$string])) {
135
$strings[$string][] = $filename;
137
$localStrings[$string] = true;
116
# grab phrases of this format: translate(array('one' => '...', 'many' => '...'))
117
if (preg_match_all("/translate\(.*?array\('one'\s*=>\s*'(.*?)'.*?'many'\s*=>\s*'(.*?)'.*?\).*?\)/s",
141
/* grab phrases of this format: translate(array('one' => '...', 'many' => '...')) */
142
if (preg_match_all("/translate\(.*?array\(\s*'one'\s*=>\s*'(.*?)'.*?'many'\s*=>\s*'(.*?)'.*?\).*?\)/s",
118
143
$data, $matches, PREG_SET_ORDER)) {
119
144
foreach ($matches as $match) {
120
145
$one = $match[1];
121
146
$many = $match[2];
122
$one = str_replace('"', '\\"', $one); # escape double-quotes
123
$many = str_replace('"', '\\"', $many); # escape double-quotes
147
$one = str_replace('"', '\\"', $one); /* escape double-quotes */
148
$many = str_replace('"', '\\"', $many); /* escape double-quotes */
124
149
$string = sprintf('ngettext("%s", "%s")', $one, $many);
125
$strings[$string] = TRUE;
150
if (!isset($strings[$string])) {
151
$strings[$string] = array();
152
} else if (!isset($localStrings[$string])) {
153
$strings[$string][] = $filename;
155
$localStrings[$string] = true;
129
# grab phrases of this format: translate(array('text' => '...', ...))
130
if (preg_match_all("/translate\(\s*array\('text'\s*=>\s+'(.*?[^\\\])'/s",
159
/* grab phrases of this format: translate(array('text' => '...', ...)) */
160
if (preg_match_all("/translate\(\s*array\(\s*'text'\s*=>\s+'(.*?[^\\\])'/s",
131
161
$data, $matches, PREG_SET_ORDER)) {
132
162
foreach ($matches as $match) {
133
163
$text = $match[1];
134
$text = str_replace('"', '\\"', $text); # escape double-quotes
164
$text = str_replace('"', '\\"', $text); /* escape double-quotes */
135
165
$string = sprintf('gettext("%s")', $text);
136
$strings[$string] = TRUE;
166
if (!isset($strings[$string])) {
167
$strings[$string] = array();
168
} else if (!isset($localStrings[$string])) {
169
$strings[$string][] = $filename;
171
$localStrings[$string] = true;
140
# grab phrases of this format: {g->text ..... }
141
if (preg_match_all("/(\{\s*g->text\s+.*?[^\\\]\})/s",
175
/* grab phrases of this format: {g->text ..... } or {g->changeInDescendents ... } */
176
if (preg_match_all("/(\{\s*g->(?:text|changeInDescendents)\s+.*?[^\\\]\})/s",
142
177
$data, $matches, PREG_SET_ORDER)) {
143
178
foreach ($matches as $match) {
144
179
$string = $match[1];
180
$text = $one = $many = null;
149
# Ignore translations of the form:
151
# as we expect those to be variables containing values that
152
# have been marked elsewhere with the i18n() function
183
* Ignore translations of the form:
185
* as we expect those to be variables containing values that
186
* have been marked elsewhere with the i18n() function
153
188
if (preg_match("/text=\\$/", $string)) {
158
193
if (preg_match("/text=\"(.*?[^\\\])\"/s", $string, $matches)) {
159
194
$text = $matches[1];
160
195
} elseif (preg_match("/text='(.*?)'/s", $string, $matches)) {
161
196
$text = $matches[1];
162
$text = str_replace('"', '\\"', $text); # escape double-quotes
197
$text = str_replace('"', '\\"', $text); /* escape double-quotes */
166
201
if (preg_match("/\s+one=\"(.*?[^\\\])\"/s", $string, $matches)) {
167
202
$one = $matches[1];
168
203
} elseif (preg_match("/\s+one='(.*?)'/s", $string, $matches)) {
169
204
$one = $matches[1];
170
$one = str_replace('"', '\\"', $one); # escape double-quotes
205
$one = str_replace('"', '\\"', $one); /* escape double-quotes */
174
209
if (preg_match("/\s+many=\"(.*?[^\\\])\"/s", $string, $matches)) {
175
210
$many = $matches[1];
176
211
} elseif (preg_match("/\s+many='(.*?)'/s", $string, $matches)) {
177
212
$many = $matches[1];
178
$many = str_replace('"', '\\"', $many); # escape double-quotes
181
# pick gettext() or ngettext()
213
$many = str_replace('"', '\\"', $many); /* escape double-quotes */
216
/* c-format hint for xgettext */
217
if (preg_match('/c[Ff]ormat=(true|false)/s', $string, $matches)) {
218
$hint = '/* xgettext:' . ($matches[1] == 'false' ? 'no-' : '') . "c-format */\n";
223
/* pick gettext() or ngettext() */
182
224
if ($text != null) {
183
$string = sprintf('gettext("%s")', $text);
184
} elseif ($one != null && $many != null) {
185
$string = sprintf('ngettext("%s", "%s")', $one, $many);
225
$string = $hint . sprintf('gettext("%s")', $text);
226
} else if ($one != null && $many != null) {
227
$string = $hint . sprintf('ngettext("%s", "%s")', $one, $many);
188
230
$stderr = fopen('php://stderr', 'w');
189
231
$text = preg_replace("/\n/s", '\n>', $text);
190
fwrite($stderr, "extract.pl parse error: $file:\n");
232
fwrite($stderr, "extract.php parse error: $filename:\n");
191
233
fwrite($stderr, "> $text\n");
195
$string = str_replace('\\}', '}', $string); # unescape right-curly-braces
196
$strings[$string] = TRUE;
237
$string = str_replace('\\}', '}', $string); /* unescape right-curly-braces */
238
if (!isset($strings[$string])) {
239
$strings[$string] = array();
240
} else if (!isset($localStrings[$string])) {
241
$strings[$string][] = $filename;
243
$localStrings[$string] = true;
246
if (count($strings) == $startSize) {
247
unset($strings["\n/* $filename */"]);