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