]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
tooltip for selecting items
[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         $escape_s_exp = '|[^\\\\]\$[a-z]|';
30         function escape_s($match){
31                 return str_replace('$','\$',$match[0]);
32         }
33         foreach ($infile as $l) {
34                 $len = strlen($l);
35                 if ($l[0]=="#") $l="";
36                 if (substr($l,0,15)=='"Plural-Forms: '){
37                         $match=Array();
38                         preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
39                         $cond = str_replace('n','$n',$match[2]);
40                         $out .= 'function string_plural_select($n){'."\n";
41                         $out .= '       return '.$cond.';'."\n";
42                         $out .= '}'."\n";
43                 }
44                 
45
46
47
48                 if ($k!="" && substr($l,0,7)=="msgstr "){
49                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
50                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
51                         
52                         $v = substr($l,8,$len-10);
53                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
54                         $inv = True;
55                         //$out .= $v;
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                         if (!$arr) {
62                                 $arr=True;
63                                 $out .= "array(\n";
64                         }
65                         $match=Array();
66                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
67                         $out .= "\t". 
68                                 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
69                                 ." => "
70                                 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
71                 }
72         
73                 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
74
75
76                 if ($ink) {
77                         $k .= trim($l,"\"\r\n"); 
78                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
79                         //$out .= '$a->strings['.$k.'] = ';
80                 }
81                 
82                 if (substr($l,0,6)=="msgid "){
83                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
84                         if ($k!="") $out .= $arr?");\n":";\n";
85                         $arr=False;
86                         $k = str_replace("msgid ","",$l);
87                         if ($k != '""' ) {
88                                 $k = trim($k,"\"\r\n");
89                         } else {
90                                 $k = "";
91                         }
92                         
93                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
94                         $ink = True;
95                 }
96                 
97                 if ($inv && substr($l,0,6)!="msgstr") {
98                         $v .= trim($l,"\"\r\n"); 
99                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
100                         //$out .= '$a->strings['.$k.'] = ';
101                 }
102         
103                 
104         }
105
106         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
107         if ($k!="") $out .= $arr?");\n":";\n";
108         
109         file_put_contents($outfile, $out);
110         
111 }
112
113 if (array_search(__file__,get_included_files())===0){
114   po2php_run($argv,$argc);
115 }