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;
27 * Read a strings.php file and create messages.po in the same directory
29 class PhpToPo extends \Asika\SimpleConsole\Console
32 protected $helpOptions = ['h', 'help', '?'];
34 private $normBaseMsgIds = [];
35 const NORM_REGEXP = "|[\\\]|";
40 public function __construct(App $app, array $argv = null)
42 parent::__construct($argv);
47 protected function getHelp()
50 console php2po - Generate a messages.po file from a strings.php file
52 bin/console php2po [-p <n>] [--base <file>] <path/to/strings.php> [-h|--help|-?] [-v]
55 Read a strings.php file and create the according messages.po in the same directory
58 -p <n> Number of plural forms. Default: 2
59 --base <file> Path to base messages.po file. Default: view/lang/C/messages.po
60 -h|--help|-? Show help information
61 -v Show more debug information.
66 protected function doExecute()
68 if ($this->getOption('v')) {
69 $this->out('Class: ' . __CLASS__);
70 $this->out('Arguments: ' . var_export($this->args, true));
71 $this->out('Options: ' . var_export($this->options, true));
74 if (count($this->args) == 0) {
75 $this->out($this->getHelp());
79 if (count($this->args) > 1) {
80 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
85 $phpfile = realpath($this->getArgument(0));
87 if (!file_exists($phpfile)) {
88 throw new \RuntimeException('Supplied file path doesn\'t exist.');
91 if (!is_writable(dirname($phpfile))) {
92 throw new \RuntimeException('Supplied directory isn\'t writable.');
95 $pofile = dirname($phpfile) . DIRECTORY_SEPARATOR . 'messages.po';
98 include_once($phpfile);
101 $out .= "# FRIENDICA Distributed Social Network\n";
102 $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
103 $out .= "# This file is distributed under the same license as the Friendica package.\n";
105 $out .= 'msgid ""' . "\n";
106 $out .= 'msgstr ""' . "\n";
107 $out .= '"Project-Id-Version: friendica\n"' . "\n";
108 $out .= '"Report-Msgid-Bugs-To: \n"' . "\n";
109 $out .= '"POT-Creation-Date: ' . date("Y-m-d H:i:sO") . '\n"' . "\n";
110 $out .= '"MIME-Version: 1.0\n"' . "\n";
111 $out .= '"Content-Type: text/plain; charset=UTF-8\n"' . "\n";
112 $out .= '"Content-Transfer-Encoding: 8bit\n"' . "\n";
114 // search for plural info
117 $lang_pnum = $this->getOption('p', 2);
119 $infile = file($phpfile);
120 foreach ($infile as $l) {
122 if ($this->startsWith($l, 'function string_plural_select_')) {
123 $lang = str_replace('function string_plural_select_', '', str_replace('($n){', '', $l));
125 if ($this->startsWith($l, 'return')) {
126 $lang_logic = str_replace('$', '', trim(str_replace('return ', '', $l), ';'));
131 $this->out('Language: ' . $lang);
132 $this->out('Plural forms: ' . $lang_pnum);
133 $this->out('Plural forms: ' . $lang_logic);
135 $out .= sprintf('"Language: %s\n"', $lang) . "\n";
136 $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n";
139 $base_path = $this->getOption('base', 'view/lang/C/messages.po');
141 // load base messages.po and extract msgids
143 $base_f = file($base_path);
145 throw new \RuntimeException('The base ' . $base_path . ' file is missing or unavailable to read.');
148 $this->out('Loading base file ' . $base_path . '...');
153 foreach ($base_f as $l) {
156 if ($this->startsWith($l, 'msgstr')) {
158 $base_msgids[$_mid] = $_mids;
159 $this->normBaseMsgIds[preg_replace(self::NORM_REGEXP, "", $_mid)] = $_mid;
167 if ($this->startsWith($l, '"') && $_f == 2) {
168 $_mids[count($_mids) - 1] .= "\n" . $l;
170 if ($this->startsWith($l, 'msgid_plural ')) {
172 $_mids[] = str_replace('msgid_plural ', '', $l);
175 if ($this->startsWith($l, '"') && $_f == 1) {
177 $_mids[count($_mids) - 1] .= "\n" . $l;
179 if ($this->startsWith($l, 'msgid ')) {
181 $_mid = str_replace('msgid ', '', $l);
186 $this->out('Creating ' . $pofile . '...');
188 // create msgid and msgstr
190 foreach ($a->strings as $key => $str) {
191 $msgid = $this->massageString($key);
193 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
194 $out .= "#, php-format\n";
196 $msgid = $this->findOriginalMsgId($msgid);
197 $out .= 'msgid ' . $msgid . "\n";
199 if (is_array($str)) {
200 if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1])) {
201 $out .= 'msgid_plural ' . $base_msgids[$msgid][1] . "\n";
203 $out .= 'msgid_plural ' . $msgid . "\n";
204 $warnings .= "[W] No source plural form for msgid:\n" . str_replace("\n", "\n\t", $msgid) . "\n\n";
206 foreach ($str as $n => $msgstr) {
207 $out .= 'msgstr[' . $n . '] ' . $this->massageString($msgstr) . "\n";
210 $out .= 'msgstr ' . $this->massageString($str) . "\n";
216 if (!file_put_contents($pofile, $out)) {
217 throw new \RuntimeException('Unable to write to ' . $pofile);
220 if ($warnings != '') {
221 $this->out($warnings);
227 private function startsWith($haystack, $needle)
229 // search backwards starting from haystack length characters from the end
230 return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
234 * Get a string and retun a message.po ready text
235 * - replace " with \"
236 * - replace tab char with \t
237 * - manage multiline strings
242 private function massageString($str)
244 $str = str_replace('\\', '\\\\', $str);
245 $str = str_replace('"', '\"', $str);
246 $str = str_replace("\t", '\t', $str);
247 $str = str_replace("\n", '\n"' . "\n" . '"', $str);
248 if (strpos($str, "\n") !== false && $str[0] !== '"') {
249 $str = '"' . "\n" . $str;
252 $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
253 return sprintf('"%s"', $str);
256 private function findOriginalMsgId($str)
258 $norm_str = preg_replace(self::NORM_REGEXP, "", $str);
259 if (array_key_exists($norm_str, $this->normBaseMsgIds)) {
260 return $this->normBaseMsgIds[$norm_str];