Translate your doctrine objects easily with some helps
Branch | Tools |
---|---|
3.x (master) |
3.x
depends on AutoFormBundle and has higher requirements (PHP8.1+, Symfony5.4+/6.3+/7.0+). It is compatible with KnpLabs, A2lix and PrezentUse composer:
composer require a2lix/translation-form-bundle
After the successful installation, add/check the bundle registration:
// Symfony >= 4.0 in bundles.php
// ...
A2lix\AutoFormBundle\A2lixAutoFormBundle::class => ['all' => true],
A2lix\TranslationFormBundle\A2lixTranslationFormBundle::class => ['all' => true],
// ...
// Symfony >= 3.4 in AppKernel::registerBundles()
$bundles = array(
// ...
new A2lix\AutoFormBundle\A2lixAutoFormBundle(),
new A2lix\TranslationFormBundle\A2lixTranslationFormBundle(),
// ...
There is no minimal configuration. Full list of optional parameters:
# Symfony >= 4.0. Create a dedicated a2lix.yaml in config/packages with:
# Symfony >= 3.4. Add in your 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: "@A2lixTranslationForm/bootstrap_4_layout.html.twig" # [2]
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
...
$builder->add('translations', TranslationsType::class);
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]
'locale_labels' => [ // [5]
'fr' => 'Français',
'en' => 'English',
],
]);
A different approach for entities which don't share fields untranslated. No strategy used here, only a locale field in your entity.
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'
]
]);
Modified version of the native 'entity' symfony form type to translate the label in the current locale by reading translations
use A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType;
...
$builder->add('medias', TranslatedEntityType::class, [
'class' => 'A2lix\DemoTranslationBundle\Entity\Media', // [1 - Mandatory]
'translation_property' => 'title', // [2 - Mandatory]
'multiple' => true, // [3]
]);
See Demo Bundle for more examples.
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer install --ignore-platform-reqs
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer run-script phpunit
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer run-script cs-fixer
This package is available under the MIT license.