• Symfony >= v2.8 (and so, 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

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\AutoFormBundle\A2lixAutoFormBundle(),
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]
    templating: "A2lixTranslationFormBundle::default.html.twig"      # [2]
  • [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] The default template is Twitter Bootstrap compatible. You can redefine your own here

In a classic formType

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

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

Advanced examples

// Symfony >= 3.0
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
...
$builder->add('translations', TranslationsType::class, [
    'locales' => ['en', 'fr', 'es', 'de'],   // [1]
    'default_locale' => ['en']               // [1]
    'required_locales' => ['fr'],            // [1]
    'fields' => [                               // [2]
        'description' => [                         // [3.a]
            'field_type' => 'textarea',                // [4]
            'label' => 'descript.',                    // [4]
            'locale_options' => [                  // [3.b]
                'es' => ['label' => 'descripciĆ³n']     // [4]
                'fr' => ['display' => false]           // [4]
            ]
        ]
    ],
    'excluded_fields' => ['details']            // [2]
]);
  • [1] Optionnal. If set, override the default value from config.yml
  • [2] Optionnal. If set, override the default value from config.yml
  • [3] Optionnal. 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'

TranslationsFormsType

A different approach for entities which don't share fields untranslated. No strategy used here, only a locale field in your entity.

// Symfony >= 3.0
use A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType;
...
$builder->add('translations', TranslationsFormsType::class, [
    'locales' => ['en', 'fr', 'es', 'de'],   // [1]
    'default_locale' => ['en']               // [1]
    'required_locales' => ['fr'],            // [1]
    'form_type' => ProductMediaType::class,     // [2 - Mandatory]
    'form_options' => [                         // [2bis]
         'context' => 'pdf'
    ]
]);
  • [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

TranslatedEntityType

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

// Symfony >= 3.0
use A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType;
...
$builder->add('medias', TranslatedEntityType::class, [
    'class' => 'A2lix\DemoTranslationBundle\Entity\Media',   // [1 - Mandatory]
    'translation_property' => 'text',                           // [2 - Mandatory]
    'multiple' => true,                                             // [3]
));
  • [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, ...)

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.