# Intl extension class MessageFormatter instantiation returns NULL

- Thread: https://externals.io/message/46382
- Original archive: https://news-web.php.net/php.internals/46382
- Started by Chris Stockton on 2009-12-09T20:38:36+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #46382 — Chris Stockton — 2009-12-09T20:38:36+00:00

URL: https://externals.io/message/46382#46382

Hello,

I am trying to use ChoiceFormat in the Intl extension. When I try to
do something like:

$r = new MessageFormatter("en", "There are {0≤are no files|1≤is one
file|1<are many files} ok");
// $r is `NULL` 

This may be a incorrect use, but my thought is that regardless null
should not be returned because no error code or error message can be
returned. I also can not think of a single case off the top of my head
when new does not return a object (aside from a exception being thrown
on instantiation).

I am struggling to find documentation on plural usage. There is no
"ChoiceFormatter" class so it is my assumption that message formatter
parses that format. I would very much appreciate if someone was able
to point me in the right direction. My apologies if this may have been
better suited on PECL list, but subscribing appears down and I am
hastily in search for a answer.

-Chris

---

## #46394 — Jared Williams — 2009-12-09T23:34:00+00:00

URL: https://externals.io/message/46382#46394 · In reply to: #46382

> -----Original Message-----
> From: Chris Stockton [mailto:chrisstocktonaz@gmail.com] 
> Sent: 09 December 2009 20:39
> To: PHP Developers Mailing List
> Subject: [PHP-DEV] Intl extension class MessageFormatter 
> instantiation returns `NULL` 
> 
> Hello,
> 
> I am trying to use ChoiceFormat in the Intl extension. When I 
> try to do something like:
> 
> $r = new MessageFormatter("en", "There are {0?are no files|1?is one
> file|1<are many files} ok");
> // $r is `NULL` 
> 
> This may be a incorrect use, but my thought is that 
> regardless null should not be returned because no error code 
> or error message can be returned. I also can not think of a 
> single case off the top of my head when new does not return a 
> object (aside from a exception being thrown on instantiation).
> 
> I am struggling to find documentation on plural usage. There 
> is no "ChoiceFormatter" class so it is my assumption that 
> message formatter parses that format. I would very much 
> appreciate if someone was able to point me in the right 
> direction. My apologies if this may have been better suited 
> on PECL list, but subscribing appears down and I am hastily 
> in search for a answer.
> 
> -Chris
> 

Try...

$fmt = MessageFormatter::create('en_GB', 'There are {0,choice,0#are no
files|1#is one file|1<are many files} ok');

echo $fmt->format(array(0)), "\n";
echo $fmt->format(array(1)), "\n";
echo $fmt->format(array(10101)), "\n";

Best place for documentation is tests, or example code I find.
http://userguide.icu-project.org/formatparse/messages/examples

Jared

---

## #46399 — Tomas Kuliavas — 2009-12-10T06:33:14+00:00

URL: https://externals.io/message/46382#46399 · In reply to: #46382

2009.12.09 22:38 Chris Stockton rašė:
> Hello,
>
> I am trying to use ChoiceFormat in the Intl extension. When I try to
> do something like:
>
> $r = new MessageFormatter("en", "There are {0≤are no files|1≤is one
> file|1<are many files} ok");
> // $r is `NULL` 
>
> This may be a incorrect use, but my thought is that regardless null
> should not be returned because no error code or error message can be
> returned. I also can not think of a single case off the top of my head
> when new does not return a object (aside from a exception being thrown
> on instantiation).
>
> I am struggling to find documentation on plural usage. There is no
> "ChoiceFormatter" class so it is my assumption that message formatter
> parses that format. I would very much appreciate if someone was able
> to point me in the right direction. My apologies if this may have been
> better suited on PECL list, but subscribing appears down and I am
> hastily in search for a answer.

What's the point of using i18n code for basic "zero, one and many"
decision. Your sample text does not have numbers and does not use plural
forms. If that function can handle plural forms, then it should know
formating rules based on language set in first argument and second
argument should only set array of strings formated according to language
rules. Second argument should not have to evaluate number every time
translator wants to use plural form.


-- 
Tomas

---

## #46401 — Chris Stockton — 2009-12-11T04:45:34+00:00

URL: https://externals.io/message/46382#46401 · In reply to: #46399

Hello,

2009/12/9 Jared Williams <jared.williams1@ntlworld.com>:
> Try...
>
> $fmt = MessageFormatter::create('en_GB', 'There are {0,choice,0#are no
> files|1#is one file|1<are many files} ok');
> ...

Thank you, the piece I was missing is "{0,choice ... " I appreciate
the response and the link.


On Wed, Dec 9, 2009 at 11:33 PM, Tomas Kuliavas
<tokul@users.sourceforge.net> wrote:
> What's the point of using i18n code for basic "zero, one and many"
> decision. Your sample text does not have numbers and does not use plural
> forms. If that function can handle plural forms, then it should know

My example was straight from the documentation [1] but I missed a key
piece, as mentioned above
  -> $r = new MessageFormatter("en", "There are {0≤are no files|1≤is
one file|1<are many files} ok");
  -> $r = new MessageFormatter("en", "There are {0,choice,0≤are no
files|1≤is one file|1<are many files} ok");

This was non-obvious based off the research I did, a overlook perhaps
on my part. Furthermore `NULL` was not the expected result from a object
instantiation; hence my mail to this list. Obviously my snippet was
reduced from within a more complex internationalisation
implementation.

> formating rules based on language set in first argument and second
> argument should only set array of strings formated according to language
> rules. Second argument should not have to evaluate number every time
> translator wants to use plural form.

Pluralism is definitely a legitimate use case that is required within
my employers application; most seamlessly integrated by translation
services at the message formatter level. But hey, maybe your right...
ICU should just remove pluralism all together and we should use
arrays. Thanks for your useless response!

-Chris

[1] http://icu-project.org/apiref/icu4c/classChoiceFormat.html
