定制 php-enspired/peekaboo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

php-enspired/peekaboo

Composer 安装命令:

composer require php-enspired/peekaboo

包简介

message formatting utilities using ICU, with a fallback on basic string templating.

README 文档

README

   

peekaboo! (ICU)

peekaboo provides message formatting utilities using International Components for Unicode, with a fallback on basic string templating.

dependencies

Requires php 8.3 or later.

ICU support requires the intl extension. Building ICU resource bundles uses genrb.

installation

Recommended installation method is via Composer: simply composer require php-enspired/peekaboo.

for starters

use at\peekaboo\ {
  HasMessages,
  MessageMapper
};

class Foo implements HasMessages {
  use MessageMapper;
}

$formatted = new Foo()->makeMessage("foo.welcome", ["place" => "jungle"]);
// Welcome to the jungle, we've got fun and games

message registry

Where did that message come from? peekaboo provides a registry for your application to store, lookup, and format messages. You can register any intl ResourceBundle, either available by default or under a named group, and get formatted messages by passing the message key and substitution context.

If a message key is not found under the specified group name, peekaboo falls back on looking in the default registries. You can also set a default locale (used when $locale is not provided for a specific message) by assigning to MessageRegistry::$defaultLocale.

<?php
use at\peekaboo\MessageRegistry;

MessageRegistry::register($yourDefaultResourceBundle);
MessageRegistry::register($yourUserResourceBundle, "user");

$formatted = MessageRegistry::message("users.namebadge", ["name" => "Adrian"], "ja_JP", "user");

If users.namebadge was found in the ja_JP locale, in either the "user" or the default registers, the above might return something like:

こんにちは、私の名前はAdrianです

If no matching formatting string was found in the ja_JP locale, peekaboo would fall back on the default locale and we might see something like:

Hello, my name is Adrian

For convenience, MessageRegistry::message() is available via a proxy function at\peekaboo\_, so the following is functionally the same as above:

<?php
use function at\peekaboo\_;

$formatted = _("users.namebadge", ["name" => "Adrian"], "ja_JP", "user");

To avoid the fallback behavior and look for messages only from a specific bundle (whether registered or not), use ::messageFrom() instead:

<?php

$formatted = MessageRegistry::messageFrom($yourUserResourceBundle, "users.namebadge", ["name" => "Adrian"], "ja_JP");

The message registry can also work with peekaboo's own MessageBundle class, which, like intl's ResourceBundle, is a container for message format strings. These are loaded from a php array, however, rather than an ICU resource file. As the name implies, only message formats are handled (not any other resource type).

<?php
use at\peekaboo\MessageBundle;

$myBundle = new MessageBundle(["welcome-user" => "Welcome to the {place}, {name}!"]);
echo MessageRegistry::messageFrom($myBundle, "welcome-user", ["name" => "Adrian", "place" => "jungle"]);
// Welcome to the jungle, Adrian!

messages without a registry

peekaboo allows classes to declare their own message formatting strings, either as a fallback if no matching message is registered or where there is simply no need to support multiple locales.

There are two ways to declare message formatting strings on a class. First, your class can define formats on the MESSAGES array. This is useful as a fallback for messages that aren't registered:

<?php

class Foo implements HasMessages {
  use MessageMapper;

  public const array MESSAGES = [
    "foo" => [
      "welcome" => "Welcome to the {place}, we've got fun and games"
    ]
  ];
}

new Foo()->makeMessage("foo.welcome", ["place" => "jungle"]);
// Welcome to the jungle, we've got fun and games

Or, you can use an enum to hold message formats with the MessageEnum trait. This provides a method message() which is equivalent to makesMessage() but does not take a key (since none is needed):

<?php

use at\peekaboo\MessageEnum;

enum Woo : string implements EnumeratesMessages {
  use MessageEnum;

  case Welcome = "Welcome to the {place}, we've got fun and games";
}

Woo::Welcome->message(["place" => "jungle"]);
// Welcome to the jungle, we've got fun and games

This is a subtle difference, but is worth calling out: when you use EnumeratesMessages->message(), peekaboo does not look up messages in the registry. It will only use the message format declared on the enum. The ->makeMessage() method is still available, though: this method will use the message registry, and will fall back on using ->message() if no message is found in the registry.

graceful fallback when intl is not loaded

A basic message formatter is included and is used if the intl extension is not available. This formatter supports named tokens (e.g., {name}), but ignores more complex formatting options like select, plural, and so forth. Given simple message formats, it should produce the same formatted message that intl's MessageFormatter would, and it does its best to reduce complex formatting expressions to a simple replacement.

This is intended more for non-icu usage (applications with simple messages) than to support a heavily localized application with many messages and complex formatting expressions. In the latter case, you should ensure the intl extension is available.

Version 2.0

2.0 requires PHP 8.3 or greater.

Additionally, peekaboo is now released under the Mozilla Public License, version 2.

Previously, the software was released under the GPLv3. That license's strong copyleft protections were a major factor in the decision to use it, but it has become apparent that their interpretations of what constitutes "combined works" was much broader than I'd understood it to be. Specifically, merely declaring this package as a dependency (e.g., via composer), without actually modifying and/or including/distributing the code with your software, was never intended to trigger these protections.

If you are using a version of this software licensed under the GPLv3, and would prefer to use it under the MPL instead, please contact me. I will grant the relicensing and waive any enforcement action against you arising from any noncompliance with the GPLv3 that would be permissible under the MPL.

tests

Run static analysis with composer test:analyze and run unit tests with composer test:unit.

Note, the first time you run a test: command, dev dependencies will be installed automatically. This requires an internet connection and may take some time.

contributing or getting help

I'm on IRC at libera#php-enspired, or open an issue on github. Feedback is welcomed as well.

php-enspired/peekaboo 适用场景与选型建议

php-enspired/peekaboo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 php-enspired/peekaboo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-enspired/peekaboo 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MPL-2.0
  • 更新时间: 2024-03-02