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)));
23 if (!file_exists($pofile)){
24 print "Unable to find '$pofile'\n";
28 print "Out to '$outfile'\n";
32 $infile = file($pofile);
38 $escape_s_exp = '|[^\\\\]\$[a-z]|';
39 function escape_s($match){
40 return str_replace('$','\$',$match[0]);
42 foreach ($infile as $l) {
43 $l = str_replace('\"', DQ_ESCAPE, $l);
45 if ($l[0]=="#") $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";
61 if ($k!="" && substr($l,0,7)=="msgstr "){
62 if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
63 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
65 $v = substr($l,8,$len-10);
66 $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
70 if ($k!="" && substr($l,0,7)=="msgstr["){
71 if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
72 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
79 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
81 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
83 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
86 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
90 $k .= trim($l,"\"\r\n");
91 $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
92 //$out .= '$a->strings['.$k.'] = ';
95 if (substr($l,0,6)=="msgid "){
96 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
97 if ($k!="") $out .= $arr?");\n":";\n";
99 $k = str_replace("msgid ","",$l);
101 $k = trim($k,"\"\r\n");
106 $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
110 if ($inv && substr($l,0,6)!="msgstr") {
111 $v .= trim($l,"\"\r\n");
112 $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
113 //$out .= '$a->strings['.$k.'] = ';
119 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
120 if ($k!="") $out .= $arr?");\n":";\n";
122 $out = str_replace(DQ_ESCAPE, '\"', $out);
123 file_put_contents($outfile, $out);
127 if (array_search(__file__,get_included_files())===0){
128 po2php_run($_SERVER["argv"],$_SERVER["argc"]);