What is the MessageFormatter class in PHP 5.3?

With PHP +5.3 and 6, a couple of new i18n classes appear. One of them is the MessageFormatter Class, which seems to be an incomplete idea. I hope that I just missed something.

The above example shows that if you ALREADY KNOW THE LANGUAGE, YOU ARE USING THE USE, then this class will correctly format your numbers.

<?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));
?>

What is he doing:

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 langauge (which is why you need to write it), then why don't you just use number_format () , where do you need the correct numbers? Why do you need a whole class?

An example of doing something other than formatting numbers really helps.

+3
source share
3

, , MessageFormatter :

MessageFormatter - , , . , , , .

number_format, .

( ):

MessageFormatter , , . MessageFormatter , . , , .

, preg_replace_callback, number_format, date_format ,

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

, MessageFormatter - , I10n.

DevZone PHP 5.3.

+3

number_format ... , - .

, MessageFormater , :

  • , ; " ", ()
    • , , .
    • , 1234,25 €; ( , ) - $1,234.25
  • , :
    • ,
    • /
  • , : , , , .
    • , / : , ; .


, , - :

  • , ,
    • , ,
  • - (, )
    • , ,
    • ,
  • , :
    • ,
    • //... ; ; -)


, MessageFormater , MessageFormatter::parseMessage, .

, , , .


, :

,

, MessageFormater , , .

MessageFormater - ... - .
, , ; -)

, Locale - Locale::acceptFromHttp ():

, , HTTP-Accept-Language

, , , - , , , /, , , .

, , Accept-languag e HTTP:

fr,en;q=0.7,en-us;q=0.3

Locale::acceptFromHttp , 'fr', ^^

+2

- ICU, - , PHP, , , , .

0

Source: https://habr.com/ru/post/1735118/


All Articles