coolshop/cool-sonata-translation-bundle
Installation
composer require coolshop/cool-sonata-translation-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Coolshop\CoolSonataTranslationBundle\CoolSonataTranslationBundle::class => ['all' => true],
Basic Configuration
Add minimal config in config/packages/cool_sonata_translation.yaml (Symfony 4+):
cool_sonata_translation:
defaultDomain: messages
driver: orm
localeManager: ['en', 'fr'] # Replace with your locales
First Use Case
use Coolshop\CoolSonataTranslationBundle\Admin\TranslationAdminExtension;
class YourAdmin extends AbstractCRUDController
{
public function configureTranslationFields(TranslationAdminExtension $extension)
{
$extension->addTranslationField('title', 'text'); // Field name, input type
}
}
php bin/console cache:clear) and visit the Sonata Admin panel.Field Translation Setup Define translatable fields in your admin class:
$extension->addTranslationField('description', 'textarea')
->setEmptyText('No description')
->setOptions(['rows' => 5]);
Locale-Specific Logic Dynamically adjust translations per locale:
$extension->setLocaleManager('@your_custom.locale_manager'); // Override default
Batch Operations
Use the TranslationAdminExtension to bulk-edit translations:
$extension->setEditableMode('popup'); // Switch between inline/popup
Integration with Forms Extend Symfony forms for translation-aware fields:
$builder->add('translations', TranslationType::class, [
'mapped' => false,
'label' => 'Translations',
]);
messages, validation) using defaultDomain or per-field:
$extension->addTranslationField('field', 'text', 'custom_domain');
localeManager:
localeManager: ['fr', 'en'] # 'en' as fallback for missing 'fr' translations
// services.yaml
Coolshop\CoolSonataTranslationBundle\Driver\TranslationDriverInterface: '@your.redis_driver'
Cache Dependencies
php bin/console sonata:cache:clear
cache:pool:clear sonata.cache.admin for targeted clearing.Locale Manager Conflicts
localeManager service not found or misconfigured.cool_sonata_translation.locale_manager.sonatapage) or use an array fallback:
localeManager: ['en', 'fr'] # Array mode bypasses service lookup
ORM Driver Limitations
TranslationDriverInterface.Field Type Mismatches
checklist requires extra config).services.yaml:
Coolshop\CoolSonataTranslationBundle\Form\Type\TranslationType:
arguments:
- ['text', 'textarea', 'your_custom_type']
Enable Debug Mode: Add to config/packages/dev/cool_sonata_translation.yaml:
debug: true
Logs translation operations to var/log/dev.log.
Check Event Listeners: Verify sonata.admin.event.DISPLAY listeners are active:
php bin/console debug:event-dispatcher
Custom Translation Storage Override the ORM driver by binding a custom service:
# config/services.yaml
Coolshop\CoolSonataTranslationBundle\Driver\TranslationDriverInterface: '@app.custom_translation_driver'
UI Customization Extend Twig templates:
{# Override translation field template #}
@CoolSonataTranslation/translation/_field.html.twig
Validation Rules Add constraints to translation fields:
$extension->addTranslationField('slug', 'text')
->addConstraint(new NotBlank());
How can I help you explore Laravel packages today?