toobo/bcp47 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

toobo/bcp47

Composer 安装命令:

composer require toobo/bcp47

包简介

PHP utility functions to validate and normalize IETF BCP 47 language tag.

README 文档

README

PHP helpers to validate and normalize IETF BCP 47 language tag.

Static Analysis Unit Tests Code Coverage PHP Version Mutation Tests

Utility functions

$isValid = Toobo\Bcp47::isValidTag('i-klingon');  // true
$isValid = Toobo\Bcp47::isValidTag('xy');         // false

$filtered = Toobo\Bcp47::filterTag('EN-us');      // "en-US"
$filtered = Toobo\Bcp47::filterTag('fr-latn-fx'); // "fr-FR"

$isRTL = Toobo\Bcp47::isRtl('he');                // true
$isRTL = Toobo\Bcp47::isRtl('en-us');             // false

var_export(Toobo\Bcp47::splitTag('En-ca-Newfound'));
array (
  'language' => 'en',
  'extLang' => '',
  'script' => '',
  'region' => 'CA',
  'variant' => 'newfound',
  'extension' => '',
  'privateUse' => '',
)

The Bcp47Tag class

The Toobo\Bcp47Tag class offers an API similar to the utility functions, but it ensures it encapsulates a valid tag, because it throws when instantiated with an invalid tag.

The class is Stringable and JsonSerializable, and it also implements the Bcp47Code interface defined by the wikimedia/bcp-47-code package.

$tag = Toobo\Bcp47Tag::new('En-ca-Newfound');

assert($tag->isSameCodeAs(Toobo\Bcp47Tag::new('en-CA-newfound')) === true);

assert($tag->language() === 'en');
assert($tag->extLang() === null);
assert($tag->script() === null);
assert($tag->region() === 'CA');
assert($tag->variant() === 'newfound');
assert($tag->extension() === null);
assert($tag->privateUse() === null);
assert($tag->isRtl() === false);

assert((string) $tag === 'en-CA-newfound');
assert($tag->toBcp47Code() === 'en-CA-newfound');
assert(json_encode($tag) === '{"language":"en","region":"CA","variant":"newfound"}');

Validation

The Bcp47Tag class, as well as the Bcp47::isValidTag(), Bcp47::filterTag(), and Bcp47::splitTag() functions, all do validation.

The class throw on instantiation for invalid tags, while the functions returns, respectively, false, null, and an array with all empty items.

The validation is not just about the format but also the actual values. For example, xy-IT looks like a valid tag, but the language "xy" does not exist, so the the tag is not valid.

The validation apply to all subtags (but "extension" and "privateUse"), and also across subtags. For example, the tag ca-valencia is valid (Valencia variant of the Catalan language), but en-valencia is not, despite the language "en" and the variant "valencia" being valid per-se, because there is no "valencia" variant for the English language.

Validation is done by comparing the values with the up-to-date list of all the registered BCP 47 subtags, which includes over 8000 languages, and several hundreds of scripts, regions, and variants.

Normalization

The Bcp47Tag class, as well as both Bcp47::filterTag() and Bcp47::splitTag() functions "normalize" the given tag.

Normalization includes:

  • Replace deprecated values with the new accepted value, when available. For example, the region code for the "Democratic Republic of the Congo" (formerly "Zaire") "ZR" is replaced with "CD".
  • Case normalization (all lowercase, but uppercase region and title-case script.
  • Replacement of numeric region codes with 2-chars alpha code, when available.
  • Replacement of 3-chars language code (ISO 639-3) with 2-chars code (ISO 636-1), when available.

FAQ

  • Why is Bc47 an enum?

This package's utility functions are stateless and pure PHP functions.

However, plain PHP functions can't be autoloaded. By using a case-less PHP enum, we get autoloading, but unlike when using a class, we prevent anyone to extend or instantiate the class without intervening on the runtime code.

A case-less PHP enum is a de facto autoload-enabling namespace for functions.

Installation

Best served via Composer, the package name is toobo/bcp47.

composer require "toobo/bcp47:^1"

Requirements

BCP 47 requires PHP 8.3+, and requires via Composer:

  • "wikimedia/bcp-47-code" (GPL v2)

When installed for development, it also requires via Composer:

  • "phpunit/phpunit" (BSD-3-Clause)
  • "inpsyde/php-coding-standards" (MIT)
  • "vimeo/psalm" (MIT)

Security Issues

If you have identified a security issue, please email giuseppe.mazzapica [at] gmail.com and do not file an issue as they are public.

License

Copyright (c), Giuseppe Mazzapica, and contributors.

This software is released under the "MIT" license.

toobo/bcp47 适用场景与选型建议

toobo/bcp47 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.5k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「locale」 「language」 「lang」 「BCP-47」 「ietf」 「bcp47」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 toobo/bcp47 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-22