symfony/translation-contracts
Symfony Translation Contracts provides lightweight interfaces and abstractions for translation in PHP, extracted from Symfony components. Use it to build interoperable, battle‑tested translation integrations while staying framework-agnostic and compatible with Symfony implementations.
TranslatorInterface, MessageCatalogueInterface) align with Laravel’s existing translation system (which already uses Symfony’s Translation component under the hood). This enables seamless integration without disrupting Laravel’s native trans() helper or language file structure.trans() helper already relies on Symfony’s Translator (which implements these contracts), so no breaking changes are required. Custom implementations can be registered via Laravel’s service provider bindings..php, .json, .po) remain compatible, as the contracts define only the interface, not the storage format.Translator acts as a safe baseline.trans() uses :placeholder syntax, but Symfony’s Translator supports ICU MessageFormat. Custom implementations must align with expected syntax.setLocale(), but Laravel’s global locale may need explicit synchronization.TranslatorInterface be mocked in unit tests, or is a lightweight in-memory implementation sufficient?App::setLocale()?trans() calls that rely on Laravel-specific features (e.g., trans_choice) not covered by the contracts?symfony/translation, symfony/http-foundation). Leverages shared abstractions for consistency.trans() usage (e.g., static strings, dynamic parameters, pluralizations).transChoice support) and plan custom implementations.trans() helper.
composer require symfony/translation-contracts
config/app.php or a service provider:
$app->bind(
Symfony\Contracts\Translation\TranslatorInterface::class,
App\Services\CustomTranslator::class
);
Translator dependencies with the interface in new code.resolve() method to inject the interface where needed.Translator already implements these contracts.trans() helper (it won’t—it’s purely additive).GoogleTranslateAdapter implementing TranslatorInterface).trans() usage.setLocale() calls) or custom implementation bugs.TranslatorTrait to reduce boilerplate and standardize behavior.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Custom translator throws exceptions | Silent translation failures | Implement fallback translators (e.g., file-based). |
| Locale mismatch | Incorrect translations displayed | Validate locale context in tests. |
| API-based translator downtime | Missing translations | Cache responses; use fallback providers. |
| Parameter interpolation errors | Broken UI/API responses | Sanitize inputs; test edge cases. |
| Pluralization rule bugs | Grammatical errors in UI | Use ICU-compliant implementations. |
TranslatorInterface, TranslatableMessage).trans().trans() helper documentation.TranslatorInterface instead of concrete classes).How can I help you explore Laravel packages today?