streams/util/php2po.php

87 lines
2.3 KiB
PHP
Raw Normal View History

2011-03-18 10:03:15 +00:00
<?php
2021-12-03 03:01:39 +00:00
if (! class_exists('App')) {
class App
{
public static $rtl;
public static $strings = array();
2018-07-19 22:22:04 +00:00
}
2021-12-03 03:01:39 +00:00
}
2011-03-18 10:03:15 +00:00
2021-12-03 03:01:39 +00:00
if ($argc != 2) {
print 'Usage: ' . $argv[0] . " <strings.php>\n\n";
return;
}
2018-07-28 15:36:48 +00:00
$phpfile = $argv[1];
2021-12-03 03:01:39 +00:00
$pofile = dirname($phpfile) . '/messages.po';
2018-07-28 15:36:48 +00:00
2021-12-03 03:01:39 +00:00
if (!file_exists($phpfile)) {
print "Unable to find '$phpfile'\n";
return;
}
2018-07-28 15:36:48 +00:00
include_once($phpfile);
2011-03-18 10:03:15 +00:00
2018-07-28 15:36:48 +00:00
print "Out to '$pofile'\n";
2011-03-18 10:03:15 +00:00
2021-12-02 23:02:31 +00:00
$out = '';
2018-07-28 15:36:48 +00:00
$infile = file($pofile);
2021-12-02 23:02:31 +00:00
$k = '';
$c = '';
2021-12-03 03:01:39 +00:00
$ink = false;
foreach ($infile as $l) {
$l = trim($l, ' ');
if (!preg_match('/^msgstr\[[1-9]/', $l)) {
if ($k != '' && (substr($l, 0, 7) == 'msgstr ' || substr($l, 0, 8) == 'msgstr[0')) {
$ink = false;
$k = stripcslashes($k);
$v = '';
if (isset(App::$strings[$k])) {
$v = App::$strings[$k];
} else {
$k = '__ctx:' . $c . '__ ' . $k;
if (isset(App::$strings[$k])) {
$v = App::$strings[$k];
$c = '';
}
}
if (!empty($v)) {
if (is_array($v)) {
$l = '';
$n = 0;
foreach ($v as &$value) {
$l .= 'msgstr[' . $n . "] \"" . addcslashes($value, "\"\n") . "\"\n";
$n++;
}
} else {
$l = "msgstr \"" . addcslashes($v, "\"\n") . "\"\n";
}
}
}
2018-07-28 15:36:48 +00:00
2021-12-03 03:01:39 +00:00
if (substr($l, 0, 6) == 'msgid_' || substr($l, 0, 7) == 'msgstr[') {
$ink = false;
}
2018-07-28 15:36:48 +00:00
2021-12-03 03:01:39 +00:00
if ($ink) {
preg_match('/^"(.*)"$/', $l, $m);
$k .= $m[1];
}
2018-07-28 15:36:48 +00:00
2021-12-03 03:01:39 +00:00
if (substr($l, 0, 6) == 'msgid ') {
preg_match('/^msgid "(.*)"$/', $l, $m);
$k = $m[1];
$ink = true;
}
2018-07-29 00:11:27 +00:00
2021-12-03 03:01:39 +00:00
if (substr($l, 0, 8) == 'msgctxt ') {
preg_match('/^msgctxt "(.*)"$/', $l, $m);
$c = $m[1];
2018-07-28 15:36:48 +00:00
}
2021-12-03 03:01:39 +00:00
$out .= $l;
}
}
2018-07-28 15:36:48 +00:00
file_put_contents($pofile, $out);