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