MessageFormatter::formatMessage
msgfmt_format_message
(No version information available, might be only in CVS)
msgfmt_format_message — Quick format message
Описание
Object oriented style
Procedural style
Quick formatting function that formats the string without having to explicitly create the formatter object. Use this function when the format operation is done only once and does not need and parameters or state to be kept.
Список параметров
- locale
-
The locale to use for formatting locale-dependent parts
- pattern
-
The pattern string to insert things into. The pattern uses an 'apostrophe-friendly' syntax; it is run through » umsg_autoQuoteApostrophe before being interpreted.
- args
Возвращаемые значения
The formatted pattern string or FALSE if an error occurred
Примеры
Пример #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", array(4560, 123, 4560/123));
echo msgfmt_format_message("de", "{0,number,integer} Affen über {1,number,integer} Bäume um {2,number} Affen pro Baum", array(4560, 123, 4560/123));
Пример #2 OO example
<?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 über {1,number,integer} Bäume um {2,number} Affen pro Baum", array(4560, 123, 4560/123));
?>
Результат выполнения данного примера:
4,560 monkeys on 123 trees make 37.073 monkeys per tree 4.560 Affen über 123 Bäume um 37,073 Affen pro Baum
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Internationalization Functions
- Функция MessageFormatter::create() - Constructs a new Message Formatter
- Функция MessageFormatter::formatMessage() - Quick format message
- Функция MessageFormatter::format() - Format the message
- Функция MessageFormatter::getErrorCode() - Get the error code from last operation
- Функция MessageFormatter::getErrorMessage() - Get the error text from the last operation
- Функция MessageFormatter::getLocale() - Get the locale for which the formatter was created.
- Функция MessageFormatter::getPattern() - Get the pattern used by the formatter
- Функция MessageFormatter::parseMessage() - Quick parse input string
- Функция MessageFormatter::parse() - Parse input string according to pattern
- Функция MessageFormatter::setPattern() - Set the pattern used by the formatter
Коментарии
<?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)
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}
?>
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']);
?>