-<?php\r
-\r
-namespace Friendica\Core\Console;\r
-\r
-/**\r
- * Read a strings.php file and create messages.po in the same directory\r
- *\r
- * @author Hypolite Petovan <mrpetovan@gmail.com>\r
- */\r
-class PhpToPo extends \Asika\SimpleConsole\Console\r
-{\r
-\r
- protected $helpOptions = ['h', 'help', '?'];\r
-\r
- private $normBaseMsgIds = [];\r
- const NORM_REGEXP = "|[\\\]|";\r
-\r
- protected function getHelp()\r
- {\r
- $help = <<<HELP\r
-console php2po - Generate a messages.po file from a string.php file\r
-Usage\r
- bin/console php2po [-p <n>] <path/to/strings.php> [-h|--help|-?] [-v]\r
-\r
-Options:\r
- -p <n> Number of plural forms/ Default: 2\r
-\r
-Description\r
- Read a strings.php file and create the according messages.po in the same directory\r
-\r
-Options\r
- -h|--help|-? Show help information\r
- -v Show more debug information.\r
-HELP;\r
- return $help;\r
- }\r
-\r
- protected function doExecute()\r
- {\r
- if ($this->getOption('v')) {\r
- $this->out('Class: ' . __CLASS__);\r
- $this->out('Arguments: ' . var_export($this->args, true));\r
- $this->out('Options: ' . var_export($this->options, true));\r
- }\r
-\r
- if (count($this->args) == 0) {\r
- $this->out($this->getHelp());\r
- return 0;\r
- }\r
-\r
- if (count($this->args) > 1) {\r
- throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');\r
- }\r
-\r
- $a = get_app();\r
-\r
- $phpfile = realpath($this->getArgument(0));\r
-\r
- if (!file_exists($phpfile)) {\r
- throw new \RuntimeException('Supplied file path doesn\'t exist.');\r
- }\r
-\r
- if (!is_writable(dirname($phpfile))) {\r
- throw new \RuntimeException('Supplied directory isn\'t writable.');\r
- }\r
-\r
- $pofile = dirname($phpfile) . '/messages.po';\r
-\r
- // start !\r
- include_once($phpfile);\r
-\r
- $out = '';\r
- $out .= "# FRIENDICA Distributed Social Network\n";\r
- $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";\r
- $out .= "# This file is distributed under the same license as the Friendica package.\n";\r
- $out .= "# \n";\r
- $out .= 'msgid ""' . "\n";\r
- $out .= 'msgstr ""' . "\n";\r
- $out .= '"Project-Id-Version: friendica\n"' . "\n";\r
- $out .= '"Report-Msgid-Bugs-To: \n"' . "\n";\r
- $out .= '"POT-Creation-Date: ' . date("Y-m-d H:i:sO") . '\n"' . "\n";\r
- $out .= '"MIME-Version: 1.0\n"' . "\n";\r
- $out .= '"Content-Type: text/plain; charset=UTF-8\n"' . "\n";\r
- $out .= '"Content-Transfer-Encoding: 8bit\n"' . "\n";\r
-\r
- // search for plural info\r
- $lang = "";\r
- $lang_logic = "";\r
- $lang_pnum = 2;\r
-\r
- $_idx = array_search('-p', $argv);\r
- if ($_idx !== false) {\r
- $lang_pnum = $argv[$_idx + 1];\r
- }\r
-\r
- $infile = file($phpfile);\r
- foreach ($infile as $l) {\r
- $l = trim($l);\r
- if ($this->startsWith($l, 'function string_plural_select_')) {\r
- $lang = str_replace('function string_plural_select_', '', str_replace('($n){', '', $l));\r
- }\r
- if ($this->startsWith($l, 'return')) {\r
- $lang_logic = str_replace('$', '', trim(str_replace('return ', '', $l), ';'));\r
- break;\r
- }\r
- }\r
-\r
- $this->out('Language: ' . $lang);\r
- $this->out('Plural forms: ' . $lang_pnum);\r
- $this->out('Plural forms: ' . $lang_logic);\r
-\r
- $out .= sprintf('"Language: %s\n"', $lang) . "\n";\r
- $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n";\r
- $out .= "\n";\r
-\r
- $this->out('Loading base message.po...');\r
-\r
- // load base messages.po and extract msgids\r
- $base_msgids = [];\r
- $base_f = file("util/messages.po");\r
- if (!$base_f) {\r
- throw new \RuntimeException('The base util/messages.po file is missing.');\r
- }\r
-\r
- $_f = 0;\r
- $_mid = "";\r
- $_mids = [];\r
- foreach ($base_f as $l) {\r
- $l = trim($l);\r
-\r
- if ($this->startsWith($l, 'msgstr')) {\r
- if ($_mid != '""') {\r
- $base_msgids[$_mid] = $_mids;\r
- $this->normBaseMsgIds[preg_replace(self::NORM_REGEXP, "", $_mid)] = $_mid;\r
- }\r
-\r
- $_f = 0;\r
- $_mid = "";\r
- $_mids = [];\r
- }\r
-\r
- if ($this->startsWith($l, '"') && $_f == 2) {\r
- $_mids[count($_mids) - 1] .= "\n" . $l;\r
- }\r
- if ($this->startsWith($l, 'msgid_plural ')) {\r
- $_f = 2;\r
- $_mids[] = str_replace('msgid_plural ', '', $l);\r
- }\r
-\r
- if ($this->startsWith($l, '"') && $_f == 1) {\r
- $_mid .= "\n" . $l;\r
- $_mids[count($_mids) - 1] .= "\n" . $l;\r
- }\r
- if ($this->startsWith($l, 'msgid ')) {\r
- $_f = 1;\r
- $_mid = str_replace('msgid ', '', $l);\r
- $_mids = [$_mid];\r
- }\r
- }\r
-\r
- $this->out('Done.');\r
- $this->out('Creating ' . $pofile . '...');\r
-\r
- // create msgid and msgstr\r
- $warnings = "";\r
- foreach ($a->strings as $key => $str) {\r
- $msgid = $this->massageString($key);\r
-\r
- if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {\r
- $out .= "#, php-format\n";\r
- }\r
- $msgid = $this->findOriginalMsgId($msgid);\r
- $out .= 'msgid ' . $msgid . "\n";\r
-\r
- if (is_array($str)) {\r
- if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1])) {\r
- $out .= 'msgid_plural ' . $base_msgids[$msgid][1] . "\n";\r
- } else {\r
- $out .= 'msgid_plural ' . $msgid . "\n";\r
- $warnings .= "[W] No source plural form for msgid:\n" . str_replace("\n", "\n\t", $msgid) . "\n\n";\r
- }\r
- foreach ($str as $n => $msgstr) {\r
- $out .= 'msgstr[' . $n . '] ' . $this->massageString($msgstr) . "\n";\r
- }\r
- } else {\r
- $out .= 'msgstr ' . $this->massageString($str) . "\n";\r
- }\r
-\r
- $out .= "\n";\r
- }\r
-\r
- file_put_contents($pofile, $out);\r
-\r
- $this->out('Done.');\r
-\r
- if ($warnings == "") {\r
- $this->out('No warnings.');\r
- } else {\r
- $this->out($warnings);\r
- }\r
-\r
- return 0;\r
- }\r
-\r
- private function startsWith($haystack, $needle)\r
- {\r
- // search backwards starting from haystack length characters from the end\r
- return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;\r
- }\r
-\r
- /**\r
- * Get a string and retun a message.po ready text\r
- * - replace " with \"\r
- * - replace tab char with \t\r
- * - manage multiline strings\r
- */\r
- private function massageString($str)\r
- {\r
- $str = str_replace('\\', '\\\\', $str);\r
- $str = str_replace('"', '\"', $str);\r
- $str = str_replace("\t", '\t', $str);\r
- $str = str_replace("\n", '\n"' . "\n" . '"', $str);\r
- if (strpos($str, "\n") !== false && $str[0] !== '"') {\r
- $str = '"' . "\n" . $str;\r
- }\r
-\r
- $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);\r
- return sprintf('"%s"', $str);\r
- }\r
-\r
- private function findOriginalMsgId($str)\r
- {\r
- $norm_str = preg_replace(self::NORM_REGEXP, "", $str);\r
- if (array_key_exists($norm_str, $this->normBaseMsgIds)) {\r
- return $this->normBaseMsgIds[$norm_str];\r
- }\r
-\r
- return $str;\r
- }\r
-\r
-}\r
+<?php
+
+namespace Friendica\Core\Console;
+
+/**
+ * Read a strings.php file and create messages.po in the same directory
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+class PhpToPo extends \Asika\SimpleConsole\Console
+{
+
+ protected $helpOptions = ['h', 'help', '?'];
+
+ private $normBaseMsgIds = [];
+ const NORM_REGEXP = "|[\\\]|";
+
+ protected function getHelp()
+ {
+ $help = <<<HELP
+console php2po - Generate a messages.po file from a strings.php file
+Usage
+ bin/console php2po [-p <n>] [--base <file>] <path/to/strings.php> [-h|--help|-?] [-v]
+
+Description
+ Read a strings.php file and create the according messages.po in the same directory
+
+Options
+ -p <n> Number of plural forms. Default: 2
+ --base <file> Path to base messages.po file. Default: util/messages.po
+ -h|--help|-? Show help information
+ -v Show more debug information.
+HELP;
+ return $help;
+ }
+
+ protected function doExecute()
+ {
+ if ($this->getOption('v')) {
+ $this->out('Class: ' . __CLASS__);
+ $this->out('Arguments: ' . var_export($this->args, true));
+ $this->out('Options: ' . var_export($this->options, true));
+ }
+
+ if (count($this->args) == 0) {
+ $this->out($this->getHelp());
+ return 0;
+ }
+
+ if (count($this->args) > 1) {
+ throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
+ }
+
+ $a = get_app();
+
+ $phpfile = realpath($this->getArgument(0));
+
+ if (!file_exists($phpfile)) {
+ throw new \RuntimeException('Supplied file path doesn\'t exist.');
+ }
+
+ if (!is_writable(dirname($phpfile))) {
+ throw new \RuntimeException('Supplied directory isn\'t writable.');
+ }
+
+ $pofile = dirname($phpfile) . DIRECTORY_SEPARATOR . 'messages.po';
+
+ // start !
+ include_once($phpfile);
+
+ $out = '';
+ $out .= "# FRIENDICA Distributed Social Network\n";
+ $out .= "# Copyright (C) 2010, 2011, 2012, 2013 the Friendica Project\n";
+ $out .= "# This file is distributed under the same license as the Friendica package.\n";
+ $out .= "# \n";
+ $out .= 'msgid ""' . "\n";
+ $out .= 'msgstr ""' . "\n";
+ $out .= '"Project-Id-Version: friendica\n"' . "\n";
+ $out .= '"Report-Msgid-Bugs-To: \n"' . "\n";
+ $out .= '"POT-Creation-Date: ' . date("Y-m-d H:i:sO") . '\n"' . "\n";
+ $out .= '"MIME-Version: 1.0\n"' . "\n";
+ $out .= '"Content-Type: text/plain; charset=UTF-8\n"' . "\n";
+ $out .= '"Content-Transfer-Encoding: 8bit\n"' . "\n";
+
+ // search for plural info
+ $lang = "";
+ $lang_logic = "";
+ $lang_pnum = $this->getOption('p', 2);
+
+ $infile = file($phpfile);
+ foreach ($infile as $l) {
+ $l = trim($l);
+ if ($this->startsWith($l, 'function string_plural_select_')) {
+ $lang = str_replace('function string_plural_select_', '', str_replace('($n){', '', $l));
+ }
+ if ($this->startsWith($l, 'return')) {
+ $lang_logic = str_replace('$', '', trim(str_replace('return ', '', $l), ';'));
+ break;
+ }
+ }
+
+ $this->out('Language: ' . $lang);
+ $this->out('Plural forms: ' . $lang_pnum);
+ $this->out('Plural forms: ' . $lang_logic);
+
+ $out .= sprintf('"Language: %s\n"', $lang) . "\n";
+ $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n";
+ $out .= "\n";
+
+ $base_path = $this->getOption('base', 'util' . DIRECTORY_SEPARATOR . 'messages.po');
+
+ // load base messages.po and extract msgids
+ $base_msgids = [];
+ $base_f = file($base_path);
+ if (!$base_f) {
+ throw new \RuntimeException('The base ' . $base_path . ' file is missing or unavailable to read.');
+ }
+
+ $this->out('Loading base file ' . $base_path . '...');
+
+ $_f = 0;
+ $_mid = "";
+ $_mids = [];
+ foreach ($base_f as $l) {
+ $l = trim($l);
+
+ if ($this->startsWith($l, 'msgstr')) {
+ if ($_mid != '""') {
+ $base_msgids[$_mid] = $_mids;
+ $this->normBaseMsgIds[preg_replace(self::NORM_REGEXP, "", $_mid)] = $_mid;
+ }
+
+ $_f = 0;
+ $_mid = "";
+ $_mids = [];
+ }
+
+ if ($this->startsWith($l, '"') && $_f == 2) {
+ $_mids[count($_mids) - 1] .= "\n" . $l;
+ }
+ if ($this->startsWith($l, 'msgid_plural ')) {
+ $_f = 2;
+ $_mids[] = str_replace('msgid_plural ', '', $l);
+ }
+
+ if ($this->startsWith($l, '"') && $_f == 1) {
+ $_mid .= "\n" . $l;
+ $_mids[count($_mids) - 1] .= "\n" . $l;
+ }
+ if ($this->startsWith($l, 'msgid ')) {
+ $_f = 1;
+ $_mid = str_replace('msgid ', '', $l);
+ $_mids = [$_mid];
+ }
+ }
+
+ $this->out('Creating ' . $pofile . '...');
+
+ // create msgid and msgstr
+ $warnings = "";
+ foreach ($a->strings as $key => $str) {
+ $msgid = $this->massageString($key);
+
+ if (preg_match("|%[sd0-9](\$[sn])*|", $msgid)) {
+ $out .= "#, php-format\n";
+ }
+ $msgid = $this->findOriginalMsgId($msgid);
+ $out .= 'msgid ' . $msgid . "\n";
+
+ if (is_array($str)) {
+ if (array_key_exists($msgid, $base_msgids) && isset($base_msgids[$msgid][1])) {
+ $out .= 'msgid_plural ' . $base_msgids[$msgid][1] . "\n";
+ } else {
+ $out .= 'msgid_plural ' . $msgid . "\n";
+ $warnings .= "[W] No source plural form for msgid:\n" . str_replace("\n", "\n\t", $msgid) . "\n\n";
+ }
+ foreach ($str as $n => $msgstr) {
+ $out .= 'msgstr[' . $n . '] ' . $this->massageString($msgstr) . "\n";
+ }
+ } else {
+ $out .= 'msgstr ' . $this->massageString($str) . "\n";
+ }
+
+ $out .= "\n";
+ }
+
+ if (!file_put_contents($pofile, $out)) {
+ throw new \RuntimeException('Unable to write to ' . $pofile);
+ }
+
+ if ($warnings != '') {
+ $this->out($warnings);
+ }
+
+ return 0;
+ }
+
+ private function startsWith($haystack, $needle)
+ {
+ // search backwards starting from haystack length characters from the end
+ return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
+ }
+
+ /**
+ * Get a string and retun a message.po ready text
+ * - replace " with \"
+ * - replace tab char with \t
+ * - manage multiline strings
+ */
+ private function massageString($str)
+ {
+ $str = str_replace('\\', '\\\\', $str);
+ $str = str_replace('"', '\"', $str);
+ $str = str_replace("\t", '\t', $str);
+ $str = str_replace("\n", '\n"' . "\n" . '"', $str);
+ if (strpos($str, "\n") !== false && $str[0] !== '"') {
+ $str = '"' . "\n" . $str;
+ }
+
+ $str = preg_replace("|\n([^\"])|", "\n\"$1", $str);
+ return sprintf('"%s"', $str);
+ }
+
+ private function findOriginalMsgId($str)
+ {
+ $norm_str = preg_replace(self::NORM_REGEXP, "", $str);
+ if (array_key_exists($norm_str, $this->normBaseMsgIds)) {
+ return $this->normBaseMsgIds[$norm_str];
+ }
+
+ return $str;
+ }
+
+}