diff --git a/bin/export-plural-rules b/bin/export-plural-rules index 0389e2e..102d44b 100755 --- a/bin/export-plural-rules +++ b/bin/export-plural-rules @@ -56,10 +56,11 @@ class Enviro /** * Omit extra parenthesis in plural rule formulas. + * If null: formulas will be exported with and without extra parenthesis (if supported by the exporter). * - * @var bool + * @var bool|null */ - public static $noExtraParenthesis; + public static $extraParenthesis; /** * Parse the command line options. @@ -72,7 +73,7 @@ class Enviro self::$outputFilename = null; self::$languages = null; self::$reduce = null; - self::$noExtraParenthesis = false; + self::$extraParenthesis = true; $exporters = Exporter::getExporters(); if (isset($argv) && is_array($argv)) { foreach ($argv as $argi => $arg) { @@ -96,10 +97,13 @@ class Enviro self::$reduce = false; break; case '--parenthesis=yes': - self::$noExtraParenthesis = false; + self::$extraParenthesis = true; break; case '--parenthesis=no': - self::$noExtraParenthesis = true; + self::$extraParenthesis = false; + break; + case '--parenthesis=both': + self::$extraParenthesis = null; break; default: if (preg_match('/^--output=.+$/', $argLC)) { @@ -185,6 +189,8 @@ Where: plural rules formulas. Those extra parenthesis are needed to create a PHP-compatible formula. + Some exporter may also export formulas both with and without + The extra parenthesis: use --parenthesis=both in this case Defaults to 'yes' --output if specified, the output will be saved to . If not @@ -265,7 +271,7 @@ try { if (Enviro::$reduce) { $languages = Enviro::reduce($languages); } - if (Enviro::$noExtraParenthesis) { + if (Enviro::$extraParenthesis === false) { $languages = array_map( function (Language $language) { $language->formula = $language->buildFormula(true); @@ -275,10 +281,18 @@ try { $languages ); } + $exporterClass = Exporter::getExporterClassName(Enviro::$outputFormat); + $options = array( + 'us-ascii' => Enviro::$outputUSAscii, + 'both-formulas' => Enviro::$extraParenthesis === null, + ); + if ($options['both-formulas'] && !call_user_func(array($exporterClass, 'supportsFormulasWithAndWithoutParenthesis'))) { + throw new Exception("The selected exporter doesn't support exporting data with and without extra paranthesis"); + } if (isset(Enviro::$outputFilename)) { - echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toFile'), $languages, Enviro::$outputFilename, array('us-ascii' => Enviro::$outputUSAscii)); + echo call_user_func(array($exporterClass, 'toFile'), $languages, Enviro::$outputFilename, $options); } else { - echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toString'), $languages, array('us-ascii' => Enviro::$outputUSAscii)); + echo call_user_func(array($exporterClass, 'toString'), $languages, $options); } } catch (Exception $x) { fwrite(STDERR, $x->getMessage() . "\n"); diff --git a/src/Exporter/Exporter.php b/src/Exporter/Exporter.php index 95a7112..eb249ba 100644 --- a/src/Exporter/Exporter.php +++ b/src/Exporter/Exporter.php @@ -90,17 +90,18 @@ final public static function getExporterClassName($exporterHandle) */ final public static function toString($languages, $options = null) { - if (isset($options) && is_array($options)) { - if (isset($options['us-ascii']) && $options['us-ascii']) { - $asciiList = array(); - foreach ($languages as $language) { - $asciiList[] = $language->getUSAsciiClone(); - } - $languages = $asciiList; + if (!isset($options) || !is_array($options)) { + $options = array(); + } + if (isset($options['us-ascii']) && $options['us-ascii']) { + $asciiList = array(); + foreach ($languages as $language) { + $asciiList[] = $language->getUSAsciiClone(); } + $languages = $asciiList; } - return static::toStringDo($languages); + return static::toStringDoWithOptions($languages, $options); } /** @@ -129,6 +130,16 @@ public static function isForPublicUse() return true; } + /** + * Does this exporter supports exporting formulas both with and without extra parenthesis? + * + * @return bool + */ + public static function supportsFormulasWithAndWithoutParenthesis() + { + return false; + } + /** * Return a short description of the exporter. * @@ -143,11 +154,15 @@ public static function getDescription() * Convert a list of Language instances to string. * * @param \Gettext\Languages\Language[] $languages the Language instances to convert + * @param array $options export options * * @return string */ - protected static function toStringDo($languages) + protected static function toStringDoWithOptions($languages, array $options) { + if (method_exists(get_called_class(), 'toStringDo')) { + return static::toStringDo($languages); + } throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__); } } diff --git a/src/Exporter/Html.php b/src/Exporter/Html.php index d16e782..5e9b90b 100644 --- a/src/Exporter/Html.php +++ b/src/Exporter/Html.php @@ -17,53 +17,47 @@ public static function getDescription() /** * {@inheritdoc} * - * @see \Gettext\Languages\Exporter\Exporter::toStringDo() + * @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions() */ - protected static function toStringDo($languages) + protected static function toStringDoWithOptions($languages, array $options) { - return self::buildTable($languages, false); - } - - protected static function h($str) - { - return htmlspecialchars($str, ENT_COMPAT, 'UTF-8'); - } - - protected static function buildTable($languages, $forDocs) - { - $prefix = $forDocs ? ' ' : ''; $lines = array(); - $lines[] = $prefix . ''; - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' Language code'; - $lines[] = $prefix . ' Language name'; - $lines[] = $prefix . ' # plurals'; - $lines[] = $prefix . ' Formula'; - $lines[] = $prefix . ' Plurals'; - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; + $lines[] = ''; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; foreach ($languages as $lc) { - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; + $lines[] = ' '; + $lines[] = ' '; $name = self::h($lc->name); if (isset($lc->supersededBy)) { $name .= '
Superseded by ' . $lc->supersededBy . ''; } - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; + $lines[] = ' '; + $lines[] = ' '; + $lines[] = ' '; $cases = array(); foreach ($lc->categories as $c) { $cases[] = '
  • ' . $c->id . '' . self::h($c->examples) . '
  • '; } - $lines[] = $prefix . ' '; - $lines[] = $prefix . ' '; + $lines[] = ' '; + $lines[] = ' '; } - $lines[] = $prefix . ' '; - $lines[] = $prefix . '
    Language codeLanguage name# pluralsFormulaPlurals
    ' . $lc->id . '
    ' . $lc->id . '' . $name . '' . count($lc->categories) . '' . self::h($lc->formula) . '' . $name . '' . count($lc->categories) . '' . self::h($lc->formula) . '' . implode('', $cases) . '
      ' . implode('', $cases) . '
    '; + $lines[] = ' '; + $lines[] = ''; return implode("\n", $lines); } + + protected static function h($str) + { + return htmlspecialchars($str, ENT_COMPAT, 'UTF-8'); + } } diff --git a/src/Exporter/Json.php b/src/Exporter/Json.php index 8ea57b8..a78ea11 100644 --- a/src/Exporter/Json.php +++ b/src/Exporter/Json.php @@ -14,6 +14,16 @@ public static function getDescription() return 'Build a compressed JSON-encoded file'; } + /** + * {@inheritdoc} + * + * @see \Gettext\Languages\Exporter\Exporter::supportsFormulasWithAndWithoutParenthesis() + */ + public static function supportsFormulasWithAndWithoutParenthesis() + { + return true; + } + /** * Return the options for json_encode. * @@ -35,9 +45,9 @@ protected static function getEncodeOptions() /** * {@inheritdoc} * - * @see \Gettext\Languages\Exporter\Exporter::toStringDo() + * @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions() */ - protected static function toStringDo($languages) + protected static function toStringDoWithOptions($languages, array $options) { $list = array(); foreach ($languages as $language) { @@ -55,7 +65,14 @@ protected static function toStringDo($languages) if (isset($language->baseLanguage)) { $item['baseLanguage'] = $language->baseLanguage; } - $item['formula'] = $language->formula; + if (!empty($options['both-formulas'])) { + $item['formulas'] = array( + 'standard' => $language->buildFormula(true), + 'php' => $language->formula, + ); + } else { + $item['formula'] = $language->formula; + } $item['plurals'] = count($language->categories); $item['cases'] = array(); $item['examples'] = array(); diff --git a/src/Exporter/Php.php b/src/Exporter/Php.php index d6b8d1b..d3cf7fe 100644 --- a/src/Exporter/Php.php +++ b/src/Exporter/Php.php @@ -17,9 +17,9 @@ public static function getDescription() /** * {@inheritdoc} * - * @see \Gettext\Languages\Exporter\Exporter::toStringDo() + * @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions() */ - protected static function toStringDo($languages) + protected static function toStringDoWithOptions($languages, array $options) { $lines = array(); $lines[] = 'loadXML('