siilike/messageformat
Composer 安装命令:
composer require siilike/messageformat
包简介
ICU message formatting using keywords instead of positional integers
README 文档
README
Forked from magneds/messageformat.
The PHP Intl MessageFormatter class works great, except for the positional variables syntax. This library solves that by providing a variable name based solution, making it easier for translators to translate content with more of an understanding of what variables represent.
As a positive side effect, it also makes the code more robust by allowing for change in variables without compromising the variable order.
Installation
$ composer require magneds/messageformat
Usage
<?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
As the MessageFormatter is a drop-in replacement to the PHP Intl MessageFormatter, the position based variables also still work. This should make migration pretty straight-forward.
<?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
Constructor
MessageFormatter::__construct(string $locale , string $pattern)
Create a new MessageFormatter instance rendering the provided pattern for the locale.
example
<?php use Magneds\MessageFormat\MessageFormatter; $en = new MessageFormatter('en', 'Hello {audience}'); $es = new MessageFormatter('es', 'Hola {audience}'); $de = new MessageFormatter('de', 'Hallo {audience}');
create
static MessageFormatter::create(string $locale, string $pattern)
Create a new instance of MessageFormatter
example
<?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)
Quickly format a message, without explicitly creating a new instance of MessageFormatter.
example
<?php use Magneds\MessageFormat\MessageFormatter; print MessageFormatter::formatMessage('en', 'Hello {audience}', ['audience' => 'universe']); // Hello universe
format
string MessageFormatter::format (array $args)
The format method is what actually does the rendering the pattern into a localized string.
example
<?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)
Obtain the locale from the MessageFormatter instance
example
<?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])
Obtain the pattern of the MessageFormatter instance. Optionally providing the PHP Intl MessageFormat compatible variant
example
<?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)
Quick parse output string, extracting all the variables
example
<?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)
Extract the variables from a formatted string
example
<?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)
Sets a new pattern without changing the locale
example
<?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}
Change log
Please see Changelog
Testing
$ composer test
Contributing
Please see Contributing
Credits
License
The MIT License (MIT). Please see License File for more information.
siilike/messageformat 适用场景与选型建议
siilike/messageformat 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 683 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「icu」 「magneds」 「messageformat」 「message-format」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 siilike/messageformat 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 siilike/messageformat 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 siilike/messageformat 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Enables ICU message formatting for translations in Symfony applications.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Translation library
Library to find iso language code from a microsoft LCID
A wrapper library for PHP to format and internationalize values in messages like sprintf
统计信息
- 总下载量: 683
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-01