2024 年 PHP Conference Japan

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 物件導向範例

<?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 筆筆記

zlobnygrif at gmail dot com
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)
ricardoyubal at gmail dot com
3 年前
雖然沒有提及,但單引號 (') 在內部被用作跳脫字元。因此,如果您希望在輸出中包含單引號,則應使用另一個單引號來跳脫它。請參閱以下範例。

<?php

// 值 {foo} 應該以單引號括起來
echo msgfmt_format_message('en_US', "The value '{foo}' should be enclosed in single quotes", ['foo' => 'bar']);

// 值 'bar' 應該以單引號括起來
echo msgfmt_format_message('en_US', "The value ''{foo}'' should be enclosed in single quotes", ['foo' => 'bar']);

?>
contact at sowapps dot com
3 年前
目前沒有定義預設值的方法。

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