a2lix / TranslationFormBundle

Ease translations with some dedicated Symfony form types
https://a2lix.fr/bundles/translation-form
MIT License
330 stars 138 forks source link

Several TranslationType Rows #346

Closed holema closed 4 years ago

holema commented 4 years ago

Hello, thanks first for this great bundle. I realy like it a lot. In my new project I want to us it as well, but we have a lot of translatables fields. so I want to split them in several Translation Blocks. I don´t want to set several dummie sttributes to generate different translation blocks. So my idea was to generete serveral TranslationType Rows into my form and rename them. f.e.

            ->add('translations', TranslationsType::class, [
                // [1]

                'excluded_fields' => ['yyy']            // [2]
            ])
            ->add('translations1', TranslationsType::class, [
                // [1]

                'excluded_fields' => ['xxx']            // [2]
            ])

But when I do it this way, I get an error that no translation1 property is set in the class. but there is also no translations property in the class set, so this is autogenerated or magic. I use KnpLabs Tranlation.

Is there an easy way to fix it?

Best regards

webda2l commented 4 years ago

Hello,

No easy way on the PHP side no... but an available way on the Twig side: https://github.com/a2lix/TranslationFormBundle/blob/master/src/Resources/views/macros.html.twig#L2-L7

holema commented 4 years ago

hello,

thanks for this great answer. Now everything works. I just needed to make a small improvment, by adding a random number to the tab, so it is possible to render several tabs on one page.

{% macro partialTranslations(form, fieldsNames) %}
    {{ form_errors(form) }}
{% set rand = random(8000) %}
    <div class="a2lix_translations">
        <ul class="a2lix_translationsLocales nav nav-tabs" role="tablist">
            {% for translationsFields in form %}
                {% set locale = translationsFields.vars.name %}
                <li class="nav-item">
                    <a href="#{{ translationsFields.vars.id }}{{ rand }}_a2lix_translations-fields" class="nav-link {% if app.request.locale == locale %}active{% endif %}" data-toggle="tab" role="tab">
                        {{ translationsFields.vars.label|default(locale|humanize)|trans }}
                        {% if form.vars.default_locale == locale %}{{ '[Default]'|trans }}{% endif %}
                        {% if translationsFields.vars.required %}*{% endif %}
                    </a>
                </li>
            {% endfor %}
        </ul>
        <div class="a2lix_translationsFields tab-content ">
            {% for translationsFields in form %}
                {% set locale = translationsFields.vars.name %}

                <div id="{{ translationsFields.vars.id }}{{ rand }}_a2lix_translations-fields" class="tab-pane {% if app.request.locale == locale %}show active{% endif %} {% if not form.vars.valid %}sonata-ba-field-error{% endif %}" role="tabpanel">
                    {% for translationsField in translationsFields|filter(translationsField => translationsField.vars.name in fieldsNames) %}
                        {{ form_row(translationsField) }}
                    {% endfor %}
                </div>
            {% endfor %}
        </div>
    </div>
{% endmacro %}

Propably you want to add the change to the official repo.

best regards