Install Dependencies
composer require ao/translation-bundle stof/doctrine-extensions-bundle
Register Bundles
Add to AppKernel.php:
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
new AO\TranslationBundle\AOTranslationBundle(),
Configure Translator
In config.yml:
framework:
translator: ~
parameters:
translator.class: AO\TranslationBundle\Translation\Translator
ao_translation:
locales:
en: ~
de: { label: "German" }
Enable Timestampable Behavior
stof_doctrine_extensions:
orm:
default:
timestampable: true
Update Schema
php bin/console doctrine:schema:update --force
Add Routing
In routing.yml:
ao_translation:
resource: "@AOTranslationBundle/Controller/"
type: annotation
prefix: /
Use translations in a controller:
$this->get('translator')->trans('welcome.message', ['%name%' => 'John']);
Access the Translations Panel via Symfony Profiler (Debug Toolbar) to edit translations dynamically.
Translation Usage
Replace static strings with trans() calls:
$this->get('translator')->trans('user.greeting', ['%username%' => $user->name]);
Editing Translations
SonataAdminBundle and expose translations via /admin/ao/translation/message/list.Caching
php bin/console ao:translation:clear-cache
Locale-Specific Labels
Configure labels in ao_translation.locales for dropdowns in the GUI:
ao_translation:
locales:
en: ~
de: { label: "Deutsch" } # Custom label
Separate Database for Translations Use a dedicated connection for shared environments (e.g., multi-developer teams):
ao_translation:
entity_manager: translations
Run schema updates with:
php bin/console doctrine:schema:update --em=translations
Fallback Locales
Define fallback chains in config.yml:
framework:
translator:
fallbacks:
de: en
Translation Domains
Group translations by domain (e.g., admin, frontend):
$this->get('translator')->trans('admin.dashboard.title', [], 'admin');
Cache Staleness
php bin/console ao:translation:clear-cache
Missing Timestampable Behavior
stof_doctrine_extensions is misconfigured, translations won’t update timestamps.timestampable: true is set for the correct entity manager.Profiler Panel Not Showing
AOTranslationBundle is in AppKernel and the Profiler is enabled in dev environment.SonataAdmin Dependency
SonataAdminBundle but isn’t documented clearly.composer require sonata-project/admin-bundle sonata-project/doctrine-orm-admin-bundle
Locale Configuration Overrides
ao_translation.locales:
php bin/console cache:clear
Check Translations Table
Inspect ao_translation_message in your DB to verify entries:
SELECT * FROM ao_translation_message WHERE domain = 'messages';
Enable SQL Logging
Debug Doctrine queries in config.yml:
doctrine:
dbal:
logging: true
Log Translator Events Add a listener to track translation calls:
// src/AO/TranslationBundle/EventListener/TranslationListener.php
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if ($request->hasPreviousSession()) {
$this->logger->info('Translations used:', [
'domain' => $request->attributes->get('_route_params'),
]);
}
}
Custom Translation Providers
Extend the bundle’s Translator class to add logic (e.g., API-based fallbacks):
class CustomTranslator extends AO\TranslationBundle\Translation\Translator {
public function getTranslation($id, $locale, $domain = null) {
// Add custom logic (e.g., check API first)
return parent::getTranslation($id, $locale, $domain);
}
}
Register in config.yml:
parameters:
translator.class: AppBundle\Translation\CustomTranslator
Override Templates Customize the Profiler panel or SonataAdmin templates by overriding:
src/AO/TranslationBundle/Resources/views/
Add Validation Rules
Extend the Message entity to validate translations (e.g., max length):
// src/AO/TranslationBundle/Entity/Message.php
/**
* @Assert\Length(max=255)
*/
private $message;
Event Subscribers Hook into translation events (e.g., log changes):
// src/AO/TranslationBundle/EventListener/TranslationSubscriber.php
public static function getSubscribedEvents() {
return [
AOTranslationEvents::PRE_TRANSLATION_SAVE => 'onPreTranslationSave',
];
}
How can I help you explore Laravel packages today?