Improve Console\PhpToPo

- Add base option
- Use DIRECTORY_SEPARATOR
- Add error handling
- Remove superfluous display
This commit is contained in:
Hypolite Petovan 2018-03-18 19:07:24 -04:00
parent 0b542d0654
commit c9945ad15b

View file

@ -18,19 +18,18 @@ class PhpToPo extends \Asika\SimpleConsole\Console
protected function getHelp() protected function getHelp()
{ {
$help = <<<HELP $help = <<<HELP
console php2po - Generate a messages.po file from a string.php file console php2po - Generate a messages.po file from a strings.php file
Usage Usage
bin/console php2po [-p <n>] <path/to/strings.php> [-h|--help|-?] [-v] bin/console php2po [-p <n>] [--base <file>] <path/to/strings.php> [-h|--help|-?] [-v]
Options:
-p <n> Number of plural forms/ Default: 2
Description Description
Read a strings.php file and create the according messages.po in the same directory Read a strings.php file and create the according messages.po in the same directory
Options Options
-h|--help|-? Show help information -p <n> Number of plural forms. Default: 2
-v Show more debug information. --base <file> Path to base messages.po file. Default: util/messages.po
-h|--help|-? Show help information
-v Show more debug information.
HELP; HELP;
return $help; return $help;
} }
@ -64,7 +63,7 @@ HELP;
throw new \RuntimeException('Supplied directory isn\'t writable.'); throw new \RuntimeException('Supplied directory isn\'t writable.');
} }
$pofile = dirname($phpfile) . '/messages.po'; $pofile = dirname($phpfile) . DIRECTORY_SEPARATOR . 'messages.po';
// start ! // start !
include_once($phpfile); include_once($phpfile);
@ -86,12 +85,7 @@ HELP;
// search for plural info // search for plural info
$lang = ""; $lang = "";
$lang_logic = ""; $lang_logic = "";
$lang_pnum = 2; $lang_pnum = $this->getOption('p', 2);
$_idx = array_search('-p', $argv);
if ($_idx !== false) {
$lang_pnum = $argv[$_idx + 1];
}
$infile = file($phpfile); $infile = file($phpfile);
foreach ($infile as $l) { foreach ($infile as $l) {
@ -113,15 +107,17 @@ HELP;
$out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n"; $out .= sprintf('"Plural-Forms: nplurals=%s; plural=%s;\n"', $lang_pnum, $lang_logic) . "\n";
$out .= "\n"; $out .= "\n";
$this->out('Loading base message.po...'); $base_path = $this->getOption('base', 'util' . DIRECTORY_SEPARATOR . 'messages.po');
// load base messages.po and extract msgids // load base messages.po and extract msgids
$base_msgids = []; $base_msgids = [];
$base_f = file("util/messages.po"); $base_f = file($base_path);
if (!$base_f) { if (!$base_f) {
throw new \RuntimeException('The base util/messages.po file is missing.'); throw new \RuntimeException('The base ' . $base_path . ' file is missing or unavailable to read.');
} }
$this->out('Loading base file ' . $base_path . '...');
$_f = 0; $_f = 0;
$_mid = ""; $_mid = "";
$_mids = []; $_mids = [];
@ -158,7 +154,6 @@ HELP;
} }
} }
$this->out('Done.');
$this->out('Creating ' . $pofile . '...'); $this->out('Creating ' . $pofile . '...');
// create msgid and msgstr // create msgid and msgstr
@ -189,13 +184,11 @@ HELP;
$out .= "\n"; $out .= "\n";
} }
file_put_contents($pofile, $out); if (!file_put_contents($pofile, $out)) {
throw new \RuntimeException('Unable to write to ' . $pofile);
}
$this->out('Done.'); if ($warnings != '') {
if ($warnings == "") {
$this->out('No warnings.');
} else {
$this->out($warnings); $this->out($warnings);
} }