koff/i18n-form-bundle

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality StyleCI Coverage Status

This bundle is fork of a2lix/TranslationFormBundle, reorganized and optimized to work with symfony flex

Requirements

Installation

composer req koff/i18n-form-bundle

Configuration

Full configuration example

# config/packages/i18n_form.yaml
i18n_form:
    locales: [en, fr, es, de]
    required_locales: [fr]
    excluded_fields: ['id', 'locale', 'translatable']
    form_theme: 'bootstrap_3' # you can use 'bootstrap_3' or 'bootstrap_4' form template

Usage

Basic example

use Koff\I18nFormBundle\Form\Type\TranslationsType;
//...
$builder->add('translations', TranslationsType::class);

Advanced example

use Koff\I18nFormBundle\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]
]);

Additional

TranslationsFormsType

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

use Koff\I18nFormBundle\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'
    ]
]);

TranslatedEntityType

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

use Koff\I18nFormBundle\Form\Type\TranslatedEntityType;
//...
$builder->add('medias', TranslatedEntityType::class, [
    'class' => 'App\Entity\Media',      // [1 - Mandatory]
    'translation_property' => 'text',   // [2 - Mandatory]
    'multiple' => true,                 // [3]
]);

Credits

All credits goes to David ALLIX and his a2lix/TranslationFormBundle

License

This package is available under the MIT license.