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 Message Php

Translator Message Php Laravel Package

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.

Deep Wiki
Context7

yiisoft/translator-message-php provides a PHP message source for the Yii Translator, enabling translation messages to be loaded from plain PHP files. It’s a lightweight, framework-friendly package designed for projects that want simple, performant localization without external formats or heavy tooling.

Use it to organize message catalogs per category/locale, keep translations in version control, and integrate cleanly with Yii’s translation pipeline.

  • Loads translations from PHP array files
  • Supports locales and message categories organization
  • Fast, minimal dependencies, easy to deploy
  • Works seamlessly with yiisoft/translator components
  • Simple structure ideal for small to medium i18n setups
Frequently asked questions about Translator Message Php
Can I use yiisoft/translator-message-php to replace Laravel’s built-in trans() helper for dynamic translations?
Yes, but you’ll need a custom facade or service provider to bridge the gap. The package outputs message objects, while Laravel’s trans() expects arrays. Start by wrapping the package in a facade that normalizes output to match Laravel’s format. This approach works well for dynamic translations like user-generated content or API-driven locales.
How do I install and configure this package in a Laravel project?
Run `composer require yiisoft/translator-message-php` and bind the translator in a service provider. For example, use `FileMessageSource` to load JSON/PO files from your `resources/lang` directory. Then create a facade to mimic Laravel’s `trans()` helper. The package requires `yiisoft/translator` or a compatible alternative like `symfony/translation`.
Does this package support Laravel’s pluralization rules (e.g., {0, plural, one {item} other {items}})?
Not natively, but you can extend the package to support Laravel’s pluralization syntax. The underlying Yii translator supports basic pluralization, so you’ll need to implement a custom formatter or message parser to handle Laravel’s specific rules. Check the package’s `MessageFormatter` for extensibility points.
Will this work with Laravel’s cache system (e.g., Redis) for translated messages?
Yes, the package has built-in caching, but you can integrate it with Laravel’s cache system for better performance. Use Laravel’s `cache:forget` or `cache:remember` to manage translated messages. The package’s `MessageSource` interfaces allow you to layer caching logic, so you can cache messages at the source level.
Can I store translations in a database instead of JSON/PO files with this package?
Absolutely. The package is backend-agnostic and supports custom `MessageSource` implementations. For databases, you could create a `DatabaseMessageSource` that queries a table like `translations`. Laravel packages like `spatie/laravel-translation-loader` can also complement this setup for seamless database-backed translations.
What Laravel versions does yiisoft/translator-message-php support?
The package itself is PHP 8.1+ compatible, which aligns with Laravel 9+. However, Laravel’s native `trans()` helper relies on older patterns, so you’ll need to build a facade or service provider to ensure compatibility. Test thoroughly with your Laravel version, as some edge cases (like locale negotiation) may require custom logic.
How do I handle missing translations or fallback locales with this package?
The package supports fallback chains out of the box. Configure your `Translator` instance with multiple `MessageSource` instances in order of priority. For example, set `en-US` as the primary source and `en` as the fallback. Laravel’s `App::setFallbackLocale()` can also integrate with this setup for consistency.
Are there alternatives to this package for dynamic translations in Laravel?
Yes, alternatives include `spatie/laravel-translation-loader` (for database-backed translations), `laravel-lang/lang` (for community-driven language packs), or `symfony/translation` (for Symfony’s i18n features). However, `yiisoft/translator-message-php` stands out for its structured message storage and support for non-file backends like APIs or databases.
Can I use this package in a Laravel microservice architecture where translations are shared across services?
Yes, this package is ideal for shared translation catalogs. Store messages in a centralized database or API, then use a custom `MessageSource` to fetch them across services. This avoids duplicating translation files and ensures consistency. Pair it with Laravel’s service container to inject the translator into all services.
How do I test translations loaded from this package in Laravel’s PHPUnit tests?
Mock the `MessageSource` in your tests to return predefined translations. Use Laravel’s `partialMock` or `createMock` to simulate the translator’s behavior. For example, mock a `FileMessageSource` to return hardcoded messages, then assert the output matches expectations. This approach works for both static and dynamic translation scenarios.
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