承接 jrmajor/fluent 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jrmajor/fluent

Composer 安装命令:

composer require jrmajor/fluent

包简介

Fluent localization system for PHP

README 文档

README

Latest Stable Version Required PHP Version

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jrmajor/fluent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 33
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-10