3 * @copyright Copyright (C) 2010-2021, the Friendica project
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 $return = $this->convertCPluralConditionToPhpReturnStatement($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 . "\n";
117 if ($k != '' && substr($l, 0, 7) == 'msgstr ') {
118 $v = substr($l, 8, $len - 10);
119 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
122 $out .= '$a->strings["' . $k . '"] = "' . $v . '"';
129 if ($k != "" && substr($l, 0, 7) == 'msgstr[') {
132 $out .= '$a->strings["' . $k . '"] = ';
136 $out .= '"' . $v . '"';
145 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
146 if ($match[2] !== '""') {
148 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[1])
150 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[2])
155 if (substr($l, 0, 6) == 'msgid_') {
157 $out .= '$a->strings["' . $k . '"] = ';
161 $k .= trim($l, "\"\r\n");
162 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
165 if (substr($l, 0, 6) == 'msgid ') {
168 $out .= '"' . $v . '"';
172 $out .= ($arr) ? "];\n" : ";\n";
176 $k = str_replace("msgid ", "", $l);
178 $k = trim($k, "\"\r\n");
183 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
187 if ($inv && substr($l, 0, 6) != "msgstr") {
188 $v .= trim($l, "\"\r\n");
189 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
194 $out .= '"' . $v . '"';
198 $out .= ($arr ? "];\n" : ";\n");
201 $out = str_replace(self::DQ_ESCAPE, '\"', $out);
202 if (!file_put_contents($outfile, $out)) {
203 throw new \RuntimeException('Unable to write to ' . $outfile);
209 private function escapeDollar($match)
211 return str_replace('$', '\$', $match[0]);
215 * Converts C-style plural condition in .po files to a PHP-style plural return statement
217 * Adapted from https://github.com/friendica/friendica/issues/9747#issuecomment-769604485
218 * Many thanks to Christian Archer (https://github.com/sunchaserinfo)
220 * @param string $cond
223 private function convertCPluralConditionToPhpReturnStatement(string $cond)
225 $cond = str_replace('n', '$n', $cond);
228 * Parses the condition into an array if there's at least a ternary operator, to a string otherwise
230 * Warning: Black recursive magic
232 * @param string $string
233 * @param array|string $node
235 function parse(string $string, &$node = [])
237 // Removes extra outward parentheses
238 if (strpos($string, '(') === 0 && strrpos($string, ')') === strlen($string) - 1) {
239 $string = substr($string, 1, -1);
242 $q = strpos($string, '?');
243 $s = strpos($string, ':');
245 if ($q === false && $s === false) {
250 if ($q === false || $s < $q) {
251 list($then, $else) = explode(':', $string, 2);
252 $node['then'] = $then;
254 parse($else, $parsedElse);
255 $node['else'] = $parsedElse;
257 list($if, $thenelse) = explode('?', $string, 2);
259 parse($thenelse, $node);
264 * Renders the parsed condition tree into a return statement
266 * Warning: Black recursive magic
271 function render($tree)
273 if (is_array($tree)) {
274 $if = trim($tree['if']);
275 $then = trim($tree['then']);
276 $else = render($tree['else']);
278 return "if ({$if}) { return {$then}; } else {$else}";
283 return " { return {$tree}; }";
289 return is_string($tree) ? "return intval({$tree});" : render($tree);