Installation:
composer require argentum/translation-bundle:dev-master
Register the bundle in AppKernel.php:
new Argentum\TranslationBundle\ArgentumTranslationBundle(),
Configure Locales:
In parameters.yml:
parameters:
locales: ['en', 'fr', 'es']
Enable Dependencies:
In config.yml:
imports:
- { resource: ../../vendor/knplabs/doctrine-behaviors/config/orm-services.yml }
bundles:
- A2lix\TranslationFormBundle\A2lixTranslationFormBundle
argentum_translation:
locales: %locales%
a2lix_translation_form:
locales: %locales%
Create Database Schema:
php app/console doctrine:schema:update --force
First Use Case:
Access the Sonata Admin panel at /admin/argentum/translation/translationdomain/list to create translation domains and entries.
Define Domains:
Use the Sonata Admin GUI to create translation domains (e.g., messages, validation). Each domain maps to a logical group of translations (e.g., form labels, error messages).
Add Translations:
For each domain, add translation keys (e.g., homepage.title) and their values per locale via the GUI. The a2lix/translation-form-bundle provides a user-friendly form for this.
Override Defaults:
Database translations override file-based translations (e.g., Resources/translations/messages.en.yml). Use this to customize translations without modifying bundle files.
Integrate with Twig: Use the standard Symfony translation syntax in templates:
{{ 'homepage.title'|trans }}
Export/Import:
php app/console translation:export --dir=translations --format=yml
php app/console translation:import --dir=translations
Use the Translatable behavior (from knplabs/doctrine-behaviors) to make entity fields translatable:
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
class Page
{
use Translatable;
// ...
}
Configure the behavior in config.yml:
knp_doctrine_behaviors:
translatable:
mappings:
AppBundle\Entity\Page:
fields: [title, content]
php app/console cache:clear
Locale Configuration:
%locales% is defined in parameters.yml before configuring argentum_translation or a2lix_translation_form. Missing locales will break the admin panel.Schema Updates:
doctrine:schema:update --force drops existing tables. Use migrations for production:
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
Sonata Admin Menu:
sonata_admin.dashboard.groups in config.yml:
sonata_admin:
dashboard:
groups:
Translation: ~
Translation Loading Order:
Resources/translations/) load before database translations. Database entries will override files, but files are still required for fallback.PHP 7+ Compatibility:
Missing Translations:
translation_domain table).%locales%.Admin Panel Issues:
sonata-project/doctrine-orm-admin-bundle is installed (required for Sonata Admin to work).Export/Import Failures:
--format=yml for debugging to see the parsed translation files.--dir) is writable.Custom Translation Domains:
Extend the TranslationDomain entity to add custom fields (e.g., priority, author):
namespace Argentum\TranslationBundle\Entity;
class TranslationDomain extends \Sonata\DoctrineORMAdminBundle\Model\BaseTranslationDomain
{
// Add custom fields here
}
Update the database schema and admin configuration accordingly.
Override Export/Import:
Extend the TranslationExporter and TranslationImporter services to add custom logic (e.g., validation, logging):
services:
app.custom_translation_exporter:
class: AppBundle\Service\CustomTranslationExporter
parent: argentum_translation.exporter
tags:
- { name: argentum_translation.exporter }
Add Translation Sources:
Extend the loader to support additional sources (e.g., API calls, external databases) by implementing Argentum\TranslationBundle\Loader\LoaderInterface.
Customize Sonata Admin:
Override the admin class for TranslationDomain to add filters, actions, or custom fields:
namespace AppBundle\Admin;
use Argentum\TranslationBundle\Admin\TranslationDomainAdmin;
class CustomTranslationDomainAdmin extends TranslationDomainAdmin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('name');
$listMapper->add('createdAt', 'datetime', ['template' => 'ArgentumTranslationBundle:Admin:field_datetime.html.twig']);
}
}
Register the override in config.yml:
sonata_admin:
options:
models:
Argentum\TranslationBundle\Entity\TranslationDomain:
admin_service: app.custom_translation_domain_admin
How can I help you explore Laravel packages today?