0. Screenshot

1. Requirement

  • Symfony >= v2.3 but Symfony < 3.x
  • Your Doctrine objects configured with an i18n strategy

Some i18n strategies that I can advise you, order by preference :

  1. A2lixI18nDoctrineBundle An indexBy strategy
  2. KnpDoctrineExtension An indexBy strategy PHP5.4
  3. PrezentDoctrineTranslatableBundle An indexBy strategy PHP5.3
  4. GedmoDoctrineExtension (with persistDefaultLocaleTranslation enabled)
  5. GedmoDoctrineExtension (with its default configuration)

2. Installation

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(),
// ...
);

3. Configuration

There is no minimal configuration. Full list:

# app/config/config.yml
a2lix_translation_form:
    locales: [en, fr, es, de]       # [1]
    default_required: true          # [2]
    manager_registry: doctrine      # [3]
    templating: "A2lixTranslationFormBundle::default.html.twig"      # [4]
  • [1] Locales of translations
  • [2] Default to true
  • [3] Default to doctrine
  • [4] The default template is Twitter Bootstrap compatible. You can redefine your own template
About the locales list, GedmoDoctrineExtension (with its default configuration) is a specific case because you can choose to include or not the default locale with translations locales to regroup all locales in tabs.

4. Usage

For indexBy strategies

In a classic formType

$builder->add('translations', 'a2lix_translations');

Gedmo strategy specificity

You have to use 'a2lix_translations_gedmo' instead of 'a2lix_translations'

If you use GedmoDoctrineExtension (with its default configuration) with the choice to include the default locale with translations locales, you need to specify the 'translatable_class' option.

$builder->add('translations', 'a2lix_translations_gedmo', array(
    'translatable_class' => "Entity\Product"
);

Advanced examples

$builder->add('translations', 'a2lix_translations', array(
    'locales' => array('en', 'fr', 'es', 'de'),   // [1]
    'required' => false,                    // [2]
    'fields' => array(                      // [3]
        '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]
            )
        )
    )
));
  • [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'

5. Additional

Quick forms

$builder->add('translations', 'a2lix_translationsForms', array(
    'locales' => array('fr', 'es', 'de'),       // [1]
    'form_type' => new ProductMediaType(),         // [2]
    'form_options' => array(                       // [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

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.

6. Advanced

If you use PHP5.4, you can use some traits provided in Util repository

If you use GedmoDoctrineExtension (with its default configuration) and a multilingual backoffice, you need, during edition of your forms, to fix the current locale to the default locale.
I provide an annotation 'GedmoTranslation' for help you.

use A2lix\TranslationFormBundle\Annotation\GedmoTranslation;
...

/**
 * New/Edit object
 *
 * @Route("/new", defaults={"id"=""}, name="backend_product_new")
 * @Route("/{id}/edit", name="backend_product_edit")
 * @Template()
 * @GedmoTranslation
 */
public function editAction($id = null)
{
...