-
Notifications
You must be signed in to change notification settings - Fork 4
/
update-po-files.php
executable file
·102 lines (84 loc) · 2.77 KB
/
update-po-files.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
#!/usr/bin/env php
<?php
/**
* @license http://unlicense.org/UNLICENSE The UNLICENSE
* @author William Desportes <[email protected]>
*/
declare(strict_types = 1);
$options = getopt(
'',
[
'po-dir:',
'po-template:',
'json-mapping::',
]
);
if (! isset($options['po-dir']) && ! isset($options['po-template'])) {
echo 'usage: update-po-files.php --po-dir po/directory --po-template path/to/file.pot' . PHP_EOL;
exit(1);
}
if (! (is_dir($options['po-dir']) || is_link($options['po-dir']) )) {
echo $options['po-dir'] . ' is not a directory.' . PHP_EOL;
exit(1);
}
if (! is_file($options['po-template'])) {
echo $options['po-template'] . ' is not a file.' . PHP_EOL;
exit(1);
}
if (isset($options['json-mapping'])) {
if (! is_file($options['json-mapping'])) {
echo $options['json-mapping'] . ' is not a file.' . PHP_EOL;
exit(1);
}
}
$poDirectory = realpath($options['po-dir']) . DIRECTORY_SEPARATOR;
$poTemplate = $options['po-template'];
$template = file_get_contents($poTemplate);
$mappings = new stdClass();
$mappings->mappings = array();
$mappings->replacements = array();
if (isset($options['json-mapping'])) {
$mappings = json_decode(file_get_contents($options['json-mapping']));
}
foreach ($mappings->replacements as $replacement) {
$template = str_replace($replacement->from, $replacement->to, $template);
}
$parts = explode('msgid ', $template);
$license_block = str_replace(', fuzzy', '', $parts[0]);
file_put_contents($poTemplate, $template);
/**
* Update the copyright header of a po file
*
* @param string $po_file The po file path
* @return void
*/
function poupdate(string $po_file): void
{
global $mappings, $license_block;
$pot_contents = file_get_contents($po_file);
$parts = explode('msgid ', $pot_contents);
$pot_contents = str_replace($parts[0], $license_block, $pot_contents);
// Replace filename by name
$pot_contents = preg_replace_callback(
'@([0-9a-f]{2}\/[0-9a-f]*.php):([0-9]*)@',
static function ($matchs) use ($mappings) {
$line = intval($matchs[2]);
$replace = $mappings->mappings->{$matchs[1]};
foreach ($replace->debugInfo as $cacheLineNumber => $iii) {
if ($line >= $cacheLineNumber) {
return $replace->fileName . ':' . $iii;
}
}
return $replace->fileName . ':0';
},
$pot_contents
);
file_put_contents($po_file, $pot_contents);
}
echo 'PoDir: ' . $poDirectory . "\r\n";
foreach (glob($poDirectory . '*.po') as $file) {
exec('msgmerge --quiet --previous -U ' . $file . ' ' . $poTemplate);
echo 'File: ' . $file . "\r\n";
poupdate($file);
}
poupdate($poTemplate);