]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
fix po2php \"" issue.
[friendica.git] / util / po2php.php
1 <?php
2 define("DQ_ESCAPE", "__DQ__");
3
4
5 function po2php_run(&$argv, &$argc) {
6
7         if ($argc!=2) {
8                 print "Usage: ".$argv[0]." <file.po>\n\n";
9                 return;
10         }
11         
12         $pofile = $argv[1];
13         $outfile = dirname($pofile)."/strings.php";
14
15         if(strstr($outfile,'util'))
16                 $lang = 'en';
17         else
18                 $lang = str_replace('-','_',basename(dirname($pofile)));
19
20
21
22         if (!file_exists($pofile)){
23                 print "Unable to find '$pofile'\n";
24                 return;
25         }
26         
27         print "Out to '$outfile'\n";
28         
29         $out="<?php\n\n";
30         
31         $infile = file($pofile);
32         $k="";
33         $v="";
34         $arr = False;
35         $ink = False;
36         $inv = False;
37         $escape_s_exp = '|[^\\\\]\$[a-z]|';
38         function escape_s($match){
39                 return str_replace('$','\$',$match[0]);
40         }
41         foreach ($infile as $l) {
42                 $l = str_replace('\"', DQ_ESCAPE, $l);
43                 $len = strlen($l);
44                 if ($l[0]=="#") $l="";
45                 if (substr($l,0,15)=='"Plural-Forms: '){
46                         $match=Array();
47                         preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
48                         $cond = str_replace('n','$n',$match[2]);
49                         $out .= 'function string_plural_select_' . $lang . '($n){'."\n";
50                         $out .= '       return '.$cond.';'."\n";
51                         $out .= '}'."\n";
52                 }
53                 
54
55
56
57                 if ($k!="" && substr($l,0,7)=="msgstr "){
58                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
59                         if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
60                         
61                         $v = substr($l,8,$len-10);
62                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
63                         $inv = True;
64                         //$out .= $v;
65                 }
66                 if ($k!="" && substr($l,0,7)=="msgstr["){
67                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
68                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
69                                                 
70                         if (!$arr) {
71                                 $arr=True;
72                                 $out .= "array(\n";
73                         }
74                         $match=Array();
75                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
76                         $out .= "\t". 
77                                 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
78                                 ." => "
79                                 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
80                 }
81         
82                 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
83
84
85                 if ($ink) {
86                         $k .= trim($l,"\"\r\n"); 
87                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
88                         //$out .= '$a->strings['.$k.'] = ';
89                 }
90                 
91                 if (substr($l,0,6)=="msgid "){
92                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
93                         if ($k!="") $out .= $arr?");\n":";\n";
94                         $arr=False;
95                         $k = str_replace("msgid ","",$l);
96                         if ($k != '""' ) {
97                                 $k = trim($k,"\"\r\n");
98                         } else {
99                                 $k = "";
100                         }
101                         
102                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
103                         $ink = True;
104                 }
105                 
106                 if ($inv && substr($l,0,6)!="msgstr") {
107                         $v .= trim($l,"\"\r\n"); 
108                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
109                         //$out .= '$a->strings['.$k.'] = ';
110                 }
111         
112                 
113         }
114
115         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
116         if ($k!="") $out .= $arr?");\n":";\n";
117         
118         $out = str_replace(DQ_ESCAPE, '\"', $out);
119         file_put_contents($outfile, $out);
120         
121 }
122
123 if (array_search(__file__,get_included_files())===0){
124   po2php_run($argv,$argc);
125 }