Installation:
composer require connectsb/translationbundle:dev-master
Add to AppKernel.php:
new ConnectSB\TranslationBundle\ConnectSBTranslationBundle(),
Configure config.yml:
connect_sb_translation:
database_translations_domain: "app" # Your translation domain (e.g., "app", "validation")
database_translations_entity: "Translation" # Your entity name (plural)
Extend Base Entities:
Create TranslationKey and TranslationValue extending BaseTranslationKey/BaseTranslationValue (see README example). Define relationships to your domain entity (e.g., @ORM\ManyToOne(targetEntity="YourEntity")).
First Use Case:
$translator = $this->get('translator');
$translator->trans('your.key', [], 'app'); // Uses DB-backed translations
Resources/config/routing.yml for routes).Translation Management:
Product entity’s translations are stored in TranslationKey/TranslationValue tied to the Product via a relationship).Integration with Symfony Translator:
# services.yml
translator:
class: Symfony\Component\Translation\Translator
arguments:
- "%kernel.default_locale%"
- { path: "%kernel.root_dir%/Resources/translations" } # Fallback to YAML/JSON
- { db: true } # Custom argument to enable DB lookup
Loader to fetch translations from the database:
// src/ConnectSB/TranslationBundle/Loader/DatabaseLoader.php
class DatabaseLoader extends Loader {
public function load($resource, $locale, $domain = null) {
// Query TranslationValue for the domain/locale and return as array
}
}
Localization Workflow:
TranslationKey (e.g., product.name).TranslationValue entries for each locale (e.g., en: "Product Name", fr: "Nom du Produit").product_id).Entity-Specific Translations:
// In your entity (e.g., Product)
/**
* @ORM\OneToMany(targetEntity="TranslationKey", mappedBy="product")
*/
private $translations;
Outdated Bundle:
symfony/translation v4+).Configuration Quirks:
database_translations_domain cannot be "messages" (reserved by Symfony). Use "app" or a custom domain.database_translations_entity name matches your extended entity (plural form).Performance:
// Cache the loaded translations for 1 hour
$cache = $this->get('translator.data_collector')->getTranslationCache();
$cache->set('app', $translations, 3600);
Admin UI Limitations:
Resources/views/Translation/key/edit.html.twig.Migration Risks:
TranslationKey/TranslationValue after initial setup may break existing data. Use Doctrine migrations carefully.Missing Translations:
TranslationValue exists for the locale/domain. Enable SQL logging:
doctrine:
dbal:
logging: true
config.yml:
framework:
translator:
fallbacks:
- "%locale%"
Entity Relationships:
mappedBy/inversedBy in your entity matches the relationship in TranslationKey.product_id (or similar) field is populated when saving.Locale-Specific Issues:
TranslationValue.Custom Validation:
TranslationKey/TranslationValue (e.g., unique keys per domain):
/**
* @Assert\Unique(entityManager="doctrine.orm.entity_manager", fields="domain", message="Key already exists.")
*/
private $key;
Event Listeners:
// src/EventListener/TranslationListener.php
class TranslationListener {
public function onTranslationUpdate(TranslationEvent $event) {
$this->get('cache')->clear('translations');
}
}
Register in services.yml:
services:
app.translation_listener:
class: AppBundle\EventListener\TranslationListener
tags:
- { name: kernel.event_listener, event: translation.update, method: onTranslationUpdate }
API Endpoints:
// src/Controller/TranslationController.php
class TranslationController extends Controller {
public function getTranslationsAction($domain, $locale) {
return $this->getDoctrine()->getRepository('ConnectSBTranslationBundle:TranslationValue')
->findByDomainAndLocale($domain, $locale);
}
}
How can I help you explore Laravel packages today?