-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv3_po_update.php
54 lines (43 loc) · 1.25 KB
/
v3_po_update.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
<?php
declare(strict_types=1);
const START_TOKEN = "/^msgid \"\"\n/";
const END_TOKEN = "/^msgstr \"/";
function correctBuffer(string $buffer): string
{
$lines = explode("\n", $buffer);
$rebuilt = '';
foreach ($lines as $line) {
$line = trim(trim($line), '"');
$rebuilt .= $line;
}
$rebuilt = preg_replace('/\\\\n/', ' ', $rebuilt);
return sprintf("msgid \"%s\"\n", trim(preg_replace('/\s{2,}/', ' ', $rebuilt)));
}
try {
$file_in = fopen('messages.po', 'r');
$file_out = fopen('messages_fixed.po', 'w');
$in_block = false;
$correction_buffer = '';
while (($buffer = fgets($file_in)) !== false) {
if (preg_match(START_TOKEN, $buffer)) {
$in_block = true;
$correction_buffer = '';
continue;
}
if (!$in_block) {
fputs($file_out, $buffer);
} elseif (preg_match(END_TOKEN, $buffer)) {
fputs($file_out, correctBuffer($correction_buffer));
fputs($file_out, $buffer);
$in_block = false;
$correction_buffer = '';
} else {
$correction_buffer .= $buffer;
}
}
} catch (Exception $ex) {
print $ex;
} finally {
fclose($file_in);
fclose($file_out);
}