]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
86c41236ea6eca75690d820d3ef292bce17168c3
[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
23         if (!file_exists($pofile)){
24                 print "Unable to find '$pofile'\n";
25                 return;
26         }
27
28         print "Out to '$outfile'\n";
29
30         $out="<?php\n\n";
31
32         $infile = file($pofile);
33         $k="";
34         $v="";
35         $arr = False;
36         $ink = False;
37         $inv = False;
38         $escape_s_exp = '|[^\\\\]\$[a-z]|';
39         function escape_s($match){
40                 return str_replace('$','\$',$match[0]);
41         }
42         foreach ($infile as $l) {
43                 $l = str_replace('\"', DQ_ESCAPE, $l);
44                 $len = strlen($l);
45                 if ($l[0]=="#") {
46                         $l="";
47                 }
48                 if (substr($l,0,15) == '"Plural-Forms: '){
49                         $match=Array();
50                         preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
51                         $cond = str_replace('n','$n',$match[2]);
52                         // define plural select function if not already defined
53                         $fnname = 'string_plural_select_' . $lang;
54                         $out .= 'if (! function_exists("'.$fnname.'")) {'."\n";
55                         $out .= 'function '. $fnname . '($n){'."\n";
56                         $out .= '       return '.$cond.';'."\n";
57                         $out .= '}}'."\n";
58                 }
59
60                 if ($k!="" && substr($l,0,7) == "msgstr "){
61                         if ($ink) {
62                                 $ink = False;
63                                 $out .= '$a->strings["'.$k.'"] = ';
64                         }
65                         if ($inv) {
66                                 $inv = False;
67                                 $out .= '"'.$v.'"';
68                         }
69
70                         $v = substr($l,8,$len-10);
71                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
72                         $inv = True;
73                         //$out .= $v;
74                 }
75                 if ($k!="" && substr($l,0,7) == "msgstr["){
76                         if ($ink) {
77                                 $ink = False;
78                                 $out .= '$a->strings["'.$k.'"] = ';
79                         }
80                         if ($inv) {
81                                 $inv = False;
82                                 $out .= '"'.$v.'"';
83                         }
84
85                         if (!$arr) {
86                                 $arr=True;
87                                 $out .= "array(\n";
88                         }
89                         $match=Array();
90                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
91                         $out .= "\t".
92                                 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
93                                 ." => "
94                                 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
95                 }
96
97                 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
98
99                 if ($ink) {
100                         $k .= trim($l,"\"\r\n");
101                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
102                         //$out .= '$a->strings['.$k.'] = ';
103                 }
104
105                 if (substr($l,0,6)=="msgid "){
106                         if ($inv) {
107                                 $inv = False;
108                                 $out .= '"'.$v.'"';
109                         }
110                         if ($k != "") {
111                                 $out .= $arr?");\n":";\n";
112                         }
113                         $arr = False;
114                         $k = str_replace("msgid ","",$l);
115                         if ($k != '""' ) {
116                                 $k = trim($k,"\"\r\n");
117                         } else {
118                                 $k = "";
119                         }
120
121                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
122                         $ink = True;
123                 }
124
125                 if ($inv && substr($l,0,6)!="msgstr") {
126                         $v .= trim($l,"\"\r\n");
127                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
128                         //$out .= '$a->strings['.$k.'] = ';
129                 }
130
131
132         }
133
134         if ($inv) {
135                 $inv = False;
136                 $out .= '"'.$v.'"';
137         }
138         if ($k!="") {
139                 $out .= $arr?");\n":";\n";
140         }
141
142         $out = str_replace(DQ_ESCAPE, '\"', $out);
143         file_put_contents($outfile, $out);
144
145 }
146
147 if (array_search(__file__,get_included_files())===0){
148   po2php_run($_SERVER["argv"],$_SERVER["argc"]);
149 }