magneds/messageformat

使用关键词而非位置整数进行 ICU 消息格式化

dev-master 2018-11-28 09:31 UTC

This package is auto-updated.

Last update: 2024-09-28 22:39:33 UTC


README

PHP Intl MessageFormatter 类非常出色,除了位置变量语法外。这个库通过提供基于变量名的解决方案来解决这个问题,使得翻译者能够更好地理解变量代表的内容,从而更容易地翻译内容。

作为积极的副作用,它还通过允许在不影响变量顺序的情况下更改变量,使代码更加健壮。

安装

$ composer require magneds/messageformat

使用方法

<?php

use Magneds\MessageFormat\MessageFormatter;

$formatter = new MessageFormatter('en', 'Found {count, plural, =0 {no result} =1 {one result} other {# results}}');

print $formatter->format(['count' => 0]);  //  Found no result
print $formatter->format(['count' => 1]);  //  Found one result
print $formatter->format(['count' => 2]);  //  Found 2 results

由于 MessageFormatter 是 PHP Intl MessageFormatter 的直接替代品,基于位置的变量仍然有效。这应该使得迁移非常直接。

<?php

use Magneds\MessageFormat\MessageFormatter;

$formatter = new MessageFormatter('en', 'Found {0, plural, =0 {no result} =1 {one result} other {# results}}');

print $formatter->format([0]);  //  Found no result
print $formatter->format([1]);  //  Found one result
print $formatter->format([2]);  //  Found 2 results

API

构造函数

MessageFormatter::__construct(string $locale , string $pattern)

创建一个新的 MessageFormatter 实例,为指定的区域设置渲染提供的模式。

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Hello {audience}');
$es = new MessageFormatter('es', 'Hola {audience}');
$de = new MessageFormatter('de', 'Hallo {audience}');

创建

static MessageFormatter::create(string $locale, string $pattern)

创建一个新的 MessageFormatter 实例

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$en = MessageFormatter::create('en', 'Hello {audience}');
$es = MessageFormatter::create('es', 'Hola {audience}');
$de = MessageFormatter::create('de', 'Hallo {audience}');

formatMessage

static MessageFormatter::formatMessage(string $locale, string $pattern, array $args)

快速格式化消息,无需显式创建新的 MessageFormatter 实例。

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

print MessageFormatter::formatMessage('en', 'Hello {audience}', ['audience' => 'universe']); //  Hello universe

格式化

string MessageFormatter::format (array $args)

format 方法实际上将模式渲染为本地化字符串。

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$es = new MessageFormatter('es-ES', 'Por el pequeño precio de {price, number, currency} puedes comprar apps.');

print $es->format(['price' => 0.99]); //  Por el pequeño precio de 0,99 € puedes comprar apps.

getLocale

string MessageFormatter::getLocale(void)

从 MessageFormatter 实例中获取区域设置

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$enNZ = new MessageFormatter('en-NZ', 'Hello {audience}');
$nlBE = new MessageFormatter('nl-BE', 'Hallo {audience}');

print $enNZ->getLocale();  //  'en_NZ'
print $nlBE->getLocale();  //  'nl_BE'

getPattern

string MessageFormatter::getPattern([bool $compatible=false])

获取 MessageFormatter 实例的模式。可选地提供 PHP Intl MessageFormat 兼容变体

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Welcome back {name}, you have {count, plural, =0{no unread messages} one{one unread message} other{# unread messages}}');

print $en->getPattern();      //  Welcome back {name}, you have {count, plural, =0{no unread messages} one{one unread message} other{# unread messages}}
print $en->getPattern(true);  //  Welcome back {0}, you have {1, plural, =0{no unread messages} one{one unread message} other{# unread messages}}

parseMessage

static array MessageFormatter::parseMessage(string $locale, string $pattern, string $source)

快速解析输出字符串,提取所有变量

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

print MessageFormatter::parseMessage(
    'en_US', 
    '{monkeys,number,integer} monkeys on {trees,number,integer} trees make {distribution,number} monkeys per tree',
    '4,560 monkeys on 123 trees make 37.073 monkeys per tree'
);  //  ['monkeys' => 4560, 'trees' => 123, 'distribution' => 37.073],

解析

array MessageFormatter::parse(string $value)

从格式化字符串中提取变量

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$nl = new MessageFormatter('nl', 'De {animal} {action} de {result} van de {target}');
$message = 'De kat krabt de krullen van de trap';

print $nl->parse($message);  //  ['animal' => 'kat', 'action' => 'krabt', 'result' => 'krullen', 'target' => 'trap']

setPattern

bool MessageFormatter::setPattern(string $pattern)

设置新模式而不更改区域设置

示例

<?php

use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Initial {value}');

print $en->getPattern();      //  Initial {value}
print $en->getPattern(true);  //  Initial {0}

$en->setPattern('Override {value, number, percentage}');

print $en->getPattern();      //  Override {value, number, percentage}
print $en->getPattern(true);  //  Override {0, number, percentage}

变更日志

请参阅 变更日志

测试

$ composer test

贡献

请参阅 贡献指南

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件