-
Notifications
You must be signed in to change notification settings - Fork 13
/
add-textdomain.php
108 lines (89 loc) · 2.65 KB
/
add-textdomain.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
<?php
/**
* Console application, which adds textdomain argument
* to all i18n function calls
*
* @package wordpress-i18n
*/
error_reporting(E_ALL);
require_once dirname( __FILE__ ) . '/makepot.php';
class AddTextdomain {
var $modified_contents = '';
var $funcs;
public function AddTextdomain() {
$makepot = new MakePOT;
$this->funcs = array_keys( $makepot->rules );
}
public function usage() {
$usage = "Usage: php add-textdomain.php [-i] <domain> <file>\n\nAdds the string <domain> as a last argument to all i18n function calls in <file>\nand prints the modified php file on standard output.\n\nOptions:\n -i Modifies the PHP file in place, instead of printing it to standard output.\n";
fwrite( STDERR, $usage );
exit( 1 );
}
public function process_token( $token_text, $inplace ) {
if ( $inplace )
$this->modified_contents .= $token_text;
else
echo $token_text;
}
public function process_file( $domain, $source_filename, $inplace ) {
$this->modified_contents = '';
$domain = addslashes( $domain );
$source = file_get_contents( $source_filename );
$tokens = token_get_all($source);
$in_func = false;
$args_started = false;
$parens_balance = 0;
$found_domain = false;
foreach( $tokens as $token ) {
$string_success = false;
if ( is_array( $token ) ) {
list( $id, $text ) = $token;
if ( T_STRING == $id && in_array($text, $this->funcs ) ) {
$in_func = true;
$parens_balance = 0;
$args_started = false;
$found_domain = false;
} elseif ( T_CONSTANT_ENCAPSED_STRING == $id && ( "'$domain'" == $text || "\"$domain\"" == $text ) ) {
if ( $in_func && $args_started ) {
$found_domain = true;
}
}
$token = $text;
} elseif ( '(' == $token ) {
$args_started = true;
++$parens_balance;
} elseif ( ')' == $token ) {
--$parens_balance;
if ( $in_func && 0 == $parens_balance ) {
$token = $found_domain? ')' : ", '$domain' )";
$in_func = false;
$args_started = false;
$found_domain = false;
}
}
$this->process_token( $token, $inplace );
}
if ( $inplace ) {
$f = fopen( $source_filename, 'w' );
fwrite( $f, $this->modified_contents );
fclose( $f );
}
}
}
// run the CLI only if the file
// wasn't included
$included_files = get_included_files();
if ( $included_files[0] == __FILE__ ) {
$adddomain = new AddTextdomain;
if ( !isset( $argv[1] ) || !isset( $argv[2] ) ) {
$adddomain->usage();
}
$inplace = false;
if ( '-i' == $argv[1] ) {
$inplace = true;
if ( !isset( $argv[3] ) ) $adddomain->usage();
array_shift( $argv );
}
$adddomain->process_file( $argv[1], $argv[2], $inplace );
}
?>