| 1 |
<?php
|
| 2 |
|
| 3 |
// Execute
|
| 4 |
// for i in *.po ; do php update-placeholders.php $i ; done
|
| 5 |
// on the command line
|
| 6 |
|
| 7 |
|
| 8 |
$argv = $GLOBALS['argv'];
|
| 9 |
array_shift ($argv);
|
| 10 |
if (count($argv)) {
|
| 11 |
$file = $argv[0];
|
| 12 |
} else {
|
| 13 |
echo "No parameters given.";
|
| 14 |
exit;
|
| 15 |
}
|
| 16 |
|
| 17 |
$variables = array();
|
| 18 |
|
| 19 |
$po = file_get_contents($file);
|
| 20 |
$po = preg_replace('~"\\n"~i', '', $po);
|
| 21 |
$po = explode("\n", $po);
|
| 22 |
|
| 23 |
|
| 24 |
foreach ($po as $num => $line) {
|
| 25 |
$content = array();
|
| 26 |
$line = trim($line);
|
| 27 |
|
| 28 |
if (preg_match('~^msgstr "(.*)"$~i', $line, $content)) {
|
| 29 |
$placeholders = array();
|
| 30 |
$variables = array();
|
| 31 |
|
| 32 |
if (preg_match_all('~[!@%][a-z-_]+~i', $po[$num - 1], $placeholders)) {
|
| 33 |
foreach ($placeholders[0] as $placeholder) {
|
| 34 |
$variables[substr($placeholder, 1)] = $placeholder{0};
|
| 35 |
}
|
| 36 |
|
| 37 |
$line = preg_replace_callback('~[!@%]([a-z-_]+)~i', 'replace_placeholder', $line);
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
$output .= $line . "\n";
|
| 42 |
}
|
| 43 |
|
| 44 |
$output = preg_replace('~\\\\n(?!"\\n)~i', "\\n\"\n\"", $output);
|
| 45 |
|
| 46 |
$fp = fopen($file, 'w+');
|
| 47 |
fwrite($fp, $output);
|
| 48 |
fclose($fp);
|
| 49 |
|
| 50 |
|
| 51 |
echo 'Updated '. $file . "\n";
|
| 52 |
|
| 53 |
|
| 54 |
function replace_placeholder($matches) {
|
| 55 |
global $variables;
|
| 56 |
|
| 57 |
$variable = $matches[1];
|
| 58 |
|
| 59 |
if (isset($variables[$variable])) {
|
| 60 |
return $variables[$variable] . $variable;
|
| 61 |
}
|
| 62 |
else {
|
| 63 |
return $matches[0];
|
| 64 |
}
|
| 65 |
}
|