Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions bin/export-plural-rules
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 <file name>. If not
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down
33 changes: 24 additions & 9 deletions src/Exporter/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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__);
}
}
60 changes: 27 additions & 33 deletions src/Exporter/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '<table' . ($forDocs ? ' class="table table-bordered table-condensed table-striped"' : '') . '>';
$lines[] = $prefix . ' <thead>';
$lines[] = $prefix . ' <tr>';
$lines[] = $prefix . ' <th>Language code</th>';
$lines[] = $prefix . ' <th>Language name</th>';
$lines[] = $prefix . ' <th># plurals</th>';
$lines[] = $prefix . ' <th>Formula</th>';
$lines[] = $prefix . ' <th>Plurals</th>';
$lines[] = $prefix . ' </tr>';
$lines[] = $prefix . ' </thead>';
$lines[] = $prefix . ' <tbody>';
$lines[] = '<table>';
$lines[] = ' <thead>';
$lines[] = ' <tr>';
$lines[] = ' <th>Language code</th>';
$lines[] = ' <th>Language name</th>';
$lines[] = ' <th># plurals</th>';
$lines[] = ' <th>Formula</th>';
$lines[] = ' <th>Plurals</th>';
$lines[] = ' </tr>';
$lines[] = ' </thead>';
$lines[] = ' <tbody>';
foreach ($languages as $lc) {
$lines[] = $prefix . ' <tr>';
$lines[] = $prefix . ' <td>' . $lc->id . '</td>';
$lines[] = ' <tr>';
$lines[] = ' <td>' . $lc->id . '</td>';
$name = self::h($lc->name);
if (isset($lc->supersededBy)) {
$name .= '<br /><small><span>Superseded by</span> ' . $lc->supersededBy . '</small>';
}
$lines[] = $prefix . ' <td>' . $name . '</td>';
$lines[] = $prefix . ' <td>' . count($lc->categories) . '</td>';
$lines[] = $prefix . ' <td>' . self::h($lc->formula) . '</td>';
$lines[] = ' <td>' . $name . '</td>';
$lines[] = ' <td>' . count($lc->categories) . '</td>';
$lines[] = ' <td>' . self::h($lc->formula) . '</td>';
$cases = array();
foreach ($lc->categories as $c) {
$cases[] = '<li><span>' . $c->id . '</span><code>' . self::h($c->examples) . '</code></li>';
}
$lines[] = $prefix . ' <td><ol' . ($forDocs ? ' class="cases"' : '') . ' start="0">' . implode('', $cases) . '</ol></td>';
$lines[] = $prefix . ' </tr>';
$lines[] = ' <td><ol start="0">' . implode('', $cases) . '</ol></td>';
$lines[] = ' </tr>';
}
$lines[] = $prefix . ' </tbody>';
$lines[] = $prefix . '</table>';
$lines[] = ' </tbody>';
$lines[] = '</table>';

return implode("\n", $lines);
}

protected static function h($str)
{
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
}
}
23 changes: 20 additions & 3 deletions src/Exporter/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/Exporter/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = '<?php';
Expand Down
4 changes: 2 additions & 2 deletions src/Exporter/Po.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,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)
{
if (count($languages) !== 1) {
throw new Exception('The ' . get_called_class() . ' exporter can only export one language');
Expand Down
4 changes: 2 additions & 2 deletions src/Exporter/Ruby.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = 'PLURAL_RULES = {';
Expand Down
4 changes: 2 additions & 2 deletions src/Exporter/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
$xml = new \DOMDocument('1.0', 'UTF-8');
$xml->loadXML('<languages
Expand Down