elao/form-translation-bundle
Installation
Add the bundle to your composer.json:
composer require elao/form-translation-bundle
Register it in config/bundles.php:
return [
// ...
Elao\FormTranslationBundle\ElaoFormTranslationBundle::class => ['all' => true],
];
Enable in Forms
Extend your form type with Elao\FormTranslationBundle\Form\TranslationAwareInterface and implement getTranslationKey():
use Elao\FormTranslationBundle\Form\TranslationAwareInterface;
class RegisterType extends AbstractType implements TranslationAwareInterface
{
public function getTranslationKey(): string
{
return 'form.register';
}
}
First Use Case
Use TranslationAwareTrait in child fields to auto-generate nested keys:
use Elao\FormTranslationBundle\Form\TranslationAwareTrait;
class EmailFieldType extends AbstractType implements TranslationAwareInterface
{
use TranslationAwareTrait;
public function getTranslationKey(): string
{
return 'emails'; // Nested under parent's key (e.g., form.register.children.emails)
}
}
Form Hierarchy
form.register).form.register.children.name).label_add, label_delete, and prototype keys.Dynamic Key Generation
TranslationAwareTrait for child fields to inherit parent keys:
class AddressType extends AbstractType implements TranslationAwareInterface
{
use TranslationAwareTrait;
public function getTranslationKey(): string
{
return 'address'; // Auto-prepended to parent (e.g., form.register.children.address)
}
}
Integration with Symfony Forms
buildForm() to inject translation keys:
$builder->add('name', TextType::class, [
'label' => $this->translator->trans('form.register.children.name.label'),
]);
TranslationAwareInterface for form types that need dynamic keys.Collection Handling
CollectionType, the bundle auto-generates:
label_add (e.g., form.register.children.emails.label_add).label_delete (e.g., form.register.children.emails.label_delete).prototype keys for nested fields.Translation Files
translations/messages.{locale}.yml:
form:
register:
children:
name:
label: "Full Name"
emails:
label: "Email Addresses"
label_add: "Add Email"
label_delete: "Remove Email"
children:
prototype:
label: "Email"
Dynamic Keys with Data
Use getTranslationKey() to generate keys based on runtime data:
public function getTranslationKey(): string
{
return sprintf('form.user.%s', $this->userId);
}
Custom Key Prefixes
Override getTranslationPrefix() in child types:
public function getTranslationPrefix(): string
{
return 'custom_prefix.';
}
Event Listeners
Subscribe to ElaoFormTranslationBundle.EventListener.FormTranslationEvent to modify keys dynamically:
$eventDispatcher->addListener(
ElaoFormTranslationBundle::EVENT_TRANSLATION_KEY,
function (FormTranslationEvent $event) {
$event->setKey(strtoupper($event->getKey()));
}
);
Key Collisions
getTranslationPrefix() to avoid overlaps.form.user.name and form.user.profile.name in the same form.Translation Update
translation:update won’t dump them. Manually add expected keys to .yml files.Collection Prototype Issues
prototype keys don’t render, verify:
entry_type with TranslationAwareInterface.prototype key is correctly nested (e.g., form.register.children.emails.children.prototype.label).Caching Translations
php bin/console cache:clear
Symfony 5.4+ Deprecations
TranslationAwareInterface methods are public and non-abstract.Log Generated Keys Add a listener to log keys during form rendering:
$eventDispatcher->addListener(
ElaoFormTranslationBundle::EVENT_TRANSLATION_KEY,
function (FormTranslationEvent $event) {
error_log('Generated key: ' . $event->getKey());
}
);
Check Parent Keys
If a child key is malformed, verify its parent’s getTranslationKey() implementation.
Override Default Behavior
Extend Elao\FormTranslationBundle\KeyGenerator\KeyGenerator to customize key logic:
class CustomKeyGenerator extends KeyGenerator
{
public function generateKey(TranslationAwareInterface $form, string $suffix = ''): string
{
return parent::generateKey($form, '.custom_suffix');
}
}
Register it in services.yaml:
services:
Elao\FormTranslationBundle\KeyGenerator\KeyGenerator:
class: App\CustomKeyGenerator
private $cachedKeys = [];
public function getTranslationKey(): string
{
if (!isset($this->cachedKeys[$this->userId])) {
$this->cachedKeys[$this->userId] = sprintf('form.user.%s', $this->userId);
}
return $this->cachedKeys[$this->userId];
}
Custom Key Generators
Implement Elao\FormTranslationBundle\KeyGenerator\KeyGeneratorInterface for bespoke logic.
Event Subscribers
Extend ElaoFormTranslationBundle.EventListener.FormTranslationEvent to modify keys or validate them.
Twig Integration Use the bundle’s keys in Twig templates:
{{ 'form.register.children.name.label'|trans }}
Or with dynamic keys:
{% set key = form._translationKey %}
{{ (key ~ '.label')|trans }}
How can I help you explore Laravel packages today?