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