PHP.mk документација

MessageFormatter::formatMessage

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

messageformatter.formatmessage.php PHP.net прокси Преводот е вчитан
Оригинал на PHP.net
Патека messageformatter.formatmessage.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + преведен приказ Кодовите, табелите и белешките остануваат читливи во истиот тек.
MessageFormatter::formatMessage

Референца за `messageformatter.formatmessage.php` со подобрена типографија и навигација.

messageformatter.formatmessage.php

MessageFormatter::formatMessage

msgfmt_format_message

Класата Collator

MessageFormatter::formatMessage -- msgfmt_format_messageБрзо форматирање порака

= NULL

Напиши целосна ознака на елемент

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 за вметнување работи во. Шемата користи синтаксија „пријателска со апостроф“; видете » Наведување/Избегнување интерполација на низи

values

На array на вредностите што треба да се вметнат во форматот string

Вратени вредности

Форматирана низа на шема или false ако настанала грешка

Примери

Пример #1 msgfmt_format_message() example

<?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 OO пример

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

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

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 да форматира валута со вообичаено и со тесно симбол за валута

Потребно е 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));

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

123,45 CA$
123,45 $

Види Исто така

Белешки од корисници 3 белешки

zlobnygrif на gmail точка com
12 години пред
<?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)
ricardoyubal на gmail точка com
пред 4 години
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']);

?>
contact на sowapps точка com
пред 4 години
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}
?>
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.