kmergen / doctrine-translatable
Doctrine Translatable Bundle
Package info
github.com/kmergen/DoctrineTranslatable
Type:symfony-bundle
pkg:composer/kmergen/doctrine-translatable
Requires
- php: >=8.1
- doctrine/common: ^3.3
- doctrine/dbal: ^3.3
- doctrine/doctrine-bundle: ^2.6
- doctrine/orm: ^3.0
- doctrine/persistence: ^2.5|^3.0
- nette/utils: ^3.2
- ramsey/uuid: ^4.2
- symfony/cache: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/framework-bundle: ^5.4|^6.0
- symfony/http-kernel: ^5.4|^6.0
- symfony/security-core: ^5.4|^6.0
- symfony/string: ^5.4|^6.0
- symfony/translation-contracts: ^2.4|^3.0
Requires (Dev)
- ext-pdo_mysql: *
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.7.10
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.6
- psr/log: ^1.1
- rector/rector: ^0.13.4
- symplify/easy-ci: ^10.2.9
- symplify/easy-coding-standard: ^10.2.9
- symplify/package-builder: ^10.2.9
- symplify/phpstan-extensions: ^10.2.9
- symplify/phpstan-rules: ^10.2.9
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This symfony bundle provides a behavior to translate Doctrine entities in Symfony. Requires PHP >= 8.4, Symfony >= 8.1 and Doctrine ORM 3.
For other Symfony versions you should consider to use the DoctrineBehaviors bundle.
It currently handles:
Install
composer require kmergen/doctrine-translatable
Usage
All you have to do is to define a Doctrine entity:
- implemented interface
- add a trait
For further informations how to use it, you can also look the documentation of DoctrineBehaviors bundle.
Locale handling
TranslatableLocaleListener sets the current locale (request locale) and the default/fallback
locale (request default locale, %locale% or %kernel.default_locale%) on every translatable
entity on postLoad and prePersist. So $entity->translate() without argument returns the
translation of the current request locale and falls back to the default locale.
Before an entity is persisted the trait default de is used.
To use your own logic, alias Kmergen\DoctrineTranslatable\Contract\Provider\LocaleProviderInterface
to your own service.
Fetch modes
# config/services.yaml parameters: doctrine_behaviors_translatable_fetch_mode: LAZY # LAZY | EAGER | EXTRA_LAZY doctrine_behaviors_translation_fetch_mode: LAZY
PHPStan
Optional extension for precise return types of translate(), getTranslations() and getTranslatable():
includes: - vendor/kmergen/doctrine-translatable/extension.neon
Development
composer install composer test # PHPUnit (SQLite in-memory) composer phpstan # PHPStan level 6 vendor/bin/rector process --dry-run
Credits
This bundle is highly inspired by the excellent work of Knplaps on its bundle:
In fact, we have used some of its processes, commands, views, and some of its attribute names to somehow standardize the bundle and make it easier for us to build our own bundle for Symfony.