3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
25 * Read a messages.po file and create strings.php in the same directory
27 class PoToPhp extends \Asika\SimpleConsole\Console
29 protected $helpOptions = ['h', 'help', '?'];
31 const DQ_ESCAPE = "__DQ__";
33 protected function getHelp()
36 console php2po - Generate a strings.php file from a messages.po file
38 bin/console php2po <path/to/messages.po> [-h|--help|-?] [-v]
41 Read a messages.po file and create the according strings.php in the same directory
44 -h|--help|-? Show help information
45 -v Show more debug information.
50 protected function doExecute()
52 if ($this->getOption('v')) {
53 $this->out('Class: ' . __CLASS__);
54 $this->out('Arguments: ' . var_export($this->args, true));
55 $this->out('Options: ' . var_export($this->options, true));
58 if (count($this->args) == 0) {
59 $this->out($this->getHelp());
63 if (count($this->args) > 1) {
64 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
67 $pofile = realpath($this->getArgument(0));
69 if (!file_exists($pofile)) {
70 throw new \RuntimeException('Supplied file path doesn\'t exist.');
73 if (!is_writable(dirname($pofile))) {
74 throw new \RuntimeException('Supplied directory isn\'t writable.');
77 $outfile = dirname($pofile) . DIRECTORY_SEPARATOR . 'strings.php';
79 if (basename(dirname($pofile)) == 'C') {
82 $lang = str_replace('-', '_', basename(dirname($pofile)));
85 $this->out('Out to ' . $outfile);
89 $infile = file($pofile);
95 $escape_s_exp = '|[^\\\\]\$[a-z]|';
97 foreach ($infile as $l) {
98 $l = str_replace('\"', self::DQ_ESCAPE, $l);
104 if (substr($l, 0, 15) == '"Plural-Forms: ') {
106 preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
107 $cond = str_replace('n', '$n', $match[2]);
108 // define plural select function if not already defined
109 $fnname = 'string_plural_select_' . $lang;
110 $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
111 $out .= 'function ' . $fnname . '($n){' . "\n";
112 $out .= ' $n = intval($n);' . "\n";
113 $out .= ' return ' . $cond . ';' . "\n";
117 if ($k != '' && substr($l, 0, 7) == 'msgstr ') {
120 $out .= '$a->strings["' . $k . '"] = ';
124 $out .= '"' . $v . '"';
127 $v = substr($l, 8, $len - 10);
128 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
133 if ($k != "" && substr($l, 0, 7) == 'msgstr[') {
136 $out .= '$a->strings["' . $k . '"] = ';
140 $out .= '"' . $v . '"';
149 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
151 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[1])
153 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[2])
157 if (substr($l, 0, 6) == 'msgid_') {
159 $out .= '$a->strings["' . $k . '"] = ';
163 $k .= trim($l, "\"\r\n");
164 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
167 if (substr($l, 0, 6) == 'msgid ') {
170 $out .= '"' . $v . '"';
174 $out .= ($arr) ? "];\n" : ";\n";
178 $k = str_replace("msgid ", "", $l);
180 $k = trim($k, "\"\r\n");
185 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
189 if ($inv && substr($l, 0, 6) != "msgstr") {
190 $v .= trim($l, "\"\r\n");
191 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
196 $out .= '"' . $v . '"';
200 $out .= ($arr ? "];\n" : ";\n");
203 $out = str_replace(self::DQ_ESCAPE, '\"', $out);
204 if (!file_put_contents($outfile, $out)) {
205 throw new \RuntimeException('Unable to write to ' . $outfile);
211 private function escapeDollar($match)
213 return str_replace('$', '\$', $match[0]);