What is the MessageFormatter Class in PHP 5.3 good for?

php

Solution

I think you are confusing what the MessageFormatter does:

MessageFormatter is a concrete class that enables users to produce concatenated, language-neutral messages. The methods supplied in this class are used to build all the messages that are seen by end users.

That's quite different from a `number_format`, that only formats a number with grouped thousands.

Quoting further (emphasis mine):

MessageFormatter takes a set of objects, formats them, and then inserts the formatted strings into the pattern at the appropriate places. Choice formats can be used in conjunction with MessageFormatter to handle plurals, match numbers, and select from an array of items. Typically, the message format will come from resources and the arguments will be dynamically set at runtime.

If you need to compare this with a native function, then it's more like a locale-aware `preg_replace_callback` that can use `number_format`, `date_format` and fancy replacements like

"{0} resulted in {1,choice,0#no errors|1#single error|1<{1, number} errors}"

It should also be emphasized that MessageFormatter is just one subpackage completing a rather feature-rich I10n framework.

See this Article on DevZone about Internationalization in PHP 5.3.

Problem

With PHP +5.3 and 6 comes a couple new i18n classes. One of them is the MessageFormatter Class which seems to be an incomplete idea. I'm hoping I'm just overlooking something. The example given seems to show that if you ALREADY KNOW THE LANGUAGE YOU ARE USING, then this class will format the numbers for you correctly. ``` <?php echo msgfmt_format_message("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree", array(4560, 123, 4560/123)); echo msgfmt_format_message("de", "{0,number,integer} Affen auf {1,number,integer} Bäumen sind {2,number} Affen pro Baum", array(4560, 123, 4560/123)); ?> ``` Which renders: ``` 4,560 monkeys on 123 trees make 37.073 monkeys per tree 4.560 Affen auf 123 Bäumen sind 37,073 Affen pro Baum ``` Now if you already know the langauge (which is why you have to write it) then why don't you just use the number_format() function where you need proper numbers? Why do you need an entire class for this? An example of doing something other than formatting numbers would really help.

Original source