-
Notifications
You must be signed in to change notification settings - Fork 0
/
not-gettexted.php
247 lines (216 loc) · 8.83 KB
/
not-gettexted.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
<?php
/**
* Console application, which extracts or replaces strings for
* translation, which cannot be gettexted
*
* @version $Id: not-gettexted.php 19275 2012-02-10 17:47:42Z nacin $
* @package wordpress-i18n
* @subpackage tools
*/
// see: http://php.net/tokenizer
if (!defined('T_ML_COMMENT'))
define('T_ML_COMMENT', T_COMMENT);
else
define('T_DOC_COMMENT', T_ML_COMMENT);
require_once dirname( __FILE__ ) . '/pomo/po.php';
require_once dirname( __FILE__ ) . '/pomo/mo.php';
class NotGettexted
{
public $enable_logging = false;
public $STAGE_OUTSIDE = 0;
public $STAGE_START_COMMENT = 1;
public $STAGE_WHITESPACE_BEFORE = 2;
public $STAGE_STRING = 3;
public $STAGE_WHITESPACE_AFTER = 4;
public $STAGE_END_COMMENT = 4;
public $commands = array('extract' => 'command_extract', 'replace' => 'command_replace' );
public function logmsg()
{
$args = func_get_args();
if ($this->enable_logging) error_log(implode(' ', $args));
}
public function stderr($msg, $nl=true)
{
fwrite(STDERR, $msg.($nl? "\n" : ""));
}
public function cli_die($msg)
{
$this->stderr($msg);
exit(1);
}
public function unchanged_token($token, $s='')
{
return is_array($token)? $token[1] : $token;
}
public function ignore_token($token, $s='')
{
return '';
}
public function list_php_files($dir)
{
$files = array();
$items = scandir( $dir );
foreach ( (array) $items as $item ) {
$full_item = $dir . '/' . $item;
if ('.' == $item || '..' == $item)
continue;
if ('.php' == substr($item, -4))
$files[] = $full_item;
if (is_dir($full_item))
$files += array_merge($files, NotGettexted::list_php_files($full_item, $files));
}
return $files;
}
public function make_string_aggregator($global_array_name, $filename)
{
$a = $global_array_name;
return create_function('$string, $comment_id, $line_number', 'global $'.$a.'; $'.$a.'[] = array($string, $comment_id, '.var_export($filename, true).', $line_number);');
}
public function make_mo_replacer($global_mo_name)
{
$m = $global_mo_name;
return create_function('$token, $string', 'global $'.$m.'; return var_export($'.$m.'->translate($string), true);');
}
public function walk_tokens(&$tokens, $string_action, $other_action, $register_action=null)
{
$current_comment_id = '';
$current_string = '';
$current_string_line = 0;
$result = '';
$line = 1;
foreach ($tokens as $token) {
if (is_array($token)) {
list($id, $text) = $token;
$line += substr_count($text, "\n");
if ((T_ML_COMMENT == $id || T_COMMENT == $id) && preg_match('|/\*\s*(/?WP_I18N_[a-z_]+)\s*\*/|i', $text, $matches)) {
if ($this->STAGE_OUTSIDE == $stage) {
$stage = $this->STAGE_START_COMMENT;
$current_comment_id = $matches[1];
$this->logmsg('start comment', $current_comment_id);
$result .= call_user_func($other_action, $token);
continue;
}
if ($this->STAGE_START_COMMENT <= $stage && $stage <= $this->STAGE_WHITESPACE_AFTER && '/'.$current_comment_id == $matches[1]) {
$stage = $this->STAGE_END_COMMENT;
$this->logmsg('end comment', $current_comment_id);
$result .= call_user_func($other_action, $token);
if (!is_null($register_action)) call_user_func($register_action, $current_string, $current_comment_id, $current_string_line);
continue;
}
} elseif (T_CONSTANT_ENCAPSED_STRING == $id) {
if ($this->STAGE_START_COMMENT <= $stage && $stage < $this->STAGE_WHITESPACE_AFTER) {
eval('$current_string='.$text.';');
$this->logmsg('string', $current_string);
$current_string_line = $line;
$result .= call_user_func($string_action, $token, $current_string);
continue;
}
} elseif (T_WHITESPACE == $id) {
if ($this->STAGE_START_COMMENT <= $stage && $stage < $this->STAGE_STRING) {
$stage = $this->STAGE_WHITESPACE_BEFORE;
$this->logmsg('whitespace before');
$result .= call_user_func($other_action, $token);
continue;
}
if ($this->STAGE_STRING < $stage && $stage < $this->STAGE_END_COMMENT) {
$stage = $this->STAGE_WHITESPACE_AFTER;
$this->logmsg('whitespace after');
$result .= call_user_func($other_action, $token);
continue;
}
}
}
$result .= call_user_func($other_action, $token);
$stage = $this->STAGE_OUTSIDE;
$current_comment_id = '';
$current_string = '';
$current_string_line = 0;
}
return $result;
}
public function command_extract()
{
$args = func_get_args();
$pot_filename = $args[0];
if (isset($args[1]) && is_array($args[1]))
$filenames = $args[1];
else
$filenames = array_slice($args, 1);
$global_name = '__entries_'.mt_rand(1, 1000);
$GLOBALS[$global_name] = array();
foreach ($filenames as $filename) {
$tokens = token_get_all(file_get_contents($filename));
$aggregator = $this->make_string_aggregator($global_name, $filename);
$this->walk_tokens($tokens, array(&$this, 'ignore_token'), array(&$this, 'ignore_token'), $aggregator);
}
$potf = '-' == $pot_filename? STDOUT : @fopen($pot_filename, 'a');
if (false === $potf) {
$this->cli_die("Couldn't open pot file: $pot_filename");
}
foreach ($GLOBALS[$global_name] as $item) {
@list($string, $comment_id, $filename, $line_number) = $item;
$filename = isset($filename)? preg_replace('|^\./|', '', $filename) : '';
$ref_line_number = isset($line_number)? ":$line_number" : '';
$args = array(
'singular' => $string,
'extracted_comments' => "Not gettexted string $comment_id",
'references' => array("$filename$ref_line_number"),
);
$entry = new Translation_Entry($args);
fwrite($potf, "\n".PO::export_entry($entry)."\n");
}
if ('-' != $pot_filename) fclose($potf);
return true;
}
public function command_replace()
{
$args = func_get_args();
$mo_filename = $args[0];
if (isset($args[1]) && is_array($args[1]))
$filenames = $args[1];
else
$filenames = array_slice($args, 1);
$global_name = '__mo_'.mt_rand(1, 1000);
$GLOBALS[$global_name] = new MO();
$replacer = $this->make_mo_replacer($global_name);
$res = $GLOBALS[$global_name]->import_from_file($mo_filename);
if (false === $res) {
$this->cli_die("Couldn't read MO file '$mo_filename'!");
}
foreach ($filenames as $filename) {
$source = file_get_contents($filename);
if ( strlen($source) > 150000 ) continue;
$tokens = token_get_all($source);
$new_file = $this->walk_tokens($tokens, $replacer, array(&$this, 'unchanged_token'));
$f = fopen($filename, 'w');
fwrite($f, $new_file);
fclose($f);
}
return true;
}
public function usage()
{
$this->stderr('php i18n-comments.php COMMAND OUTPUTFILE INPUTFILES');
$this->stderr('Extracts and replaces strings, which cannot be gettexted');
$this->stderr('Commands:');
$this->stderr(' extract POTFILE PHPFILES appends the strings to POTFILE');
$this->stderr(' replace MOFILE PHPFILES replaces strings in PHPFILES with translations from MOFILE');
}
public function cli()
{
global $argv, $commands;
if (count($argv) < 4 || !in_array($argv[1], array_keys($this->commands))) {
$this->usage();
exit(1);
}
call_user_func_array(array(&$this, $this->commands[$argv[1]]), array_slice($argv, 2));
}
}
// run the CLI only if the file
// wasn't included
$included_files = get_included_files();
if ($included_files[0] == __FILE__) {
error_reporting(E_ALL);
$not_gettexted = new NotGettexted;
$not_gettexted->cli();
}