-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbbl.php
More file actions
executable file
·207 lines (193 loc) · 6.82 KB
/
bbl.php
File metadata and controls
executable file
·207 lines (193 loc) · 6.82 KB
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
#!/usr/bin/php
<?php
# Help
if (count($argv) <= 1) {
echo PHP_EOL;
echo 'Usage:' . PHP_EOL;
echo $argv[0] . ' get [PROJECT] [-d[DIR]]' . PHP_EOL;
echo $argv[0] . ' merge [PROJECT] [-d[DIR]] [-p[FILE|URL] [-l[LOCALE]]' . PHP_EOL;
echo PHP_EOL;
echo 'Examples:' . PHP_EOL;
echo $argv[0] . ' get opencart -d /path/to/opencart' . PHP_EOL;
echo $argv[0] . ' merge opencart -d /path/to/opencart -p /path/to/opencart.po -l es-cl' . PHP_EOL;
echo $argv[0] . ' merge opencart -d /path/to/opencart -p https://example.com/glotpress/projects/opencart/es-cl/default/ -l es-cl' . PHP_EOL;
echo PHP_EOL;
echo 'To get some more information, please visit: https://github.com/burbuja/bbl.' . PHP_EOL;
exit;
}
# Validate action
if ($argv[1] == 'get' || $argv[1] == 'merge') {
$action = $argv[1];
} else {
echo 'This action is not supported!' . PHP_EOL;
exit;
}
# Validate project
$projects = [
'opencart',
];
if (in_array($argv[2], $projects)) {
$project = $argv[2];
} else {
echo 'This project is not supported!' . PHP_EOL;
exit;
}
# Validate directory
$key = array_search('-d', $argv);
if ($key && array_key_exists($key + 1, $argv)) {
$dn = realpath($argv[$key + 1]);
$tdn = $dn . '_trans';
} else {
echo 'Directory not specified!' . PHP_EOL;
exit;
}
if (!is_dir($dn)) {
echo 'Directory does not exist!' . PHP_EOL;
exit;
}
# Validate .po file
$key = array_search('-p', $argv);
if ($key && array_key_exists($key + 1, $argv)) {
$po = $argv[$key + 1];
} elseif ($action == 'merge') {
echo 'PO file not specified!' . PHP_EOL;
exit;
}
# Validate locale
$key = array_search('-l', $argv);
if ($key && array_key_exists($key + 1, $argv)) {
$locale = $argv[$key + 1];
} elseif ($action == 'merge') {
echo 'Locale not specified!' . PHP_EOL;
exit;
}
# Extract translations
$po_patterns = [
'~msgid\s"(.+)"\nmsgstr\s"(.+)"\n~',
];
if ($action == 'merge') {
if ( preg_match( '~^https?\://~U', $po ) ) {
$o = fopen($po . 'export-translations?format=po', 'r');
$c = stream_get_contents($o);
} elseif (file_exists($po)) {
$o = fopen($po, 'r');
$c = fread($o, filesize($po));
} else {
echo 'PO file does not exist!' . PHP_EOL;
exit;
}
foreach ($po_patterns as $p)
preg_match_all($p, $c, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$d[]['original'] = $m[1];
$d[array_key_last($d)]['translated'] = $m[2];
}
}
# Create a list of files
function getFileList($dir, $array = []) {
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $array = getFileList("$dir/$file", $array) : $array[] = "$dir/$file";
}
return $array;
}
$fl = getFileList($dn);
# Execute action
switch ($project) {
case 'opencart':
switch ($action) {
case 'get':
# Extract phrases
foreach ( $fl as $f ) {
if (
strstr($f, 'language/en-gb')
&& pathinfo($f, PATHINFO_EXTENSION) == 'php'
) {
include_once $f;
if (isset($_)) {
foreach ($_ as $k => $v) {
if ($k != 'text_terms') {
$v = addslashes($v);
$v = str_replace('\\\'', '\'', $v);
$v = str_replace("\n", '\n', $v);
if (empty($phrases) || !in_array($v, $phrases)) {
$phrases[] = $v;
}
}
}
}
unset($_);
}
}
break;
case 'merge':
# Validate locale
# Split locale
# Write .php files
foreach ($fl as $f) {
# Write translated files
$f = str_replace('\\', '/', realpath($f));
if (
strstr($f, 'language/en-gb')
&& pathinfo($f, PATHINFO_EXTENSION) == 'php'
) {
include_once ($f);
if (isset($_)) {
$c = '<?php' .
PHP_EOL .
PHP_EOL;
array_walk(
$_,
function (&$v, $k) {
global $d;
$v = addslashes($v);
$v = str_replace('\\\'', '\'', $v);
$v = str_replace("\n", '\n', $v);
$dk = array_search($v, array_column($d, 'original'));
if ($dk) {
$v = $d[$dk]['translated'];
}
}
);
foreach($_ as $k => $v) {
$c.= '$_[\'' . $k . '\'] = \'' . str_replace ( '\\"', '"', str_replace ( '\'', '\\\'', $_[$k])) . '\';' . "\n";
}
$f = str_replace('en-gb', $locale, $f);
$f = str_replace($dn, $tdn, $f);
if (!is_dir(pathinfo($f, PATHINFO_DIRNAME)))
mkdir(pathinfo($f, PATHINFO_DIRNAME), 0775, true);
$o = fopen ($f, 'w');
fwrite ($o, $c);
fclose ($o);
unset($_);
}
}
}
echo 'Translated files have been written to "' . $tdn . '".' . PHP_EOL;
break;
}
break;
}
# Write a new .po file
if ($action == 'get'){
if (count($phrases) != 0) {
echo 'There are ' . count($phrases) . ' phrases obtained.' . PHP_EOL;
$pofn = $project . '.po';
$po = fopen($pofn, 'w+');
fwrite($po, '# Generated by bbl.php' . PHP_EOL);
fwrite($po, 'msgid ""' . PHP_EOL);
fwrite($po, 'msgstr ""' . PHP_EOL);
foreach ( $phrases as $p ) {
$p = str_replace( '\\\'', "'", $p );
$p = str_replace( "\n", '\n', $p );
fwrite( $po, PHP_EOL );
fwrite( $po, 'msgid "' . $p . '"' . PHP_EOL );
fwrite( $po, 'msgstr ""' . PHP_EOL );
}
fclose($po);
echo 'The file "' . $pofn . '" has been successfully created!' . PHP_EOL;
} else {
echo 'Phrases could not be obtained.' . PHP_EOL;
exit;
}
}