Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Translator Laravel Package

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.

Deep Wiki
Context7

yiisoft/translator is a lightweight PHP package for managing application translations and localization in a clean, framework-friendly way. It provides a simple translator API and integrates well with message sources so you can keep language strings organized and easy to maintain.

Designed for modern PHP apps, it helps you render the right message for the current locale, with support for flexible categories and message formatting.

  • PSR-friendly translator interface and clean API
  • Multiple message sources via pluggable message loaders
  • Locale-aware translation with category/message key support
  • Message formatting (e.g., parameters) for dynamic strings
  • Easy integration with Yii and other PHP applications
Frequently asked questions about Translator
Can yiisoft/translator replace Laravel’s default Translator without breaking existing __() calls?
Yes, you can fully replace Laravel’s Translator by binding it in your service provider. For a smoother transition, use a hybrid approach where you wrap the new translator in a facade (e.g., `translator()`) while gradually updating `__()` calls to support context-aware translations. The package maintains backward compatibility with Laravel’s `__()` helper by default.
How does yiisoft/translator handle dynamic translations fetched from APIs like DeepL or Google Translate?
The package supports dynamic translations via MessageSource interfaces, allowing you to integrate with translation APIs seamlessly. You can configure a fallback chain (e.g., API → static files) and cache responses aggressively in Redis to minimize latency. For production, use lazy-loading to avoid blocking requests.
What Laravel versions does yiisoft/translator support, and are there workarounds for older versions?
The package is optimized for Laravel 8+ due to its dependency injection compatibility, but a bridge package (`yiisoft/translator-laravel`) can extend support to Laravel 7+. For versions below 7, you may need polyfills or manual service provider bindings. Always test thoroughly, as older Laravel versions might lack full DI support.
How can I manage translation keys for a large team where developers, designers, and translators collaborate?
Use a structured naming convention (e.g., `key.catalog.context`) and implement a `TranslationValidator` middleware to enforce consistency. For versioning, append suffixes like `key.v1` or `key.v2` and document deprecations in a `TRANSLATIONS.md` file. Tools like PHPStorm schemas can also help enforce key standards.
Does yiisoft/translator support right-to-left (RTL) languages like Arabic or Hebrew, and how?
Yes, the package supports RTL languages out of the box, but you’ll need the `mbstring` PHP extension for proper text handling. For complex formatting (e.g., bidirectional text), extend the `MessageFormatter` to include RTL-specific rules. Combine this with Laravel’s locale-specific CSS (e.g., `rtl.css`) for full RTL support.
How can I cache translations in Redis to improve performance for high-traffic Laravel apps?
Configure Redis as a cache driver in Laravel’s `config/cache.php` and use the `yiisoft/translator`'s built-in caching layer. For dynamic translations, cache API responses with a TTL (e.g., 24 hours) and invalidate the cache on updates. Use tags (e.g., `lang:en`) to selectively flush translations when specific language files change.
Can yiisoft/translator work with Laravel’s Inertia.js or API responses for frontend localization?
Absolutely. The package integrates seamlessly with Inertia.js by including translations in API responses (e.g., `return response()->json(['translations' => $translator->getCatalog('en')])`). For frontend frameworks like Vue or React, pass the translations as props or use Laravel’s `share()` method in your `AppServiceProvider` to make them globally available.
What are the risks of using dynamic translations in production, and how can I mitigate them?
Dynamic translations introduce latency risks if not cached properly, and API-based translations may fail if the service is unavailable. Mitigate these by implementing a fallback chain (e.g., API → static files) and using circuit breakers for external APIs. Log failed translations and set up alerts for API downtime to ensure graceful degradation.
How do I handle pluralization and gender-specific translations (e.g., for Russian or German) in Laravel?
Use ICU message syntax (e.g., `{n, plural, one {# item} other {# items}}`) in your translation files, which the package supports via the `intl` PHP extension. For gender-specific rules, extend the `MessageFormatter` or use context-aware keys (e.g., `greeting.male`, `greeting.female`). Test thoroughly with your target locales to ensure correctness.
Are there alternatives to yiisoft/translator for Laravel, and when might I choose one over the other?
Alternatives include Laravel’s built-in Translator (simpler but less flexible) or specialized packages like `spatie/laravel-translatable` (for Eloquent models). Choose yiisoft/translator if you need dynamic catalogs, API integrations, or microservice compatibility. For static translations with Eloquent, `spatie/laravel-translatable` might suffice, but it lacks the extensibility of yiisoft/translator.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport