]> git.mxchange.org Git - friendica.git/blob - util/po2php.php
Merge pull request #4552 from tobiasd/20180305-messagespo
[friendica.git] / util / po2php.php
1 #!/usr/bin/env php
2 <?php
3 define("DQ_ESCAPE", "__DQ__");
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         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                 $l = str_replace('\"', DQ_ESCAPE, $l);
42                 $len = strlen($l);
43                 if ($l[0] == "#") {
44                         $l = "";
45                 }
46                 if (substr($l, 0, 15) == '"Plural-Forms: ') {
47                         $match = [];
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                 if ($k != "" && substr($l, 0, 7) == "msgstr ") {
59                         if ($ink) {
60                                 $ink = false;
61                                 $out .= '$a->strings["' . $k . '"] = ';
62                         }
63                         if ($inv) {
64                                 $inv = false;
65                                 $out .= '"' . $v . '"';
66                         }
67
68                         $v = substr($l, 8, $len - 10);
69                         $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
70                         $inv = true;
71                         //$out .= $v;
72                 }
73                 if ($k != "" && substr($l, 0, 7) == "msgstr[") {
74                         if ($ink) {
75                                 $ink = false;
76                                 $out .= '$a->strings["' . $k . '"] = ';
77                         }
78                         if ($inv) {
79                                 $inv = false;
80                                 $out .= '"' . $v . '"';
81                         }
82
83                         if (!$arr) {
84                                 $arr=True;
85                                 $out .= "[\n";
86                         }
87                         $match = [];
88                         preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
89                         $out .= "\t"
90                                 . preg_replace_callback($escape_s_exp, 'escape_s', $match[1])
91                                 . " => "
92                                 . preg_replace_callback($escape_s_exp, 'escape_s', $match[2])
93                                 . ",\n";
94                 }
95
96                 if (substr($l, 0, 6) == "msgid_") {
97                         $ink = false;
98                         $out .= '$a->strings["' . $k . '"] = ';
99                 }
100
101                 if ($ink) {
102                         $k .= trim($l, "\"\r\n");
103                         $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
104                         //$out .= '$a->strings['.$k.'] = ';
105                 }
106
107                 if (substr($l, 0, 6) == "msgid ") {
108                         if ($inv) {
109                                 $inv = false;
110                                 $out .= '"' . $v . '"';
111                         }
112                         if ($k != "") {
113                                 $out .= ($arr) ? "];\n" : ";\n";
114                         }
115                         $arr = false;
116                         $k = str_replace("msgid ", "", $l);
117                         if ($k != '""') {
118                                 $k = trim($k, "\"\r\n");
119                         } else {
120                                 $k = "";
121                         }
122
123                         $k = preg_replace_callback($escape_s_exp, 'escape_s', $k);
124                         $ink = true;
125                 }
126
127                 if ($inv && substr($l, 0, 6) != "msgstr") {
128                         $v .= trim($l, "\"\r\n");
129                         $v = preg_replace_callback($escape_s_exp, 'escape_s', $v);
130                         //$out .= '$a->strings['.$k.'] = ';
131                 }
132
133
134         }
135
136         if ($inv) {
137                 $inv = false;
138                 $out .= '"' . $v . '"';
139         }
140         if ($k != "") {
141                 $out .= ($arr ? "];\n" : ";\n");
142         }
143
144         $out = str_replace(DQ_ESCAPE, '\"', $out);
145         file_put_contents($outfile, $out);
146
147 }
148
149 if (array_search(__FILE__, get_included_files()) === 0) {
150         po2php_run($_SERVER["argv"],$_SERVER["argc"]);
151 }