|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67580 Unable to set Formatter on MessageFormatter instance. Fractions Broken.
Submitted: 2014-07-06 23:53 UTC Modified: -
From: php at deep-freeze dot ca Assigned:
Status: Open Package: intl (PECL)
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-07-06 23:53 UTC] php at deep-freeze dot ca
In order to select the correct plural form, MessageFormatter needs to pre-format the given argument before choosing the correct plural form, as trailing 0's can affect the plural case.

Because of this, the functionality to explicitly set a formatter instance on a parameter needs to be exposed to the PHP User.  Otherwise, correct formatting becomes impossible.

One can attempt to work around the trailing zero option, by setting the number fromat explicitly in the pattern, however, this then loses all the localized formatting about the groupings.  

The correct method would be for something like :

$nf = new NumberFormatter("hr-HR", NumberFormatter::DECIMAL);
$nf->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
$mf = new MessageFormatter("hr-HR", "{num,plural,one{# ONE}two{# TWO}few{# FEW}many{# MANY}other{$ OTHER}}");
$mf->setFormatter("num", $nf);
echo $mf->format(array("num" => 1234.4));

As you can see by the actual results below, the plural section changes by visible trailing zeros, but there is no means to actually properly configure the MessageFormatter to work.

Test script:
echo "DEFAULT FORMAT :", MessageFormatter::formatMessage("hr-HR", "{num,plural,one{# ONE}two{# TWO}few{# FEW}many{# MANY}other{{num,number} OTHER}}", array("num" => "1234.40")), PHP_EOL;

echo "EXPLICIT FORMAT:", MessageFormatter::formatMessage("hr-HR", "{num,plural,one{# ONE}two{# TWO}few{# FEW}many{# MANY}other{{num,number,0.00} OTHER}}", array("num" => "1234.40")), PHP_EOL;

Actual result:


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Sep 19 17:03:36 2021 UTC