braunstetter/translated-forms

Translate your forms. Simple and easy.

Installs: 283

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v0.1.3 2022-12-03 19:13 UTC

This package is auto-updated.

Last update: 2024-03-30 00:37:49 UTC


README

Scrutinizer Code Quality Build Status License

This bundle gives you a translated option for your Symfony forms. So when you switch languages your forms are translated and work just fine.

It is assumed that you are using the KNP Doctrine Behaviours for your translations.

Installation

composer require braunstetter/translated-forms

Usage

Once installed this bundle lets you set an option inside your form parameter array:

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'translated' => true
    ]);
}

When this is set to true your form will react to the current locale inside your request object.

It is important to use Proxy Translations to allow this bundle to read data by a magic method.

Why should you use this bundle?

There is the popular a2lix/translation-form-bundle. It is actively maintained and if you like it - you can stick to it.

But to use a2lix/translation-form-bundle there is some CSS and Javascript required. Also, it comes with a dedicated FormType. So you have to wrap every field inside this FormType. It provides these clickable tabs for every single translated field, which is not so handy, when you just want to translate your whole form.

braunstetter/translated-forms will just work fine with all your Symfony forms. No extra CSS no Javascript no extra FormType to implement. Your forms stay the same. Just switch the languages and even nested forms are translated.

Custom Traits

Knp Doctrine Behaviours brings some Traits to your translatable Entities and your translations.

By default, it assumes you put the translation Entity into the same folder as the translatable. This gets quite messy, as soon as you translate a lot.

So braunstetter/translated-forms bundle delivers two traits. TranslatableTrait and TranslationTrait.

They work just the same way as the default one's form KNP (in fact they use it under the hood), but they put translations into a Translation folder - and they implement the magic __call method which is required for this bundle to translate your forms.

How does this magic happen?

Reading the data (mapDataToForms)

If enabled this bundle sets a new data mapper to your forms.

The default data mapper of the Symfony form component does not work with magic properties - the data mapper of this bundle does.

Writing the data (mapFormsToData)

When data of a form is going to be saved - this data mapper tries to save it the normal way.

If the field of the base entity is not writeable directly it is going to pick the current translation and is writing it into it.