jrmajor/fluent
Composer 安装命令:
composer require jrmajor/fluent
包简介
Fluent localization system for PHP
关键字:
README 文档
README
A PHP implementation of the Project Fluent, a localization system designed by Mozilla to unleash the expressive power of the natural language.
Read the Fluent Syntax Guide or try it out in the Fluent Playground to learn more about the syntax.
shared-photos =
{ $userName } { $photoCount ->
[one] added a new photo
*[other] added { $photoCount } new photos
} to { $userGender ->
[male] his stream
[female] her stream
*[other] their stream
}.
$bundle->message('stream.shared-photos', [ 'userName' => 'jrmajor', 'photoCount' => 2, 'userGender' => 'male', ]); // jrmajor added 2 new photos to his stream.
You may install this package via Composer: composer require jrmajor/fluent.
Use jrmajor/laravel-fluent to integrate Fluent translations into your Laravel application.
Usage
Parsing
use Major\Fluent\Parser\FluentParser; $parser = new FluentParser(strict: true); $resource = $parser->parse('hello-user = Hello, { $userName }!');
$strict constructor argument defaults to false. In strict mode syntax errors result in a ParserException. Otherwise, they are ignored and represented in AST by Junk.
Message formatting
use Major\Fluent\Bundle\FluentBundle; $bundle = new FluentBundle('en', strict: true); $bundle->addResource($resource); // or $bundle->addFtl('hello-user = Hello, { $userName }!'); $bundle->message('hello-user', userName: 'World'); // Hello, World!
FluentBundle is single-language store of translation resources. Its constructor accepts following arguments:
string $locale: Locale to instantiate locale-specific formatters (e.g. en-US, pl, zh-Hant-TW, fr-CA).bool $strict = false: See handling errors.bool $useIsolating = true: Specifying whether to use Unicode isolation marks (FSI, PDI) for bidirectional interpolations.bool $allowOverrides = false: Allow overriding existing messages and terms.
Adding resources
Translations can be added to bundle using addResource() or addFtl() methods. The first one accepts FluentResource object returned by the parser, while the second one accepts a raw FTL string.
addResource(FluentResource $resource, bool $allowOverrides = null): static addFtl(string $ftl, bool $allowOverrides = null): static
If $allowOverrides is null, bundle default will be used.
If bundle is in strict mode, $ftl parsing will be also done in strict mode.
Formatting messages
message(string $_message, mixed ...$arguments): ?string
message() method accepts an id of a message as a first parameter and message arguments as named parameters. It returns null for missing messages.
welcome = Welcome
.guest = Welcome, Guest
.user = Welcome, { $userName }
$bundle->message('welcome'); // Welcome $bundle->message('goodbye'); // null
For attributes, you may use “dot” notation:
$bundle->message('welcome.guest'); // Welcome, Guest
Message arguments may be passed as named arguments or as an associative array:
$bundle->message('welcome.user', userName: 'John'); // Welcome, John // equivalent to $bundle->message('welcome.user', ['userName' => 'John']); // Welcome, John
Handling errors
By default, all resolver exceptions are ignored and can be obtained using popErrors() method. It returns an array of exceptions and clears exception cache, which means the next time you call it, it will return only new errors.
$bundle->message('welcome.user'); // Welcome, {$userName} $errors = $bundle->popErrors(); count($errors); // 1 $errors[0]->getMessage(); // Unknown variable: $userName.
All resolver exceptions extend ResolverException. In strict mode they would be thrown right away in message() call.
Testing
vendor/bin/phpunit # Tests vendor/bin/phpstan analyze # Static analysis vendor/bin/php-cs-fixer fix # Formatting
jrmajor/fluent 适用场景与选型建议
jrmajor/fluent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.24k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2021 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translation」 「parser」 「locale」 「language」 「l10n」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jrmajor/fluent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jrmajor/fluent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jrmajor/fluent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
An MT940 bank statement parser for PHP
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 18.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 33
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-10