PHP Conference Japan 2024

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(字符串 $locale, 字符串 $pattern, 数组 $values): 字符串|false

过程式风格

msgfmt_format_message(字符串 $locale, 字符串 $pattern, 数组 $values): 字符串|false

快速格式化函数,用于在无需显式创建格式化程序对象的情况下格式化字符串。当格式化操作仅执行一次且不需要保留任何参数或状态,或者想要通过向 ICU 直接提供其他上下文来自定义输出时,请使用此函数。

参数

locale

用于格式化依赖于区域设置的部分的区域设置

pattern

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

values

要插入格式字符串中的值的数组

返回值

格式化的模式字符串或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 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";

以上示例将输出

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 个注释

[email protected]
11年前
<?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])
);
?>

输出 (PHP 5.4.17RC1)
==========================
字符串 'The value of -1 is less 12' (长度=26)
字符串 'The value of 14 is between 12 and 32' (长度=36)
字符串 'The value of 133 is more than 32' (长度=32)
字符串 'Zero' (长度=4)
字符串 'One' (长度=3)
字符串 'Unknown 2' (长度=9)
字符串 'This is some value' (长度=18)
字符串 'Unknown value "somevalue2"' (长度=26)
字符串 'Peter has no cat' (长度=16)
字符串 'Peter has a cat' (长度=15)
字符串 'Peter has 2 cats' (长度=16)
[email protected]
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']);

?>
[email protected]
3年前
目前,无法定义默认值。

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