Beware: passing an empty string for format returns null, not a formatter which returns empty strings.
$ php -r "print_r(new MessageFormatter('en_US',' '));"
MessageFormatter Object
(
)
$ php -r "print_r(new MessageFormatter('en_US',''));"(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)
MessageFormatter::create -- MessageFormatter::__construct -- msgfmt_create β Π‘ΠΎΠ·Π΄Π°ΡΡ ΡΡΠ΅Π΄ΡΡΠ²ΠΎ ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ
ΠΠ±ΡΠ΅ΠΊΡΠ½ΠΎ-ΠΎΡΠΈΠ΅Π½ΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΡΡΠΈΠ»Ρ (ΠΌΠ΅ΡΠΎΠ΄)
ΠΠ±ΡΠ΅ΠΊΡΠ½ΠΎ-ΠΎΡΠΈΠ΅Π½ΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΡΡΠΈΠ»Ρ (ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ):
ΠΡΠΎΡΠ΅Π΄ΡΡΠ½ΡΠΉ ΡΡΠΈΠ»Ρ
Π‘ΠΎΠ·Π΄Π°ΡΡ ΡΡΠ΅Π΄ΡΡΠ²ΠΎ ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ.
localeΠΠΎΠΊΠ°Π»Ρ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌΠ°Ρ ΠΏΡΠΈ ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ²
patternΠ‘ΡΡΠΎΠΊΠ° (string) ΡΠ°Π±Π»ΠΎΠ½Π° Π΄Π»Ρ Π²ΡΡΠ°Π²ΠΊΠΈ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ². Π ΡΠ°Π±Π»ΠΎΠ½Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ "Π΄ΡΡΠΆΠ΅ΡΡΠ²Π΅Π½Π½ΡΠΉ ΠΊ Π°ΠΏΠΎΡΡΡΠΎΡΠ°ΠΌ" ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡ; ΠΏΠΎΠ΄ΡΠΎΠ±Π½Π΅Π΅ ΡΠΌΠΎΡΡΠΈΡΠ΅ Π² ΡΠ°Π·Π΄Π΅Π»Π΅ » Quoting/Escaping.
ΠΠ±ΡΠ΅ΠΊΡ MessageFormatter ΠΈΠ»ΠΈ null Π² ΡΠ»ΡΡΠ°Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΡ ΠΎΡΠΈΠ±ΠΊΠΈ.
ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡΠ° Π² ΡΠ»ΡΡΠ°Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΡ ΠΎΡΠΈΠ±ΠΊΠΈ Π²ΡΠ±ΡΠ°ΡΡΠ²Π°Π΅ΡΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ IntlException.
ΠΡΠΈΠΌΠ΅Ρ #1 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ msgfmt_create()
<?php
$fmt = msgfmt_create("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree");
echo msgfmt_format($fmt, array(4560, 123, 4560/123));
$fmt = msgfmt_create("de", "{0,number,integer} Affen auf {1,number,integer} BΓ€umen sind {2,number} Affen pro Baum");
echo msgfmt_format($fmt, array(4560, 123, 4560/123));
?>ΠΡΠΈΠΌΠ΅Ρ #2 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ Π² ΠΎΠ±ΡΠ΅ΠΊΡΠ½ΠΎ-ΠΎΡΠΈΠ΅Π½ΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΌ ΡΡΠΈΠ»Π΅
<?php
$fmt = new MessageFormatter("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree");
echo $fmt->format(array(4560, 123, 4560/123));
$fmt = new MessageFormatter("de", "{0,number,integer} Affen auf {1,number,integer} BΓ€umen sind {2,number} Affen pro Baum");
echo $fmt->format(array(4560, 123, 4560/123));
?>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΏΡΠΈΠ²Π΅Π΄ΡΠ½Π½ΠΎΠ³ΠΎ ΠΏΡΠΈΠΌΠ΅ΡΠ°:
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
Beware: passing an empty string for format returns null, not a formatter which returns empty strings.
$ php -r "print_r(new MessageFormatter('en_US',' '));"
MessageFormatter Object
(
)
$ php -r "print_r(new MessageFormatter('en_US',''));"