2 define("DQ_ESCAPE", "__DQ__");
5 function po2php_run(&$argv, &$argc) {
8 print "Usage: ".$argv[0]." <file.po>\n\n";
13 $outfile = dirname($pofile)."/strings.php";
15 if (strstr($outfile, 'util')) {
18 $lang = str_replace('-','_',basename(dirname($pofile)));
21 if (!file_exists($pofile)) {
22 print "Unable to find '$pofile'\n";
26 print "Out to '$outfile'\n";
30 $infile = file($pofile);
36 $escape_s_exp = '|[^\\\\]\$[a-z]|';
37 function escape_s($match) {
38 return str_replace('$','\$',$match[0]);
40 foreach ($infile as $l) {
41 $l = str_replace('\"', DQ_ESCAPE, $l);
46 if (substr($l, 0, 15) == '"Plural-Forms: ') {
48 preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
49 $cond = str_replace('n', '$n', $match[2]);
50 // define plural select function if not already defined
51 $fnname = 'string_plural_select_' . $lang;
52 $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
53 $out .= 'function '. $fnname . '($n){' . "\n";
54 $out .= ' return ' . $cond . ';' . "\n";
58 if ($k != "" && substr($l, 0, 7) == "msgstr ") {
61 $out .= '$a->strings["' . $k . '"] = ';
65 $out .= '"' . $v . '"';
68 $v = substr($l, 8, $len - 10);
69 $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
73 if ($k != "" && substr($l, 0, 7) == "msgstr[") {
76 $out .= '$a->strings["' . $k . '"] = ';
80 $out .= '"' . $v . '"';
88 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
90 . preg_replace_callback($escape_s_exp, 'escape_s', $match[1])
92 . preg_replace_callback($escape_s_exp, 'escape_s', $match[2])
96 if (substr($l, 0, 6) == "msgid_") {
98 $out .= '$a->strings["' . $k . '"] = ';
102 $k .= trim($l, "\"\r\n");
103 $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
104 //$out .= '$a->strings['.$k.'] = ';
107 if (substr($l, 0, 6) == "msgid ") {
110 $out .= '"' . $v . '"';
113 $out .= ($arr) ? ");\n" : ";\n";
116 $k = str_replace("msgid ", "", $l);
118 $k = trim($k, "\"\r\n");
123 $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
127 if ($inv && substr($l, 0, 6) != "msgstr") {
128 $v .= trim($l, "\"\r\n");
129 $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
130 //$out .= '$a->strings['.$k.'] = ';
138 $out .= '"' . $v . '"';
141 $out .= ($arr ? ");\n" : ";\n");
144 $out = str_replace(DQ_ESCAPE, '\"', $out);
145 file_put_contents($outfile, $out);
149 if (array_search(__FILE__, get_included_files()) === 0) {
150 po2php_run($_SERVER["argv"],$_SERVER["argc"]);