]> git.mxchange.org Git - friendica.git/blob - src/Console/PoToPhp.php
Move Console namespace one level up
[friendica.git] / src / Console / PoToPhp.php
1 <?php
2
3 namespace Friendica\Console;
4
5 /**
6  * Read a messages.po file and create strings.php in the same directory
7  *
8  * @author Hypolite Petovan <hypolite@mrpetovan.com>
9  */
10 class PoToPhp extends \Asika\SimpleConsole\Console
11 {
12         protected $helpOptions = ['h', 'help', '?'];
13
14         const DQ_ESCAPE = "__DQ__";
15
16         protected function getHelp()
17         {
18                 $help = <<<HELP
19 console php2po - Generate a strings.php file from a messages.po file
20 Usage
21         bin/console php2po <path/to/messages.po> [-h|--help|-?] [-v]
22
23 Description
24         Read a messages.po file and create the according strings.php in the same directory
25
26 Options
27         -h|--help|-?  Show help information
28         -v            Show more debug information.
29 HELP;
30                 return $help;
31         }
32
33         protected function doExecute()
34         {
35                 if ($this->getOption('v')) {
36                         $this->out('Class: ' . __CLASS__);
37                         $this->out('Arguments: ' . var_export($this->args, true));
38                         $this->out('Options: ' . var_export($this->options, true));
39                 }
40
41                 if (count($this->args) == 0) {
42                         $this->out($this->getHelp());
43                         return 0;
44                 }
45
46                 if (count($this->args) > 1) {
47                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
48                 }
49
50                 $pofile = realpath($this->getArgument(0));
51
52                 if (!file_exists($pofile)) {
53                         throw new \RuntimeException('Supplied file path doesn\'t exist.');
54                 }
55
56                 if (!is_writable(dirname($pofile))) {
57                         throw new \RuntimeException('Supplied directory isn\'t writable.');
58                 }
59
60                 $outfile = dirname($pofile) . DIRECTORY_SEPARATOR . 'strings.php';
61
62                 if (basename(dirname($pofile)) == 'C') {
63                         $lang = 'en';
64                 } else {
65                         $lang = str_replace('-', '_', basename(dirname($pofile)));
66                 }
67
68                 $this->out('Out to ' . $outfile);
69
70                 $out = "<?php\n\n";
71
72                 $infile = file($pofile);
73                 $k = '';
74                 $v = '';
75                 $arr = false;
76                 $ink = false;
77                 $inv = false;
78                 $escape_s_exp = '|[^\\\\]\$[a-z]|';
79
80                 foreach ($infile as $l) {
81                         $l = str_replace('\"', self::DQ_ESCAPE, $l);
82                         $len = strlen($l);
83                         if ($l[0] == "#") {
84                                 $l = "";
85                         }
86
87                         if (substr($l, 0, 15) == '"Plural-Forms: ') {
88                                 $match = [];
89                                 preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
90                                 $cond = str_replace('n', '$n', $match[2]);
91                                 // define plural select function if not already defined
92                                 $fnname = 'string_plural_select_' . $lang;
93                                 $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
94                                 $out .= 'function ' . $fnname . '($n){' . "\n";
95                                 $out .= '       $n = intval($n);' . "\n";
96                                 $out .= '       return ' . $cond . ';' . "\n";
97                                 $out .= '}}' . "\n";
98                         }
99
100                         if ($k != '' && substr($l, 0, 7) == 'msgstr ') {
101                                 if ($ink) {
102                                         $ink = false;
103                                         $out .= '$a->strings["' . $k . '"] = ';
104                                 }
105
106                                 if ($inv) {
107                                         $out .= '"' . $v . '"';
108                                 }
109
110                                 $v = substr($l, 8, $len - 10);
111                                 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
112
113                                 $inv = true;
114                         }
115
116                         if ($k != "" && substr($l, 0, 7) == 'msgstr[') {
117                                 if ($ink) {
118                                         $ink = false;
119                                         $out .= '$a->strings["' . $k . '"] = ';
120                                 }
121                                 if ($inv) {
122                                         $inv = false;
123                                         $out .= '"' . $v . '"';
124                                 }
125
126                                 if (!$arr) {
127                                         $arr = true;
128                                         $out .= "[\n";
129                                 }
130
131                                 $match = [];
132                                 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
133                                 $out .= "\t"
134                                         . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[1])
135                                         . ' => '
136                                         . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[2])
137                                         . ",\n";
138                         }
139
140                         if (substr($l, 0, 6) == 'msgid_') {
141                                 $ink = false;
142                                 $out .= '$a->strings["' . $k . '"] = ';
143                         }
144
145                         if ($ink) {
146                                 $k .= trim($l, "\"\r\n");
147                                 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
148                         }
149
150                         if (substr($l, 0, 6) == 'msgid ') {
151                                 if ($inv) {
152                                         $inv = false;
153                                         $out .= '"' . $v . '"';
154                                 }
155
156                                 if ($k != "") {
157                                         $out .= ($arr) ? "];\n" : ";\n";
158                                 }
159
160                                 $arr = false;
161                                 $k = str_replace("msgid ", "", $l);
162                                 if ($k != '""') {
163                                         $k = trim($k, "\"\r\n");
164                                 } else {
165                                         $k = '';
166                                 }
167
168                                 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
169                                 $ink = true;
170                         }
171
172                         if ($inv && substr($l, 0, 6) != "msgstr") {
173                                 $v .= trim($l, "\"\r\n");
174                                 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
175                         }
176                 }
177
178                 if ($inv) {
179                         $out .= '"' . $v . '"';
180                 }
181
182                 if ($k != '') {
183                         $out .= ($arr ? "];\n" : ";\n");
184                 }
185
186                 $out = str_replace(self::DQ_ESCAPE, '\"', $out);
187                 if (!file_put_contents($outfile, $out)) {
188                         throw new \RuntimeException('Unable to write to ' . $outfile);
189                 }
190
191                 return 0;
192         }
193
194         private function escapeDollar($match)
195         {
196                 return str_replace('$', '\$', $match[0]);
197         }
198 }