number_format only allows one to format... well, numbers -- and it will only format them as pure number.
On the other hand, the MessageFormater class does more than this :
- It allows you to format numbers, yes ; but not only as "pure numbers", but also as (for instance) money
- And both the format of a number, and the monerary unit, are different, depending on the locale.
- For instance, in France, you'd use
1234,25 € ; while in the US, you'd use (forgive me if I'm wrong) something like $1,234.25
- It allows you to format more than only numbers :
- You can also format dates, for example
- including the names of the days/month
- And it does more than just formating some piece of data : it allows you to specify full strings, in which you use placeholders -- indicating which kind of data those should represent.
- That last point is important when you are trying to translate/localize an application : formats changes, yes ; but positions of the data can also change, depending on the language.
This is great when you are developping an application, having someone else do the translations for you :
- You code your application so it loads its translation strings from a file, that depends on the language
- And you say, for each string, in which order you'll pass the data
- Then, someone else takes your (english, for instance) translation file, and translate it to another language
- Using the same placeholders as you did, for dynamic data
- Placing them in a different order, if needed
- Then, when your application is executed :
- You load the right translation file,
- You inject the data
- And no-one has to care about formating the numbers/dates/... nor about the order of the data in the strings ; which is great ;-)
Also note that MessageFormater providers some additionnal features, like the MessageFormatter::parseMessage method, that does exactly the opposite.
I've never really used that one yet, though -- but it might prove useful in some situations, I bet.
Finally, you say this :
The example given seems to show that
if you ALREADY KNOW THE LANGUAGE YOU
ARE USING
Yes, MessageFormater expects that you know which language your application is currently renderring.
As a matter of fact, the role of the MessageFormater class is... to format messages -- nothing more.
But you have some other classes, that allow you to detect which language you should use ;-)
For instance, you might want to take a look at the Locale class -- the Locale::acceptFromHttp might especially interest you (quoting) :
Tries to find locale that can satisfy
the language list that is requested by
the HTTP "Accept-Language" header
Basically, this should help you detect which language the browser is "accepted" by your user is using -- which, most probably, will indicate which language/locale the current user is the most probably able to understand.
For instance, considering my browser is sending this for the Accept-language HTTP header :
fr,en;q=0.7,en-us;q=0.3
Locale::acceptFromHttp would indicate that I prefer the 'fr' locale -- which I do prefer ^^