Skip to content

Commit 2a99cd7

Browse files
committed
Allow exporting formulas with and without extra parenthesis
1 parent 8b32f07 commit 2a99cd7

8 files changed

Lines changed: 101 additions & 61 deletions

File tree

bin/export-plural-rules

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ class Enviro
5656

5757
/**
5858
* Omit extra parenthesis in plural rule formulas.
59+
* If null: formulas will be exported with and without extra parenthesis (if supported by the exporter).
5960
*
60-
* @var bool
61+
* @var bool|null
6162
*/
62-
public static $noExtraParenthesis;
63+
public static $extraParenthesis;
6364

6465
/**
6566
* Parse the command line options.
@@ -72,7 +73,7 @@ class Enviro
7273
self::$outputFilename = null;
7374
self::$languages = null;
7475
self::$reduce = null;
75-
self::$noExtraParenthesis = false;
76+
self::$extraParenthesis = true;
7677
$exporters = Exporter::getExporters();
7778
if (isset($argv) && is_array($argv)) {
7879
foreach ($argv as $argi => $arg) {
@@ -96,10 +97,13 @@ class Enviro
9697
self::$reduce = false;
9798
break;
9899
case '--parenthesis=yes':
99-
self::$noExtraParenthesis = false;
100+
self::$extraParenthesis = true;
100101
break;
101102
case '--parenthesis=no':
102-
self::$noExtraParenthesis = true;
103+
self::$extraParenthesis = false;
104+
break;
105+
case '--parenthesis=both':
106+
self::$extraParenthesis = null;
103107
break;
104108
default:
105109
if (preg_match('/^--output=.+$/', $argLC)) {
@@ -185,6 +189,8 @@ Where:
185189
plural rules formulas.
186190
Those extra parenthesis are needed to create a PHP-compatible
187191
formula.
192+
Some exporter may also export formulas both with and without
193+
The extra parenthesis: use --parenthesis=both in this case
188194
Defaults to 'yes'
189195
--output
190196
if specified, the output will be saved to <file name>. If not
@@ -265,7 +271,7 @@ try {
265271
if (Enviro::$reduce) {
266272
$languages = Enviro::reduce($languages);
267273
}
268-
if (Enviro::$noExtraParenthesis) {
274+
if (Enviro::$extraParenthesis === false) {
269275
$languages = array_map(
270276
function (Language $language) {
271277
$language->formula = $language->buildFormula(true);
@@ -275,10 +281,18 @@ try {
275281
$languages
276282
);
277283
}
284+
$exporterClass = Exporter::getExporterClassName(Enviro::$outputFormat);
285+
$options = array(
286+
'us-ascii' => Enviro::$outputUSAscii,
287+
'both-formulas' => Enviro::$extraParenthesis === null,
288+
);
289+
if ($options['both-formulas'] && !call_user_func(array($exporterClass, 'supportsFormulasWithAndWithoutParenthesis'))) {
290+
throw new Exception("The selected exporter doesn't support exporting data with and without extra paranthesis");
291+
}
278292
if (isset(Enviro::$outputFilename)) {
279-
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toFile'), $languages, Enviro::$outputFilename, array('us-ascii' => Enviro::$outputUSAscii));
293+
echo call_user_func(array($exporterClass, 'toFile'), $languages, Enviro::$outputFilename, $options);
280294
} else {
281-
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toString'), $languages, array('us-ascii' => Enviro::$outputUSAscii));
295+
echo call_user_func(array($exporterClass, 'toString'), $languages, $options);
282296
}
283297
} catch (Exception $x) {
284298
fwrite(STDERR, $x->getMessage() . "\n");

src/Exporter/Exporter.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@ final public static function getExporterClassName($exporterHandle)
9090
*/
9191
final public static function toString($languages, $options = null)
9292
{
93-
if (isset($options) && is_array($options)) {
94-
if (isset($options['us-ascii']) && $options['us-ascii']) {
95-
$asciiList = array();
96-
foreach ($languages as $language) {
97-
$asciiList[] = $language->getUSAsciiClone();
98-
}
99-
$languages = $asciiList;
93+
if (!isset($options) || !is_array($options)) {
94+
$options = array();
95+
}
96+
if (isset($options['us-ascii']) && $options['us-ascii']) {
97+
$asciiList = array();
98+
foreach ($languages as $language) {
99+
$asciiList[] = $language->getUSAsciiClone();
100100
}
101+
$languages = $asciiList;
101102
}
102103

103-
return static::toStringDo($languages);
104+
return static::toStringDoWithOptions($languages, $options);
104105
}
105106

106107
/**
@@ -129,6 +130,16 @@ public static function isForPublicUse()
129130
return true;
130131
}
131132

133+
/**
134+
* Does this exporter supports exporting formulas both with and without extra parenthesis?
135+
*
136+
* @return bool
137+
*/
138+
public static function supportsFormulasWithAndWithoutParenthesis()
139+
{
140+
return false;
141+
}
142+
132143
/**
133144
* Return a short description of the exporter.
134145
*
@@ -143,11 +154,15 @@ public static function getDescription()
143154
* Convert a list of Language instances to string.
144155
*
145156
* @param \Gettext\Languages\Language[] $languages the Language instances to convert
157+
* @param array $options export options
146158
*
147159
* @return string
148160
*/
149-
protected static function toStringDo($languages)
161+
protected static function toStringDoWithOptions($languages, array $options)
150162
{
163+
if (method_exists(get_called_class(), 'toStringDo')) {
164+
return static::toStringDo($languages);
165+
}
151166
throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__);
152167
}
153168
}

src/Exporter/Html.php

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,47 @@ public static function getDescription()
1717
/**
1818
* {@inheritdoc}
1919
*
20-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
20+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
2121
*/
22-
protected static function toStringDo($languages)
22+
protected static function toStringDoWithOptions($languages, array $options)
2323
{
24-
return self::buildTable($languages, false);
25-
}
26-
27-
protected static function h($str)
28-
{
29-
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
30-
}
31-
32-
protected static function buildTable($languages, $forDocs)
33-
{
34-
$prefix = $forDocs ? ' ' : '';
3524
$lines = array();
36-
$lines[] = $prefix . '<table' . ($forDocs ? ' class="table table-bordered table-condensed table-striped"' : '') . '>';
37-
$lines[] = $prefix . ' <thead>';
38-
$lines[] = $prefix . ' <tr>';
39-
$lines[] = $prefix . ' <th>Language code</th>';
40-
$lines[] = $prefix . ' <th>Language name</th>';
41-
$lines[] = $prefix . ' <th># plurals</th>';
42-
$lines[] = $prefix . ' <th>Formula</th>';
43-
$lines[] = $prefix . ' <th>Plurals</th>';
44-
$lines[] = $prefix . ' </tr>';
45-
$lines[] = $prefix . ' </thead>';
46-
$lines[] = $prefix . ' <tbody>';
25+
$lines[] = '<table>';
26+
$lines[] = ' <thead>';
27+
$lines[] = ' <tr>';
28+
$lines[] = ' <th>Language code</th>';
29+
$lines[] = ' <th>Language name</th>';
30+
$lines[] = ' <th># plurals</th>';
31+
$lines[] = ' <th>Formula</th>';
32+
$lines[] = ' <th>Plurals</th>';
33+
$lines[] = ' </tr>';
34+
$lines[] = ' </thead>';
35+
$lines[] = ' <tbody>';
4736
foreach ($languages as $lc) {
48-
$lines[] = $prefix . ' <tr>';
49-
$lines[] = $prefix . ' <td>' . $lc->id . '</td>';
37+
$lines[] = ' <tr>';
38+
$lines[] = ' <td>' . $lc->id . '</td>';
5039
$name = self::h($lc->name);
5140
if (isset($lc->supersededBy)) {
5241
$name .= '<br /><small><span>Superseded by</span> ' . $lc->supersededBy . '</small>';
5342
}
54-
$lines[] = $prefix . ' <td>' . $name . '</td>';
55-
$lines[] = $prefix . ' <td>' . count($lc->categories) . '</td>';
56-
$lines[] = $prefix . ' <td>' . self::h($lc->formula) . '</td>';
43+
$lines[] = ' <td>' . $name . '</td>';
44+
$lines[] = ' <td>' . count($lc->categories) . '</td>';
45+
$lines[] = ' <td>' . self::h($lc->formula) . '</td>';
5746
$cases = array();
5847
foreach ($lc->categories as $c) {
5948
$cases[] = '<li><span>' . $c->id . '</span><code>' . self::h($c->examples) . '</code></li>';
6049
}
61-
$lines[] = $prefix . ' <td><ol' . ($forDocs ? ' class="cases"' : '') . ' start="0">' . implode('', $cases) . '</ol></td>';
62-
$lines[] = $prefix . ' </tr>';
50+
$lines[] = ' <td><ol start="0">' . implode('', $cases) . '</ol></td>';
51+
$lines[] = ' </tr>';
6352
}
64-
$lines[] = $prefix . ' </tbody>';
65-
$lines[] = $prefix . '</table>';
53+
$lines[] = ' </tbody>';
54+
$lines[] = '</table>';
6655

6756
return implode("\n", $lines);
6857
}
58+
59+
protected static function h($str)
60+
{
61+
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
62+
}
6963
}

src/Exporter/Json.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public static function getDescription()
1414
return 'Build a compressed JSON-encoded file';
1515
}
1616

17+
/**
18+
* {@inheritdoc}
19+
*
20+
* @see \Gettext\Languages\Exporter\Exporter::supportsFormulasWithAndWithoutParenthesis()
21+
*/
22+
public static function supportsFormulasWithAndWithoutParenthesis()
23+
{
24+
return true;
25+
}
26+
1727
/**
1828
* Return the options for json_encode.
1929
*
@@ -35,9 +45,9 @@ protected static function getEncodeOptions()
3545
/**
3646
* {@inheritdoc}
3747
*
38-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
48+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
3949
*/
40-
protected static function toStringDo($languages)
50+
protected static function toStringDoWithOptions($languages, array $options)
4151
{
4252
$list = array();
4353
foreach ($languages as $language) {
@@ -55,7 +65,14 @@ protected static function toStringDo($languages)
5565
if (isset($language->baseLanguage)) {
5666
$item['baseLanguage'] = $language->baseLanguage;
5767
}
58-
$item['formula'] = $language->formula;
68+
if (!empty($options['both-formulas'])) {
69+
$item['formulas'] = array(
70+
'standard' => $language->buildFormula(true),
71+
'php' => $language->formula,
72+
);
73+
} else {
74+
$item['formula'] = $language->formula;
75+
}
5976
$item['plurals'] = count($language->categories);
6077
$item['cases'] = array();
6178
$item['examples'] = array();

src/Exporter/Php.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static function getDescription()
1717
/**
1818
* {@inheritdoc}
1919
*
20-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
20+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
2121
*/
22-
protected static function toStringDo($languages)
22+
protected static function toStringDoWithOptions($languages, array $options)
2323
{
2424
$lines = array();
2525
$lines[] = '<?php';

src/Exporter/Po.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public static function getDescription()
1919
/**
2020
* {@inheritdoc}
2121
*
22-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
22+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
2323
*/
24-
protected static function toStringDo($languages)
24+
protected static function toStringDoWithOptions($languages, array $options)
2525
{
2626
if (count($languages) !== 1) {
2727
throw new Exception('The ' . get_called_class() . ' exporter can only export one language');

src/Exporter/Ruby.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static function getDescription()
1717
/**
1818
* {@inheritdoc}
1919
*
20-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
20+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
2121
*/
22-
protected static function toStringDo($languages)
22+
protected static function toStringDoWithOptions($languages, array $options)
2323
{
2424
$lines = array();
2525
$lines[] = 'PLURAL_RULES = {';

src/Exporter/Xml.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static function getDescription()
1717
/**
1818
* {@inheritdoc}
1919
*
20-
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
20+
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions()
2121
*/
22-
protected static function toStringDo($languages)
22+
protected static function toStringDoWithOptions($languages, array $options)
2323
{
2424
$xml = new \DOMDocument('1.0', 'UTF-8');
2525
$xml->loadXML('<languages

0 commit comments

Comments
 (0)