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