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.
Start by installing the package via Composer:
composer require symfony/translation-contracts
This package provides only interfaces — it has no concrete implementation. Your first use case will likely be integrating translation into your own framework or library (e.g., writing a custom translator that implements Symfony\Contracts\Translation\TranslatorInterface). Read the interfaces in src/Translation/ — especially TranslatorInterface, TranslatableInterface, and MessageCatalogueInterface. These define the contract for translating messages, handling parameters, pluralization, and locale switching.
TranslatorInterface to build a translator for your stack (e.g., using PO files, JSON, or a database). Use TranslatorTrait to reduce boilerplate.TranslatableInterface objects (e.g., Symfony\Contracts\Translation\TranslatableMessage) instead of raw strings to defer translation and preserve context.MessageCatalogueInterface to manage locales and message domains — useful when building i18n-aware CLI tools or headless CMS backends.TranslatorInterface wherever translation is needed, promoting testability and testability-friendly design (e.g., mock in unit tests).symfony/translation package) to actually translate messages in production.trans() method uses :placeholder syntax by default — but real-world implementations (like symfony/translation) support ICU MessageFormat ({count, plural, =0{...}}) — ensure your translator supports the format you expect.trans() but not helper methods like transChoice(). Implementations often add their own — don’t rely on them unless using symfony/translation.TranslatorInterface, set strict expectations on locale and domain — mismatches here are common sources of silent translation failures.TranslatorTrait offers fallback and parameter substitution logic — reuse it when building lightweight translators.trans() — the contract assumes your implementation respects locale changes (e.g., setLocale() or via context-aware resolvers).How can I help you explore Laravel packages today?