MessageFormatter::formatMessage

msgfmt_format_message

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

MessageFormatter::formatMessage -- msgfmt_format_message β€” Быстро Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ сообщСниС

ОписаниС

ΠžΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

public static function MessageFormatter::formatMessage(string $locale, string $pattern, array $values): string|false

ΠŸΡ€ΠΎΡ†Π΅Π΄ΡƒΡ€Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

function msgfmt_format_message(string $locale, string $pattern, array $values): string|false

Ѐункция быстрого форматирования, которая Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ строку Π±Π΅Π· нСобходимости явно ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ форматирования. Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ эту Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, ΠΊΠΎΠ³Π΄Π° опСрация форматирования выполняСтся Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π· ΠΈ Π½Π΅ Ρ‚Ρ€Π΅Π±ΡƒΠ΅Ρ‚ сохранСния ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² ΠΈΠ»ΠΈ состояния, Π° Ρ‚Π°ΠΊΠΆΠ΅ ΠΊΠΎΠ³Π΄Π° Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΡΡ‚Ρ€ΠΎΠΈΡ‚ΡŒ Π²Ρ‹Π²ΠΎΠ΄, прСдоставив Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ контСкст нСпосрСдствСнно для ICU.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

locale

Π›ΠΎΠΊΠ°Π»ΡŒ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΠ°Ρ для форматирования частСй, зависящих ΠΎΡ‚ Π»ΠΎΠΊΠ°Π»ΠΈ

pattern

Π‘Ρ‚Ρ€ΠΎΠΊΠ° (string) шаблона для вставки Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ². Π’ шаблонС ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ "друТСствСнный ΠΊ апострофам" синтаксис; ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅ смотритС Π² Ρ€Π°Π·Π΄Π΅Π»Π΅ » Quoting/Escaping.

values

Массив Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ (array) для вставки Π² строку Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° (string).

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ шаблона ΠΈΠ»ΠΈ false Π² случаС возникновСния ошибки.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования msgfmt_format_message()

<?php
echo msgfmt_format_message("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree\n", 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\n", array(4560, 123, 4560/123));
echo
msgfmt_format_message("en", 'You finished {place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}!', ['place' => 3]), "\n";
echo
msgfmt_format_message("en",
"There {apple, plural,
=0 {are no apples}
=1 {is one apple...}
other {are # apples!}
}"
,
[
'apple' => 0]
),
"\n";
?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠΌ стилС

<?php
echo MessageFormatter::formatMessage("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree\n", array(4560, 123, 4560/123));
echo
MessageFormatter::formatMessage("de", "{0,number,integer} Affen auf {1,number,integer} BΓ€umen sind {2,number} Affen pro Baum\n", array(4560, 123, 4560/123));
echo
MessageFormatter::formatMessage("en", 'You finished {place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}!', ['place' => 3]), "\n";
echo
MessageFormatter::formatMessage("en",
"There {apple, plural,
=0 {are no apples}
=1 {is one apple...}
other {are # apples!}
}"
,
[
'apple' => 0]
),
"\n";
?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

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
You finished 3rd!
There are no apples

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования ICU для форматирования Π²Π°Π»ΡŽΡ‚Ρ‹ с ΠΎΠ±Ρ‰ΠΈΠΌ ΠΈ с ΡƒΠ·ΠΊΠΈΠΌ символом Π²Π°Π»ΡŽΡ‚Ρ‹

Requires ICU β‰₯ 67.

<?php
echo msgfmt_format_message("cs_CZ", "{0, number, :: currency/CAD}", array(123.45));
echo
msgfmt_format_message("cs_CZ", "{0, number, :: currency/CAD unit-width-narrow}", array(123.45));

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

123,45 CA$
123,45 $

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • msgfmt_create() - Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ срСдство форматирования сообщСний
  • msgfmt_parse() - Π Π°Π·Π±ΠΈΡ€Π°Π΅Ρ‚ строку согласно ΡˆΠ°Π±Π»ΠΎΠ½Ρƒ
  • msgfmt_get_error_code() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΊΠΎΠ΄ ошибки послСднСй ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ
  • msgfmt_get_error_message() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ тСкст ошибки послСднСй ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
6
zlobnygrif at gmail dot com ΒΆ
12 years ago
<?php
var_dump(
  msgfmt_format_message('ru_RU', 'The value of {somekey} is {0, choice, 0 #less 12| 12 #between 12 and 32| 32 #more than 32}', ['SomeAnotherKey' => -1]),
  msgfmt_format_message('ru_RU', 'The value of {somekey} is {0, choice, 0 #less 12| 12 #between 12 and 32| 32 #more than 32}', [14]),
  msgfmt_format_message('ru_RU', 'The value of {somekey} is {0, choice, 0 #less 12| 12 #between 12 and 32| 32 #more than 32}', [133]),
  msgfmt_format_message('ru_RU', '{0, plural, =0{Zero}=1{One}other{Unknown #}}', [0]),
  msgfmt_format_message('ru_RU', '{0, plural, =0{Zero}=1{One}other{Unknown #}}', [1]),
  msgfmt_format_message('ru_RU', '{0, plural, =0{Zero}=1{One}other{Unknown #}}', [2]),
  msgfmt_format_message('ru_RU', '{0, select, somevalue{This is some value} other{Unknown value}}', ['somevalue']),
  msgfmt_format_message('ru_RU', '{0, select, somevalue{This is some value} other{Unknown value "{0}"}}', ['somevalue2'])
);

var_dump(
  msgfmt_format_message('en_GB', 'Peter has {0, plural, =0{no cat} =1{a cat} other{# cats}}', [0]),
  msgfmt_format_message('en_GB', 'Peter has {0, plural, =0{no cat} =1{a cat} other{# cats}}', [1]),
  msgfmt_format_message('en_GB', 'Peter has {0, plural, =0{no cat} =1{a cat} other{# cats}}', [2])
);
?>

Output (PHP 5.4.17RC1)
==========================
string 'The value of -1 is less 12' (length=26)
string 'The value of 14 is between 12 and 32' (length=36)
string 'The value of 133 is more than 32' (length=32)
string 'Zero' (length=4)
string 'One' (length=3)
string 'Unknown 2' (length=9)
string 'This is some value' (length=18)
string 'Unknown value "somevalue2"' (length=26)
string 'Peter has no cat' (length=16)
string 'Peter has a cat' (length=15)
string 'Peter has 2 cats' (length=16)
up
1
ricardoyubal at gmail dot com ΒΆ
4 years ago
Although not mentioned, single quotes (') are used internally as escaping characters. So if you want to have them in your output, you should escape them with another single quote. See examples below.

<?php

// The value {foo} should be enclosed in single quotes
echo msgfmt_format_message('en_US', "The value '{foo}' should be enclosed in single quotes", ['foo' => 'bar']);

// The value 'bar' should be enclosed in single quotes
echo msgfmt_format_message('en_US', "The value ''{foo}'' should be enclosed in single quotes", ['foo' => 'bar']);

?>
up
0
contact at sowapps dot com ΒΆ
4 years ago
For now, there is now way to define a default value.

<?php
echo msgfmt_format_message('fr_FR', '{distance, plural, =0{Here}other{# km}}', []);
// Show {distance}
?>