Commit b5af2f6a by Carsten Brandt

fixed size format plural rule to not format number itself

This fix ensures the format configured in the formatter class is used in the message instead of the default ICU format. fixes #4960
parent 2150fa32
......@@ -60,10 +60,10 @@ class DataColumn extends Column
*/
public $value;
/**
* @var string|array in which format should the value of each data model be displayed as (e.g. "raw", "text", "html",
* ['date', 'Y-m-d']). Supported formats are determined by the [[GridView::formatter|formatter]] used by
* @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
* `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
* the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
* [[\yii\i18n\Formatter::format()]] or [[\yii\i18n\Formatter::format()]] is used.
* [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
*/
public $format = 'text';
/**
......
......@@ -1075,21 +1075,21 @@ class Formatter extends Component
if ($this->sizeFormatBase == 1024) {
switch ($position) {
case 0: return Yii::t('yii', '{n} B', $params, $this->locale);
case 1: return Yii::t('yii', '{n} KiB', $params, $this->locale);
case 2: return Yii::t('yii', '{n} MiB', $params, $this->locale);
case 3: return Yii::t('yii', '{n} GiB', $params, $this->locale);
case 4: return Yii::t('yii', '{n} TiB', $params, $this->locale);
default: return Yii::t('yii', '{n} PiB', $params, $this->locale);
case 0: return Yii::t('yii', '{nFormatted} B', $params, $this->locale);
case 1: return Yii::t('yii', '{nFormatted} KiB', $params, $this->locale);
case 2: return Yii::t('yii', '{nFormatted} MiB', $params, $this->locale);
case 3: return Yii::t('yii', '{nFormatted} GiB', $params, $this->locale);
case 4: return Yii::t('yii', '{nFormatted} TiB', $params, $this->locale);
default: return Yii::t('yii', '{nFormatted} PiB', $params, $this->locale);
}
} else {
switch ($position) {
case 0: return Yii::t('yii', '{n} B', $params, $this->locale);
case 1: return Yii::t('yii', '{n} KB', $params, $this->locale);
case 2: return Yii::t('yii', '{n} MB', $params, $this->locale);
case 3: return Yii::t('yii', '{n} GB', $params, $this->locale);
case 4: return Yii::t('yii', '{n} TB', $params, $this->locale);
default: return Yii::t('yii', '{n} PB', $params, $this->locale);
case 0: return Yii::t('yii', '{nFormatted} B', $params, $this->locale);
case 1: return Yii::t('yii', '{nFormatted} KB', $params, $this->locale);
case 2: return Yii::t('yii', '{nFormatted} MB', $params, $this->locale);
case 3: return Yii::t('yii', '{nFormatted} GB', $params, $this->locale);
case 4: return Yii::t('yii', '{nFormatted} TB', $params, $this->locale);
default: return Yii::t('yii', '{nFormatted} PB', $params, $this->locale);
}
}
}
......@@ -1119,21 +1119,21 @@ class Formatter extends Component
if ($this->sizeFormatBase == 1024) {
switch ($position) {
case 0: return Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params, $this->locale);
case 1: return Yii::t('yii', '{n, plural, =1{# kibibyte} other{# kibibytes}}', $params, $this->locale);
case 2: return Yii::t('yii', '{n, plural, =1{# mebibyte} other{# mebibytes}}', $params, $this->locale);
case 3: return Yii::t('yii', '{n, plural, =1{# gibibyte} other{# gibibytes}}', $params, $this->locale);
case 4: return Yii::t('yii', '{n, plural, =1{# tebibyte} other{# tebibytes}}', $params, $this->locale);
default: return Yii::t('yii', '{n, plural, =1{# pebibyte} other{# pebibytes}}', $params, $this->locale);
case 0: return Yii::t('yii', '{nFormatted} {n, plural, =1{byte} other{bytes}}', $params, $this->locale);
case 1: return Yii::t('yii', '{nFormatted} {n, plural, =1{kibibyte} other{kibibytes}}', $params, $this->locale);
case 2: return Yii::t('yii', '{nFormatted} {n, plural, =1{mebibyte} other{mebibytes}}', $params, $this->locale);
case 3: return Yii::t('yii', '{nFormatted} {n, plural, =1{gibibyte} other{gibibytes}}', $params, $this->locale);
case 4: return Yii::t('yii', '{nFormatted} {n, plural, =1{tebibyte} other{tebibytes}}', $params, $this->locale);
default: return Yii::t('yii', '{nFormatted} {n, plural, =1{pebibyte} other{pebibytes}}', $params, $this->locale);
}
} else {
switch ($position) {
case 0: return Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params, $this->locale);
case 1: return Yii::t('yii', '{n, plural, =1{# kilobyte} other{# kilobytes}}', $params, $this->locale);
case 2: return Yii::t('yii', '{n, plural, =1{# megabyte} other{# megabytes}}', $params, $this->locale);
case 3: return Yii::t('yii', '{n, plural, =1{# gigabyte} other{# gigabytes}}', $params, $this->locale);
case 4: return Yii::t('yii', '{n, plural, =1{# terabyte} other{# terabytes}}', $params, $this->locale);
default: return Yii::t('yii', '{n, plural, =1{# petabyte} other{# petabytes}}', $params, $this->locale);
case 0: return Yii::t('yii', '{nFormatted} {n, plural, =1{byte} other{bytes}}', $params, $this->locale);
case 1: return Yii::t('yii', '{nFormatted} {n, plural, =1{kilobyte} other{kilobytes}}', $params, $this->locale);
case 2: return Yii::t('yii', '{nFormatted} {n, plural, =1{megabyte} other{megabytes}}', $params, $this->locale);
case 3: return Yii::t('yii', '{nFormatted} {n, plural, =1{gigabyte} other{gigabytes}}', $params, $this->locale);
case 4: return Yii::t('yii', '{nFormatted} {n, plural, =1{terabyte} other{terabytes}}', $params, $this->locale);
default: return Yii::t('yii', '{nFormatted} {n, plural, =1{petabyte} other{petabytes}}', $params, $this->locale);
}
}
}
......@@ -1167,7 +1167,12 @@ class Formatter extends Component
$this->thousandSeparator = '';
$options[NumberFormatter::GROUPING_USED] = false;
// format the size value
$params = ['n' => $this->asDecimal($value, $decimals, $options, $textOptions)];
$params = [
// this is the unformatted number used for the plural rule
'n' => $value,
// this is the formatted number used for display
'nFormatted' => $this->asDecimal($value, $decimals, $options, $textOptions),
];
$this->thousandSeparator = $oldThousandSeparator;
return [$params, $position];
......
......@@ -746,16 +746,13 @@ class FormatterTest extends TestCase
$this->formatter->sizeFormatBase = 1024;
$this->assertSame("1 kibibyte", $this->formatter->asSize(1024));
$this->assertSame("1 mebibyte", $this->formatter->asSize(1024 * 1024));
// https://github.com/yiisoft/yii2/issues/4960
// $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
$this->assertSame("1023 bytes", $this->formatter->asSize(1023));
$this->assertSame("5 gibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
$this->assertNotEquals("5 pibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
//$this->assertSame("1 YiB", $this->formatter->asSize(pow(2, 80)));
$this->assertSame("2 gibibytes", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
$this->formatter->decimalSeparator = ',';
$this->formatter->numberFormatterOptions = [];
// https://github.com/yiisoft/yii2/issues/4960
// $this->assertSame("1,001 KiB", $this->formatter->asSize(1025, 3));
$this->assertSame("1,001 kibibytes", $this->formatter->asSize(1025, 3));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asSize(null));
......@@ -767,28 +764,33 @@ class FormatterTest extends TestCase
$this->formatter->sizeFormatBase = 1000;
$this->assertSame("999 bytes", $this->formatter->asSize(999));
$this->assertSame("1.05 megabytes", $this->formatter->asSize(1024 * 1024));
// https://github.com/yiisoft/yii2/issues/4960
// $this->assertSame("1.0486 megabytes", $this->formatter->asSize(1024 * 1024, 4));
$this->assertSame("1 kilobyte", $this->formatter->asSize(1000));
$this->assertSame("1.0486 megabytes", $this->formatter->asSize(1024 * 1024, 4));
$this->assertSame("1.00 kilobyte", $this->formatter->asSize(1000));
$this->assertSame("1.02 kilobytes", $this->formatter->asSize(1023));
$this->assertSame("3 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000));
$this->assertSame("3.00 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000));
$this->assertNotEquals("3 PB", $this->formatter->asSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)); // this is 3 EB not 3 PB
// tests for base 1024
$this->formatter->sizeFormatBase = 1024;
$this->assertSame("1 kibibyte", $this->formatter->asSize(1024));
$this->assertSame("1 mebibyte", $this->formatter->asSize(1024 * 1024));
// https://github.com/yiisoft/yii2/issues/4960
// $this->assertSame("1023 B", $this->formatter->asSize(1023));
$this->assertSame("5 gibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
$this->assertNotEquals("5 pibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
//$this->assertSame("1 YiB", $this->formatter->asSize(pow(2, 80)));
$this->assertSame("2 gibibytes", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
$this->assertSame("1.00 kibibyte", $this->formatter->asSize(1024));
$this->assertSame("1.00 mebibyte", $this->formatter->asSize(1024 * 1024));
$this->assertSame("1023 bytes", $this->formatter->asSize(1023));
$this->assertSame("5.00 gibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
$this->assertNotEquals("5.00 pibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
$this->assertSame("2.00 gibibytes", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
$this->formatter->decimalSeparator = ',';
$this->formatter->numberFormatterOptions = [];
// https://github.com/yiisoft/yii2/issues/4960
// $this->assertSame("1,001 kibibytes", $this->formatter->asSize(1025));
$this->assertSame("1,001 kibibytes", $this->formatter->asSize(1025, 3));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asSize(null));
}
/**
* https://github.com/yiisoft/yii2/issues/4960
*/
public function testAsSizeConfiguration()
{
// $this->formatter->thousandSeparator = '';
$this->assertSame("1023 bytes", $this->formatter->asSize(1023));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment