• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> 国际化功能

    MessageFormatter类

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

    MessageFormatter是一个具体的类,使用户能够生成连接的、与语言无关的消息。此类中提供的方法用于生成最终用户看到的所有消息。

    MessageFormatter类从程序提供的各种片段(如文本片段、数字和日期)中组装消息。由于使用了MessageFormatter类,程序不需要知道片段的顺序。该类使用片段的格式规范将它们组合成包含在资源包中的单个字符串中的消息。例如,MessageFormatter使您能够打印“完成打印X的Y文件…”的方式,仍然允许翻译的灵活性。

    以前,最终用户消息被创建为一个句子,并作为字符串处理。这个过程给本地化程序带来了问题,因为不同语言的句子结构、词序、数字格式等都有很大的不同。创建消息的语言中立方式使消息的每个部分保持独立,并为数据提供键。使用这些键,MessageFormatter类可以连接消息的各个部分,本地化它们,并向最终用户显示格式良好的字符串。

    MessageFormatter获取一组对象,对它们进行格式化,然后将格式化的字符串插入到模式的适当位置。Choice格式可与MessageFormatter结合使用,以处理复数、匹配数字和从项目数组中进行选择。通常,消息格式将来自资源,参数将在运行时动态设置。

    MessageFormatter
    {
    	/* 方法 */
    	public __construct ( string $locale , string $pattern )
    	public static create ( string $locale , string $pattern ) : MessageFormatter
    	public static formatMessage ( string $locale , string $pattern , array $args ) : string
    	public format ( array $args ) : string
    	public getErrorCode ( void ) : int
    	public getErrorMessage ( void ) : string
    	public getLocale ( void ) : string
    	public getPattern ( void ) : string
    	public static parseMessage ( string $locale , string $pattern , string $source ) : array
    	public parse ( string $value ) : array
    	public setPattern ( string $pattern ) : bool
    }