cr0w/phrazor
Composer 安装命令:
composer require cr0w/phrazor
包简介
Minimal gettext-style i18n with ICU message formatting.
README 文档
README
Minimal gettext-style i18n for PHP with ICU message formatting.
- Source patterns are the keys — no separate message IDs
- ICU
MessageFormatterfor pluralization, gender, and variable substitution - Scanner CLI to extract patterns and keep locale files in sync
- Zero config for simple projects, fully configurable for complex ones
Requirements: PHP 8.2+, ext-intl
Installation
composer require cr0w/phrazor
Basic usage
// Simplest case — returns the pattern if no translation found echo t('Welcome'); // Variable substitution (requires ext-intl) echo t('Welcome, {name}', ['name' => 'Alice']); // Pluralization echo t('{count, plural, =0 {No items} one {# item} other {# items} }', ['count' => 3]); // Gender echo t('{gender, select, male {He liked this} female {She liked this} other {They liked this} }', ['gender' => 'female']); // Canonical namespaced call echo \phrazor\t('Welcome, {name}', ['name' => 'Alice']);
The global t() alias is registered automatically at autoload time. If t() is already defined in your project, phrazor will issue an E_USER_NOTICE and you can use \phrazor\t() instead.
Locale files
Locale files live in your i18n directory and return a plain PHP array. The source pattern is the key, the translation is the value.
// i18n/fr_FR.php <?php return [ // Simple string 'Welcome' => 'Bienvenue', // Variable substitution 'Welcome, {name}' => 'Bienvenue, {name}', // Pluralization '{count, plural, =0 {No items} one {# item} other {# items} }' => '{count, plural, =0 {Aucun élément} one {# élément} other {# éléments} }', ];
If a pattern has no translation, the pattern itself is returned — so en_US is a valid no-op locale.
Configuration
Runtime (constants)
Set these in your bootstrap before autoload:
define('PHRAZOR_I18N_PATH', __DIR__ . '/path/to/i18n'); define('PHRAZOR_LOCALE', 'fr_FR'); define('PHRAZOR_MISSING_LOCALE', 'warn'); // 'error' | 'warn' | 'silent'
Locale resolution order:
$localeargument passed tot()PHRAZOR_LOCALEconstant$_SERVER['APP_LOCALE']'en_US'
Missing locale file behaviour:
If the resolved locale file doesn't exist, phrazor will:
- Throw a
RuntimeExceptionifPHRAZOR_MISSING_LOCALEis'error' - Issue an
E_USER_WARNINGif'warn' - Silently fall back if
'silent'
If PHRAZOR_MISSING_LOCALE is not set, the default is 'warn' when APP_DEBUG is truthy, 'silent' otherwise.
Scanner (config file)
Create phrazor.php in your project root:
<?php return [ 'i18n_path' => __DIR__ . '/i18n', 'source' => [__DIR__ . '/src'], 'locales' => ['fr_FR', 'de_DE'], 'functions' => ['t', '_', 'trans'], // function names to scan for 'scan' => [ 'removed' => 'comment', // 'comment' | 'delete' | 'keep' 'new_keys' => 'bottom', // 'bottom' | 'top' | 'sort' 'sort' => false, // sort entire file after update 'identity' => false, // write key => key for new entries 'extensions' => ['php'], // file extensions to crawl 'output' => 'summary', // 'summary' | 'verbose' | 'silent' 'on_syntax_error' => 'error', // 'error' | 'warn' | 'skip' 'config_discovery'=> 'cwd', // 'cwd' | 'walk' ], ];
All options have sensible defaults — an empty config file or no config file at all will work for simple projects.
Scanner CLI
# Scan using phrazor.php in cwd vendor/bin/phrazor scan # Explicit config file vendor/bin/phrazor scan --config=config/phrazor.php # Override locales and output verbosity vendor/bin/phrazor scan --locale=fr_FR,de_DE --output=verbose # Multiple source directories vendor/bin/phrazor scan --source=src/,templates/,resources/views/ # Custom function names (your own aliases for t()) vendor/bin/phrazor scan --functions=t,_,trans # Write identity mappings for new keys (key => key) vendor/bin/phrazor scan --identity # Sort the entire locale file after update vendor/bin/phrazor scan --sort
All flags
| Flag | Description | Default |
|---|---|---|
--config=PATH |
Path to config file | phrazor.php in cwd |
--source=DIR,... |
Source directories to scan | src/ |
--locale=LOCALE,... |
Locales to update | discovered from i18n dir |
--functions=NAMES,... |
Function names to scan for | t |
--i18n-path=PATH |
Path to i18n directory | i18n/ |
--removed=MODE |
Handle removed keys: comment, delete, keep |
comment |
--new-keys=MODE |
Place new keys: bottom, top, sort |
bottom |
--sort |
Sort entire locale file after update | off |
--identity |
Write key => key for new entries |
off |
--output=MODE |
Verbosity: summary, verbose, silent |
summary |
--on-syntax-error=MODE |
Handle syntax errors: error, warn, skip |
error |
How the scanner works
- Uses
token_get_all()to parse PHP source — handles multiline patterns, single/double quoted strings, heredocs, and nowdocs correctly - Skips dynamic patterns like
t($var)ort('a' . $b)with a warning showing the file and line phrazor\t()is always scanned regardless of--functions- The i18n directory is automatically excluded from source scanning
- New keys with no translation are written as
// TODO:comments so translators know what needs attention - Removed keys are commented out by default (
// [removed]) so translations aren't lost if a key comes back
How new keys appear in locale files
With --identity off (default), new keys are written as TODO comments:
// TODO: translate 'Hello, {name}' => '',
With --identity on, identity mappings are written directly:
'Hello, {name}' => 'Hello, {name}',
Removed keys (with removed = 'comment') are preserved but flagged:
// [removed] 'This string is no longer in source' => 'Cette chaîne...',
License
MIT
cr0w/phrazor 适用场景与选型建议
cr0w/phrazor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cr0w/phrazor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cr0w/phrazor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-20