yoeunes/icu-parser
Composer 安装命令:
composer require yoeunes/icu-parser
包简介
High-performance ICU MessageFormat parser with lexer, AST, type inference, and CLI tooling.
README 文档
README
ICU Parser: Static Analysis, Linter & AST Generator
ICU Parser is a PHP 8.2+ library that treats ICU MessageFormat strings as code.
Unlike simple wrappers around MessageFormatter, ICU Parser implements a compiler-style pipeline (Lexer → Parser → AST) and provides static analysis for ICU messages.
This architecture allows for advanced tooling:
- Parsing: Parse ICU messages into a structured, typed AST.
- Validation: Detect common syntax and semantic errors.
- Type Inference: Extract parameter types from messages.
- Linting: Validate translation catalogs across multiple locales.
- Analysis: Syntax highlighting, pretty-formatting, and debugging.
Built for validation, analysis, and robust tooling in i18n workflows.
If you are new to ICU Parser, start with the Quick Start Guide. If you want a short overview, see docs/overview.md.
Getting started
# Install the library composer require yoeunes/icu-parser # Optional: improve locale support composer require symfony/intl # Try the CLI bin/icu lint translations/
Requirements: PHP 8.2+ and ext-intl (the MessageFormatter class).
What ICU Parser provides
- 🏗️ Deep Parsing: Parse ICU MessageFormat strings into a structured, typed AST.
- 🔍 Type Inference: Extract parameter types and their expected formats from messages.
- ✅ Validation: Detect syntax errors, missing plural categories, and semantic issues.
- 📊 Catalog Analysis: Analyze translation files for consistency and completeness.
- 🎨 Syntax Highlighting: Colorize ICU messages for better readability.
- 🔧 Visitor API: A flexible API for building custom ICU analysis tools.
- 🛠️ CLI Tooling: Lint, audit, debug, and highlight messages from the command line.
Philosophy & Scope
ICU Parser is a static analysis tool, not a runtime formatter.
What it does
- Parse: Converts ICU MessageFormat strings into an AST.
- Validate: Checks for syntax errors and common semantic issues.
- Analyze: Infers types, formats messages, and highlights syntax.
- Lint: Scans translation files for errors and inconsistencies.
What it does NOT do
- Runtime Formatting: It does not format messages at runtime. Use
MessageFormatterfromext-intlfor that. - Complete ICU Coverage: It supports the most common ICU constructs but does not cover every edge case or locale-specific nuance.
- MessageFormat 2 (MF2): It targets ICU MessageFormat 1.x only.
Validation notes
- Well-tested behavior: Parsing, AST structure, error offsets, and syntax validation for the supported ICU syntax.
- Heuristic: Semantic validation is conservative and may miss edge cases. Treat it as a helpful tool, not a complete validator.
- Context matters: Locale-specific rules (e.g., plural categories) vary. Use
symfony/intlfor better locale support.
How it works
IcuParser::parse()converts an ICU string into an AST.- The lexer produces a token stream.
- The parser builds an AST (
MessageNode). - Visitors walk the AST to validate, infer types, highlight, or format.
For the full architecture, see docs/ARCHITECTURE.md.
CLI quick tour
# Parse and validate a message bin/icu debug "{count, plural, one {# item} other {# items}}" # Highlight a message bin/icu highlight "{name}, you have {count, number} items" # Lint translation files bin/icu lint translations/ # Audit translation catalogs bin/icu audit translations/
PHP API at a glance
use IcuParser\IcuParser; use IcuParser\Validation\SemanticValidator; use IcuParser\Highlight\HighlightTheme; use IcuParser\Type\ParameterType; $parser = new IcuParser(); // Parse a message into AST $ast = $parser->parse('Hello {name}, you have {count, plural, one {# item} other {# items}}'); // Infer parameter types $types = $parser->infer('{count, number} {date, date, short}')->all(); // Returns: ['count' => ParameterType::NUMBER, 'date' => ParameterType::DATETIME] // Validate semantics $validator = new SemanticValidator(); $result = $validator->validate($ast, 'Hello {name}', 'en'); if (!$result->isValid()) { foreach ($result->getErrors() as $error) { echo $error->getMessage() . "\n"; } } // Format for readability $pretty = $parser->format('{gender, select, male {He} other {They}}'); // Output: // {gender, select, // male {He} // other {They} // } // Highlight syntax $highlighted = $parser->highlight('{count, number}', HighlightTheme::ansi());
Integrations
ICU Parser integrates with common PHP tooling:
- Symfony bundle: See docs/overview.md
- PHPStan: PHPStan rule (early/experimental)
- Twig: Twig extractor (used by the audit command; requires Twig)
- GitHub Actions: Use
bin/icu lintin your CI pipeline
Documentation
Start here:
- Quick Start - Get started in a few minutes
- Overview - Scope and capabilities
- Usage Guide - Detailed API usage
- ICU Support - Supported constructs
Key references:
- Architecture - Internal design
- Formatting - Formatting and highlighting
Contributing
Contributions are welcome! See CONTRIBUTING.md to get started.
# Set up development environment composer install # Run tests composer phpunit # Check code style composer phpcs # Run static analysis composer phpstan # Run full lint (includes all of the above) composer lint
License
Released under the MIT License.
Support
If you run into issues or have questions, please open an issue on GitHub: https://github.com/yoeunes/icu-parser/issues.
yoeunes/icu-parser 适用场景与选型建议
yoeunes/icu-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「i18n」 「cli」 「parser」 「ast」 「lexer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yoeunes/icu-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yoeunes/icu-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yoeunes/icu-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Nette Framework adapter for I18n package
Yii2 integration for AirBnB Polyglot.js
Slim starter / Slim skeleton package to boost your development with Slim framework
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-10