]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
Merge remote branch 'upstream/master'
[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(strstr($outfile,'util'))
15                 $lang = 'en';
16         else
17                 $lang = str_replace('-','_',basename(dirname($pofile)));
18
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                 $len = strlen($l);
42                 if ($l[0]=="#") $l="";
43                 if (substr($l,0,15)=='"Plural-Forms: '){
44                         $match=Array();
45                         preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
46                         $cond = str_replace('n','$n',$match[2]);
47                         $out .= 'function string_plural_select_' . $lang . '($n){'."\n";
48                         $out .= '       return '.$cond.';'."\n";
49                         $out .= '}'."\n";
50                 }
51                 
52
53
54
55                 if ($k!="" && substr($l,0,7)=="msgstr "){
56                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
57                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
58                         
59                         $v = substr($l,8,$len-10);
60                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
61                         $inv = True;
62                         //$out .= $v;
63                 }
64                 if ($k!="" && substr($l,0,7)=="msgstr["){
65                         if ($ink) { $ink = False; $out .= '$a->strings["'.$k.'"] = '; }
66                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
67                                                 
68                         if (!$arr) {
69                                 $arr=True;
70                                 $out .= "array(\n";
71                         }
72                         $match=Array();
73                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
74                         $out .= "\t". 
75                                 preg_replace_callback($escape_s_exp,'escape_s',$match[1])
76                                 ." => "
77                                 .preg_replace_callback($escape_s_exp,'escape_s',$match[2]) .",\n";
78                 }
79         
80                 if (substr($l,0,6)=="msgid_") { $ink = False; $out .= '$a->strings["'.$k.'"] = '; };
81
82
83                 if ($ink) {
84                         $k .= trim($l,"\"\r\n"); 
85                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
86                         //$out .= '$a->strings['.$k.'] = ';
87                 }
88                 
89                 if (substr($l,0,6)=="msgid "){
90                         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
91                         if ($k!="") $out .= $arr?");\n":";\n";
92                         $arr=False;
93                         $k = str_replace("msgid ","",$l);
94                         if ($k != '""' ) {
95                                 $k = trim($k,"\"\r\n");
96                         } else {
97                                 $k = "";
98                         }
99                         
100                         $k = preg_replace_callback($escape_s_exp,'escape_s',$k);
101                         $ink = True;
102                 }
103                 
104                 if ($inv && substr($l,0,6)!="msgstr") {
105                         $v .= trim($l,"\"\r\n"); 
106                         $v = preg_replace_callback($escape_s_exp,'escape_s',$v);
107                         //$out .= '$a->strings['.$k.'] = ';
108                 }
109         
110                 
111         }
112
113         if ($inv) {     $inv = False; $out .= '"'.$v.'"'; }
114         if ($k!="") $out .= $arr?");\n":";\n";
115         
116         file_put_contents($outfile, $out);
117         
118 }
119
120 if (array_search(__file__,get_included_files())===0){
121   po2php_run($argv,$argc);
122 }