3 namespace Friendica\Core\Console;
6 * Read a messages.po file and create strings.php in the same directory
8 * @author Hypolite Petovan <mrpetovan@gmail.com>
10 class PoToPhp extends \Asika\SimpleConsole\Console
12 protected $helpOptions = ['h', 'help', '?'];
14 const DQ_ESCAPE = "__DQ__";
16 protected function getHelp()
19 console php2po - Generate a strings.php file from a messages.po file
21 bin/console php2po <path/to/messages.po> [-h|--help|-?] [-v]
24 Read a messages.po file and create the according strings.php in the same directory
27 -h|--help|-? Show help information
28 -v Show more debug information.
33 protected function doExecute()
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));
41 if (count($this->args) == 0) {
42 $this->out($this->getHelp());
46 if (count($this->args) > 1) {
47 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
52 $pofile = realpath($this->getArgument(0));
54 if (!file_exists($pofile)) {
55 throw new \RuntimeException('Supplied file path doesn\'t exist.');
58 if (!is_writable(dirname($pofile))) {
59 throw new \RuntimeException('Supplied directory isn\'t writable.');
62 $outfile = dirname($pofile) . DIRECTORY_SEPARATOR . 'strings.php';
64 if (strstr($outfile, 'util')) {
67 $lang = str_replace('-', '_', basename(dirname($pofile)));
70 $this->out('Out to ' . $outfile);
74 $infile = file($pofile);
80 $escape_s_exp = '|[^\\\\]\$[a-z]|';
82 foreach ($infile as $l) {
83 $l = str_replace('\"', self::DQ_ESCAPE, $l);
89 if (substr($l, 0, 15) == '"Plural-Forms: ') {
91 preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match);
92 $cond = str_replace('n', '$n', $match[2]);
93 // define plural select function if not already defined
94 $fnname = 'string_plural_select_' . $lang;
95 $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
96 $out .= 'function ' . $fnname . '($n){' . "\n";
97 $out .= ' return ' . $cond . ';' . "\n";
101 if ($k != "" && substr($l, 0, 7) == 'msgstr ') {
104 $out .= '$a->strings["' . $k . '"] = ';
109 $out .= '"' . $v . '"';
112 $v = substr($l, 8, $len - 11);
113 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
117 if ($k != "" && substr($l, 0, 7) == 'msgstr[') {
120 $out .= '$a->strings["' . $k . '"] = ';
124 $out .= '"' . $v . '"';
132 preg_match("|\[([0-9]*)\] (.*)|", $l, $match);
134 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[1])
136 . preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $match[2])
140 if (substr($l, 0, 6) == 'msgid_') {
142 $out .= '$a->strings["' . $k . '"] = ';
146 $k .= trim($l, "\"\r\n");
147 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
150 if (substr($l, 0, 6) == 'msgid ') {
153 $out .= '"' . $v . '"';
156 $out .= ($arr) ? "];\n" : ";\n";
159 $k = str_replace("msgid ", "", $l);
161 $k = trim($k, "\"\r\n");
166 $k = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $k);
170 if ($inv && substr($l, 0, 6) != "msgstr") {
171 $v .= trim($l, "\"\r\n");
172 $v = preg_replace_callback($escape_s_exp, [$this, 'escapeDollar'], $v);
178 $out .= '"' . $v . '"';
182 $out .= ($arr ? "];\n" : ";\n");
185 $out = str_replace(self::DQ_ESCAPE, '\"', $out);
186 if (!file_put_contents($outfile, $out)) {
187 throw new \RuntimeException('Unable to write to ' . $outfile);
193 private function escapeDollar($match)
195 return str_replace('$', '\$', $match[0]);