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)));
22 if (!file_exists($pofile)){
23 print "Unable to find '$pofile'\n";
27 print "Out to '$outfile'\n";
31 $infile = file($pofile);
37 $escape_s_exp = '|[^\\\\]\$[a-z]|';
38 function escape_s($match){
39 return str_replace('$','\$',$match[0]);
41 foreach ($infile as $l) {
42 $l = str_replace('\"', DQ_ESCAPE, $l);
44 if ($l[0]=="#") $l="";
45 if (substr($l,0,15)=='"Plural-Forms: '){
47 preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
48 $cond = str_replace('n','$n',$match[2]);
49 // define plural select function if not already defined
50 $fnname = 'string_plural_select_' . $lang;
51 $out .= 'if(! function_exists("'.$fnname.'")) {'."\n";
52 $out .= 'function '. $fnname . '($n){'."\n";
53 $out .= ' return '.$cond.';'."\n";
60 if ($k!="" && substr($l,0,7)=="msgstr "){
61 if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
62 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
64 $v = substr($l,8,$len-10);
65 $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
69 if ($k!="" && substr($l,0,7)=="msgstr["){
70 if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
71 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
78 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
80 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
82 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
85 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
89 $k .= trim($l,"\"\r\n");
90 $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
91 //$out .= '$a->strings['.$k.'] = ';
94 if (substr($l,0,6)=="msgid "){
95 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
96 if ($k!="") $out .= $arr?");\n":";\n";
98 $k = str_replace("msgid ","",$l);
100 $k = trim($k,"\"\r\n");
105 $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
109 if ($inv && substr($l,0,6)!="msgstr") {
110 $v .= trim($l,"\"\r\n");
111 $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
112 //$out .= '$a->strings['.$k.'] = ';
118 if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
119 if ($k!="") $out .= $arr?");\n":";\n";
121 $out = str_replace(DQ_ESCAPE, '\"', $out);
122 file_put_contents($outfile, $out);
126 if (array_search(__file__,get_included_files())===0){
127 po2php_run($_SERVER["argv"],$_SERVER["argc"]);