php.net |  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
 [2014-07-06 23:53 UTC] php at deep-freeze dot ca
Description:
------------
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}}");
// REQUIRED FUNCTIONALITY::
$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:
--------------
DEFAULT FORMAT :1.234,4 FEW
EXPLICIT FORMAT:1234,40 OTHER

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-11 11:17 UTC] nikic@php.net
-Package: intl +Package: I18N and L10N related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 08:01:29 2024 UTC