|  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: 2021-11-11 11:17 UTC
From: php at deep-freeze dot ca Assigned:
Status: Open Package: I18N and L10N related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: php at deep-freeze dot ca
New email:
PHP Version: OS:


 [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


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-11 11:17 UTC]
-Package: intl +Package: I18N and L10N related
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Oct 05 18:05:54 2022 UTC