• Symfony >= v2.3 (and partial support of Symfony 3.x)
  • Your Doctrine objects configured with an i18n strategy of your choice

Strategies documentations :

  1. KnpDoctrineExtension An indexBy strategy PHP5.4+ Recommended
  2. A2lixI18nDoctrineBundle An indexBy strategy
  3. PrezentDoctrineTranslatableBundle An indexBy strategy PHP5.3

Read this blog post to help your choice.

Use composer

composer require a2lix/translation-form-bundle

After the successful installation, add in your AppKernel.php file

// in AppKernel::registerBundles()
$bundles = array(
// ...
new A2lix\TranslationFormBundle\A2lixTranslationFormBundle(),
// ...
);

There is no minimal configuration. Full list of optional parameters:

# app/config/config.yml
a2lix_translation_form:
    locale_provider: default       # [1]
    locales: [en, fr, es, de]      # [1-a]
    default_locale: en             # [1-b]
    required_locales: [fr]         # [1-c]
    manager_registry: doctrine     # [2]
    templating: "A2lixTranslationFormBundle::default.html.twig"      # [3]
  • [1] Custom locale provider service id. Default one relies on [1-*] values:
    • [1-a] List of translations locales to propose
    • [1-b] Default locale
    • [1-c] List of required translations locales
  • [2] Default to 'doctrine'
  • [3] The default template is Twitter Bootstrap compatible. You can redefine your own here

In a classic formType

// Symfony >= 2.3
$builder->add('translations', 'a2lix_translations');

// OR Symfony >= 3.0
$builder->add('translations', 'A2lix\TranslationFormBundle\Form\Type\TranslationsType');

Advanced examples

$builder->add('translations', 'a2lix_translations', array(
    'locales' => array('en', 'fr', 'es', 'de'),   // [1]
    'default_locale' => array('en')               // [1]
    'required_locales' => array('fr'),            // [1]
    'fields' => array(                               // [2]
        'description' => array(                         // [3.a]
            'field_type' => 'textarea',                     // [4]
            'label' => 'descript.',                         // [4]
            'locale_options' => array(                  // [3.b]
                'es' => array('label' => 'descripciĆ³n')     // [4]
                'fr' => array('display' => false)           // [4]
            )
        )
    ),
    'excluded_fields' => array('details')            // [2]
));
  • [1] Optionnal. Locales related. If set, override default values from config.yml
  • [2] Optionnal. Fields related. If set, override the auto configuration of fields
  • [3.a] Optionnal. - For a field, applied to all locales
  • [3.b] Optionnal. - For a specific locale of a field
  • [4] Optionnal. Common options of symfony forms (max_length, required, trim, read_only, constraints, ...), which was added 'field_type' and 'display'

a2lix_translationsForms

A different approach for translation entire entity. No strategy used here, only a locale field in your entity.

// Symfony >= 2.3
$builder->add('translations', 'a2lix_translationsForms', array(
    'locales' => array('en', 'fr', 'es', 'de'),   // [1]
    'default_locale' => array('en')               // [1]
    'required_locales' => array('fr'),            // [1]
    'form_type' => new ProductMediaType(),          // [2 - Mandatory]
    'form_options' => array(                        // [2bis]
         'context' => 'pdf'
    )
));

// OR Symfony >= 3.0
$builder->add('translations', 'A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType', array(
...
));
  • [1] Optionnal. If set, override the default value from config.yml
  • [2] Mandatory. A real form type that you have to do
  • [2bis] Optionnal. - An array of options that you can set to your form

a2lix_translatedEntity

Modified version of the native 'entity' symfony2 form type to translate the label in the current locale by reading translations

// Symfony >= 2.3
$builder->add('medias', 'a2lix_translatedEntity', array(
    'class' => 'A2lix\DemoTranslationBundle\Entity\Media',   // [1 - Mandatory]
    'translation_property' => 'text',                           // [2 - Mandatory]
    'multiple' => true,                                             // [3]
));

// OR Symfony >= 3.0
$builder->add('medias', 'A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType', array(
...
));
  • [1] Path of the translatable class
  • [2] Property/Method of the translatable class that will be display
  • [3] Common options of the 'entity' symfony2 form type (multiple, ...)
Current implementation required that translations exist for the current locale.

Assets

If you already use Twitter Bootstrap, you only need to enable the Tab functionality and use a2lix_translation_bootstrap.js.

Otherwise, you will still need jquery, and you use a2lix_translation_default.js and a2lix_translation_default.css.