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 MessageFormatter::formatMessage(string $locale, string $pattern, array $values): string|false

过程式风格

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

快速格式化函数,无需显式创建格式化器对象即可格式化字符串。当格式化操作仅执行一次且不需要任何参数或状态来保存时,或者想要通过向 ICU 直接提供额外的上下文来定制输出时,请使用此函数。

参数

locale

用于格式化区域设置相关部分的区域设置。

pattern

要插入内容的模式 string。该模式使用“单引号友好”语法;有关详细信息,请参阅 » 引用/转义

values

要插入到格式 string 中的值的 array

返回值

格式化的模式字符串或如果发生错误则为 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 使用常见货币符号和缩写货币符号格式化货币

需要 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 $

参见

添加注释

用户贡献的注释 3 个注释

7
zlobnygrif at gmail dot com
11 年前
<?php
var_dump
(
msgfmt_format_message('ru_RU', 'Значение {somekey} равно {0, choice, 0 #меньше 12| 12 #между 12 и 32| 32 #больше 32}', ['SomeAnotherKey' => -1]),
msgfmt_format_message('ru_RU', 'Значение {somekey} равно {0, choice, 0 #меньше 12| 12 #между 12 и 32| 32 #больше 32}', [14]),
msgfmt_format_message('ru_RU', 'Значение {somekey} равно {0, choice, 0 #меньше 12| 12 #между 12 и 32| 32 #больше 32}', [133]),
msgfmt_format_message('ru_RU', '{0, plural, =0{Ноль}=1{Один}other{Неизвестно #}}', [0]),
msgfmt_format_message('ru_RU', '{0, plural, =0{Ноль}=1{Один}other{Неизвестно #}}', [1]),
msgfmt_format_message('ru_RU', '{0, plural, =0{Ноль}=1{Один}other{Неизвестно #}}', [2]),
msgfmt_format_message('ru_RU', '{0, select, somevalue{Это какое-то значение} other{Неизвестное значение}}', ['somevalue']),
msgfmt_format_message('ru_RU', '{0, select, somevalue{Это какое-то значение} other{Неизвестное значение "{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])
);
?>

输出 (PHP 5.4.17RC1)
==========================
string 'Значение -1 равно меньше 12' (length=28)
string 'Значение 14 равно между 12 и 32' (length=38)
string 'Значение 133 равно больше 32' (length=34)
string 'Ноль' (length=4)
string 'Один' (length=4)
string 'Неизвестно 2' (length=11)
string 'Это какое-то значение' (length=21)
string 'Неизвестное значение "somevalue2"' (length=30)
string 'Peter has no cat' (length=16)
string 'Peter has a cat' (length=15)
string 'Peter has 2 cats' (length=16)
1
ricardoyubal at gmail dot com
3 年前
虽然没有提到,但单引号 (') 在内部用作转义字符。 所以,如果你想在输出中使用它们,你应该用另一个单引号转义它们。 请看下面的示例。

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

?>
0
contact at sowapps dot com
3 年前
目前,还没有办法定义默认值。

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