Question

I am using MessageFormat (part of the intl extension) in PHP. The extension uses ICU 4.6.1.

However, the problem isn't with PHP.

I would like to format a piece of string by making a decision on the first token {0}:

  • When {0} is 0, the text should be a
  • When it is 1, the text should be b
  • For all other cases, the text should be c

I understand that this could be done with plural, =0 for the 0 case, but through my testing, it doesn't seem to work, most likely because offsets and custom plurals were introduced in 4.8.

I am thus, trying to accomplish that using select:

{0, select, 0 {a} other {{0,plural,one{b}other{c}}}}

However, for some reason, the message formatter was unable to format the above and returns false (couldn't format).

Why is this the case? Is there a problem with my syntax?


Quick update: After some digging, it seems that I cannot use select to match values that start with a number. Are there any ways around this?

Another update: Updated to ICU 49.1.2 and intl 3.0.0a2. I can now use =0, =1, etc, but this has stopped working (was working previously):

{0}{0, plural, one{a} other{b}}

where as this works:

{0, plural, one{a} other{b}}
Was it helpful?

Solution

I ended up upgrading to the latest version of intl and ICU 49.1.2.

However, once that was done, this would not work:

{0}{0, plural, one{a} other{b}}

But this worked:

{0, plural, =0{a} one{# b} other {# c}}

OTHER TIPS

The select argument type is an exact string match. You would need to make sure that you are passing a string argument value, and then have clauses that match. Avoid using plurals for exact case matching, since each locale has its own rules for what one means.

The following should give you what you want, but I know much more about the format than the PHP implementation.

MessageFormatter::formatMessage($locale,
  '{0, select, _0 {a} _1 {b} other {c}}',
  array( "_$value" ))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top