yiisoft/translator-message-php
PHP message source for Yii Translator that loads translations from PHP files/arrays. Provides a simple, fast backend for storing and retrieving localized messages, suitable for Yii apps needing file-based i18n without a database.
yiisoft/translator-message-php) is a message storage layer for internationalization (i18n) in PHP, designed to complement Yii’s translator component. It abstracts message storage (e.g., JSON, YAML, database) and retrieval, making it ideal for Laravel applications requiring scalable, structured, and maintainable localization.trans() helper and lang directory (JSON/PO) are compatible with this package’s design (message catalogs as structured data).translated events) for extensibility.lang files lack.trans() helper relies on array-based message catalogs, while this package uses objects/collections for messages. A wrapper facade or service provider would be needed to bridge the gap.yiisoft/translator (Yii’s translator component) or a compatible alternative (e.g., symfony/translation).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| API Incompatibility | Medium | Build a Laravel facade to normalize output. |
| Performance Overhead | Low | Benchmark against Laravel’s native trans(). |
| Yii-Specific Patterns | Medium | Abstract Yii dependencies via interfaces. |
| Localization Cache | Low | Leverage Laravel’s cache (e.g., cache:forget). |
| Migration Complexity | Medium | Phase integration (start with non-critical locales). |
trans()?
trans() helper with a custom facade wrapping yiisoft/translator-message-php.lang/ directory).spatie/laravel-translation-loader) or API (e.g., Crowdin, Lokalise).yiisoft/translator-message-php's built-in caching layer.Phase 1: Parallel Integration
composer require yiisoft/translator-message-php
$this->app->bind(TranslatorInterface::class, function ($app) {
return new Translator([
'messageSource' => new FileMessageSource(__DIR__.'/../resources/lang'),
]);
});
trans():
Facade::register(TranslatorFacade::class, Translator::class);
Phase 2: Feature Parity
yiisoft/translator-message-php).App::getLocale()).en translation missing, fall back to en-US).Phase 3: Full Replacement
trans() in favor of the custom facade.lang/ files to the package’s expected format (if needed).lang/ directory structure (with minor adjustments).@lang may need a custom resolver.spatie/laravel-translation-loader) may conflict; evaluate alternatives.ICUMessageFormatter (not needed in Laravel).| Step | Task | Dependencies |
|---|---|---|
| 1 | Install package + basic binding | Composer, Laravel service container |
| 2 | Create facade for trans() |
Step 1 |
| 3 | Test with existing lang/ files |
Step 2 |
| 4 | Implement caching layer | Laravel cache, Step 3 |
| 5 | Add fallback logic | Step 4 |
| 6 | Deprecate native trans() |
All features tested |
yiisoft/yii-translator).php artisan for cache management.spatie/laravel-translation-loader).locale/message fields.trans() for high-traffic routes.| Scenario | Impact | Mitigation |
|---|---|---|
| Message not found | Broken UI | Implement robust fallback chain (e.g., en → en-US → en-GB). |
| Storage backend failure (e.g., DB down) | Translations missing | Cache static fallbacks; log errors. |
| Facade misconfiguration | trans() returns null |
Use Laravel’s tap() for debugging. |
| PHP version incompatibility | Runtime errors | Pin PHP version in composer.json. |
| Cache corruption | Stale translations | Use cache:clear in deployments. |
How can I help you explore Laravel packages today?