yiisoft/translator
yiisoft/translator is a lightweight PHP translation library for managing messages, locales, and pluralization. Integrates with Yii and PSR-style components, supports multiple message sources and fallback locales, and is suitable for apps and libraries needing i18n.
translated events or via message queues like RabbitMQ).App\Services layer, config/, and resources/lang/ directories. Works with Laravel’s built-in localization (e.g., __() helper) but extends it with dynamic catalogs, pluralization, and context-aware translations.yiisoft/translator's MessageSource interfaces.*.php/JSON) and dynamic databases (MySQL, PostgreSQL) for translations, enabling runtime updates without redeploys.TranslationValidator middleware to enforce naming conventions (e.g., key.catalog.context).yiisoft/translator-laravel bridge package for older versions.en-US → en) be handled?key.v1, key.v2) be supported?MessageFormatter implementations.Translator with yiisoft/translator via service provider binding:
$app->bind(\Illuminate\Contracts\Translation\Translator::class, \Yiisoft\Translator\Translator::class);
config/app.php to define translation sources (e.g., catalogs array for multi-source support).intl extension for ICU message formatting (plurals, gender). Test for mbstring compatibility if handling non-Latin scripts.yiisoft/translator's make:translation Artisan commands (if wrapped) or integrate with Laravel’s lang:publish..php/JSON translation files with custom schemas.// Old way
__('key');
// New way (via facade or service)
translator()->translate('key', ['context' => 'dashboard']);
TranslationService facade to abstract differences.AppServiceProvider bindings and update __() calls to support contexts:
__('key', [], 'catalog', 'context');
auth.login.title → auth.title with context=login).MessageSource for database-backed translations.TranslationObserver to sync changes between catalogs and APIs.illuminate/support polyfills).ArrayObject changes or use a wrapper.yiisoft/translator is lightweight (~1MB) and avoids Laravel’s illuminate/translation directly.intl for ICU; redis for caching (optional).__() calls for context dependencies (e.g., {{ __('welcome', ['name' => $user->name]) }}).resources/catalogs/auth.php).TranslationSync cron job to pull updates from APIs (e.g., every 6 hours).auth.login.title.v2) or use a TranslationKey enum to track changes.TranslationDeprecator service (e.g., logs warnings when old keys are used).phpunit test to validate all translation keys are defined.Translator::missing() calls via Sentry or Laravel’s debugbar.YII_DEBUG to log translation lookups and fallbacks.TranslationDebugger middleware to dump context for missing keys in dev environments.translate:export CLI command to generate CSV/Excel for translators.translation route to let admins override translations on-the-fly (e.g., for promotions).cache()->remember() for user-specific overrides.locale or catalog to reduce query scope.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Translation API downtime | Missing translations for dynamic keys | Fallback to static catalogs; notify admins via Slack/PagerDuty. |
| Cache eviction storm | Thundering herd of API calls | Implement circuit breakers (e.g., yiisoft/circuit-breaker). |
| Database lock contention | Slow translation lookups | Use optimistic locking or read-only replicas for catalog queries. |
| Key collision in merged catalogs | Overwritten translations | Use UUIDs for dynamic keys or implement a TranslationConflictResolver. |
| Unsupported locale | Broken rendering (e.g., RTL) | Default to en |
How can I help you explore Laravel packages today?