]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
Merge pull request #2110 from annando/1511-subscribe-feed
[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                         // 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";
54                         $out .= '}}'."\n";
55                 }
56
57
58
59
60                 if ($k!="" && substr($l,0,7)=="msgstr "){
61                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
62                         if ($inv) { $inv = False; $out .= '"'.$v.'"'; }
63
64                         $v = substr($l,8,$len-10);
65                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
66                         $inv = True;
67                         //$out .= $v;
68                 }
69                 if ($k!="" && substr($l,0,7)=="msgstr["){
70                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
71                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
72
73                         if (!$arr) {
74                                 $arr=True;
75                                 $out .= "array(\n";
76                         }
77                         $match=Array();
78                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
79                         $out .= "\t".
80                                 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
81                                 ." => "
82                                 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
83                 }
84
85                 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
86
87
88                 if ($ink) {
89                         $k .= trim($l,"\"\r\n");
90                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
91                         //$out .= '$a->strings['.$k.'] = ';
92                 }
93
94                 if (substr($l,0,6)=="msgid "){
95                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
96                         if ($k!="") $out .= $arr?");\n":";\n";
97                         $arr=False;
98                         $k = str_replace("msgid ","",$l);
99                         if ($k != '""' ) {
100                                 $k = trim($k,"\"\r\n");
101                         } else {
102                                 $k = "";
103                         }
104
105                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
106                         $ink = True;
107                 }
108
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.'] = ';
113                 }
114
115
116         }
117
118         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
119         if ($k!="") $out .= $arr?");\n":";\n";
120
121         $out = str_replace(DQ_ESCAPE, '\"', $out);
122         file_put_contents($outfile, $out);
123
124 }
125
126 if (array_search(__file__,get_included_files())===0){
127   po2php_run($_SERVER["argv"],$_SERVER["argc"]);
128 }