3 namespace Friendica\Core\Console;
6 * Read a strings.php file and create messages.po in the same directory
8 * @author Hypolite Petovan <hypolite@mrpetovan.com>
10 class PhpToPo extends \Asika\SimpleConsole\Console
13 protected $helpOptions = ['h', 'help', '?'];
15 private $normBaseMsgIds = [];
16 const NORM_REGEXP = "|[\\\]|";
18 protected function getHelp()
21 console php2po - Generate a messages.po file from a strings.php file
23 bin/console php2po [-p <n>] [--base <file>] <path/to/strings.php> [-h|--help|-?] [-v]
26 Read a strings.php file and create the according messages.po in the same directory
29 -p <n> Number of plural forms. Default: 2
30 --base <file> Path to base messages.po file. Default: view/lang/C/messages.po
31 -h|--help|-? Show help information
32 -v Show more debug information.
37 protected function doExecute()
39 if ($this->getOption('v')) {
40 $this->out('Class: ' . __CLASS__);
41 $this->out('Arguments: ' . var_export($this->args, true));
42 $this->out('Options: ' . var_export($this->options, true));
45 if (count($this->args) == 0) {
46 $this->out($this->getHelp());
50 if (count($this->args) > 1) {
51 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
56 $phpfile = realpath($this->getArgument(0));
58 if (!file_exists($phpfile)) {
59 throw new \RuntimeException('Supplied file path doesn\'t exist.');
62 if (!is_writable(dirname($phpfile))) {
63 throw new \RuntimeException('Supplied directory isn\'t writable.');
66 $pofile = dirname($phpfile) . DIRECTORY_SEPARATOR . 'messages.po';
69 include_once($phpfile);
72 $out .= "# FRIENDICA Distributed Social Network\n";
73 $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
74 $out .= "# This file is distributed under the same license as the Friendica package.\n";
76 $out .= 'msgid ""' . "\n";
77 $out .= 'msgstr ""' . "\n";
78 $out .= '"Project-Id-Version: friendica\n"' . "\n";
79 $out .= '"Report-Msgid-Bugs-To: \n"' . "\n";
80 $out .= '"POT-Creation-Date: ' . date("Y-m-d H:i:sO") . '\n"' . "\n";
81 $out .= '"MIME-Version: 1.0\n"' . "\n";
82 $out .= '"Content-Type: text/plain; charset=UTF-8\n"' . "\n";
83 $out .= '"Content-Transfer-Encoding: 8bit\n"' . "\n";
85 // search for plural info
88 $lang_pnum = $this->getOption('p', 2);
90 $infile = file($phpfile);
91 foreach ($infile as $l) {
93 if ($this->startsWith($l, 'function string_plural_select_')) {
94 $lang = str_replace('function string_plural_select_', '', str_replace('($n){', '', $l));
96 if ($this->startsWith($l, 'return')) {
97 $lang_logic = str_replace('$', '', trim(str_replace('return ', '', $l), ';'));
102 $this->out('Language: ' . $lang);
103 $this->out('Plural forms: ' . $lang_pnum);
104 $this->out('Plural forms: ' . $lang_logic);
106 $out .= sprintf('"Language: %s\n"', $lang) . "\n";
107 $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n";
110 $base_path = $this->getOption('base', 'view/lang/C/messages.po');
112 // load base messages.po and extract msgids
114 $base_f = file($base_path);
116 throw new \RuntimeException('The base ' . $base_path . ' file is missing or unavailable to read.');
119 $this->out('Loading base file ' . $base_path . '...');
124 foreach ($base_f as $l) {
127 if ($this->startsWith($l, 'msgstr')) {
129 $base_msgids[$_mid] = $_mids;
130 $this->normBaseMsgIds[preg_replace(self::NORM_REGEXP, "", $_mid)] = $_mid;
138 if ($this->startsWith($l, '"') && $_f == 2) {
139 $_mids[count($_mids) - 1] .= "\n" . $l;
141 if ($this->startsWith($l, 'msgid_plural ')) {
143 $_mids[] = str_replace('msgid_plural ', '', $l);
146 if ($this->startsWith($l, '"') && $_f == 1) {
148 $_mids[count($_mids) - 1] .= "\n" . $l;
150 if ($this->startsWith($l, 'msgid ')) {
152 $_mid = str_replace('msgid ', '', $l);
157 $this->out('Creating ' . $pofile . '...');
159 // create msgid and msgstr
161 foreach ($a->strings as $key => $str) {
162 $msgid = $this->massageString($key);
164 if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
165 $out .= "#, php-format\n";
167 $msgid = $this->findOriginalMsgId($msgid);
168 $out .= 'msgid ' . $msgid . "\n";
170 if (is_array($str)) {
171 if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1])) {
172 $out .= 'msgid_plural ' . $base_msgids[$msgid][1] . "\n";
174 $out .= 'msgid_plural ' . $msgid . "\n";
175 $warnings .= "[W] No source plural form for msgid:\n" . str_replace("\n", "\n\t", $msgid) . "\n\n";
177 foreach ($str as $n => $msgstr) {
178 $out .= 'msgstr[' . $n . '] ' . $this->massageString($msgstr) . "\n";
181 $out .= 'msgstr ' . $this->massageString($str) . "\n";
187 if (!file_put_contents($pofile, $out)) {
188 throw new \RuntimeException('Unable to write to ' . $pofile);
191 if ($warnings != '') {
192 $this->out($warnings);
198 private function startsWith($haystack, $needle)
200 // search backwards starting from haystack length characters from the end
201 return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
205 * Get a string and retun a message.po ready text
206 * - replace " with \"
207 * - replace tab char with \t
208 * - manage multiline strings
213 private function massageString($str)
215 $str = str_replace('\\', '\\\\', $str);
216 $str = str_replace('"', '\"', $str);
217 $str = str_replace("\t", '\t', $str);
218 $str = str_replace("\n", '\n"' . "\n" . '"', $str);
219 if (strpos($str, "\n") !== false && $str[0] !== '"') {
220 $str = '"' . "\n" . $str;
223 $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
224 return sprintf('"%s"', $str);
227 private function findOriginalMsgId($str)
229 $norm_str = preg_replace(self::NORM_REGEXP, "", $str);
230 if (array_key_exists($norm_str, $this->normBaseMsgIds)) {
231 return $this->normBaseMsgIds[$norm_str];