siilike / messageformat
使用关键字而非位置整数进行ICU消息格式化
v1.0
2023-02-01 22:09 UTC
Requires
- ext-intl: *
Requires (Dev)
- phpunit/phpunit: ^6.5
- scato/phpeg: ^1.0
- squizlabs/php_codesniffer: ^2.3
This package is not auto-updated.
Last update: 2024-09-27 03:46:14 UTC
README
从 magneds/messageformat 分支而来。
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
format
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],
parse
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)。请参阅 许可文件 了解更多信息。