略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MessageFormatter

2024-11-13

The MessageFormatter class

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

简介

MessageFormatter is a concrete class that enables users to produce concatenated, language-neutral messages. The methods supplied in this class are used to build all the messages that are seen by end users.

The MessageFormatter class assembles messages from various fragments (such as text fragments, numbers, and dates) supplied by the program. Because of the MessageFormatter class, the program does not need to know the order of the fragments. The class uses the formatting specifications for the fragments to assemble them into a message that is contained in a single string within a resource bundle. For example, MessageFormatter enables you to print the phrase "Finished printing x out of y files..." in a manner that still allows for flexibility in translation.

Previously, an end user message was created as a sentence and handled as a string. This procedure created problems for localizers because the sentence structure, word order, number format and so on are very different from language to language. The language-neutral way to create messages keeps each part of the message separate and provides keys to the data. Using these keys, the MessageFormatter class can concatenate the parts of the message, localize them, and display a well-formed string to the end user.

MessageFormatter takes a set of objects, formats them, and then inserts the formatted strings into the pattern at the appropriate places. Choice formats can be used in conjunction with MessageFormatter to handle plurals, match numbers, and select from an array of items. Typically, the message format will come from resources and the arguments will be dynamically set at runtime.

类摘要

class MessageFormatter {
/* 方法 */
public __construct(string $locale, string $pattern)
public static create(string $locale, string $pattern): ?MessageFormatter
public static formatMessage(string $locale, string $pattern, array $values): string|false
public format(array $values): string|false
public getErrorCode(): int
public getErrorMessage(): string
public getLocale(): string
public getPattern(): string|false
public static parseMessage(string $locale, string $pattern, string $message): array|false
public parse(string $string): array|false
public setPattern(string $pattern): bool
}

目录

add a noteadd a note

User Contributed Notes 1 note

up
5
from dot php dot net at NOSPAM dot brainbox dot cz
7 years ago
MessageFormatter does not work with DateTime instances as parameters in PHP < 5.5. Instance will be converted to timestamp with value 0 (e.g. 1970-01-01) and following Notice will be raised: „Object of class DateTime could not be converted to int“. You have to manually convert the instance to timestamp in these old PHP versions.

<?php
$datetime
= new DateTime();
if (
PHP_VERSION_ID < 50500) { // PHP < 5.5 needs conversion to timestamp
  
MessageFormatter::formatMessage('en_US', 'Today is {0, date, full}.', array($datetime->getTimestamp()));
} else {
  
// current code
  
MessageFormatter::formatMessage('en_US', 'Today is {0, date, full}.', array($datetime));
}
?>

官方地址:https://www.php.net/manual/en/class.messageformatter.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3