How to translate "(optional)" to a Symfony2 form

I have some types of forms that are not required. Form signs should be localized, and that’s easy.

But when you configure a particular type of form as 'required'=>'false' , the word "(optional)" appears immediately after the type label.

What would be the correct way to translate "optional" or disable it?

Btw. I don’t see any way at all.

thanks

  "require": { "php": ">=5.3.3", "symfony/symfony": "v2.3.0", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.2.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.3.*", "symfony/monolog-bundle": "2.3.*", "sensio/distribution-bundle": "2.3.*", "sensio/framework-extra-bundle": "2.3.*", "sensio/generator-bundle": "2.3.*", "jms/security-extra-bundle": "1.4.*@dev", "jms/di-extra-bundle": "1.3.*@dev", "twitter/bootstrap" : "dev-master", "cg/kint-bundle": "dev-master", "raveren/kint": "dev-master", "mopa/bootstrap-bundle": "dev-master", "sonata-project/intl-bundle": "dev-master", "egeloen/ckeditor-bundle": "2.*" }, 
+4
source share
2 answers

An "optional" string rendering is introduced by mopa / bootstrap-bundle .

It can be found in the package Resources / Views / Form / fields.html.twig .

An “optional” line is added to the form_label_asterisk block:

 {% block label_asterisk %} {% if required %} {% if render_required_asterisk %} <span>*</span> {% endif %} {% else %} {% if render_optional_text %} <span>{{ "(optional)"|trans({}, translation_domain) }}</span> {% endif %} {% endif %} {% endblock label_asterisk %} 

As you can see, rendering requires you to set domain_domain for the optional string to translate. The correct implementation would use return to messages

 ... <span>{{ "(optional)"|trans({}, translation_domain|default('messages')) }}</span> ... 

Decision:

Remove the optional rendering completely by adding to your config.yml:

 # app/config/config.yml parameters: mopa_bootstrap.form.render_optional_text: false 

... or add render_optional_text => false to the form parameters.

As an initial form type, BootstrapBundle can be found here .

Alternatively, you can remove the optional line by overriding the block in one form

 {% form_theme form _self %} {% block label_asterisk %} {% if required %} {% if render_required_asterisk %} <span>*</span> {% endif %} {% endif %} {% endblock label_asterisk %} 

More details on form overrides can be found in my answer here .

+10
source

See Allow translation (optional) # 277 to the package repository. This code has changed a lot since the accepted answer to this question.

Now it uses translation_domain as follows:

 {%- if render_optional_text %}&nbsp;<span>{{ "(optional)"|trans({}, translation_domain) }}</span>{% endif %} 

I personally used the JMSTranslationBundle , so to extract this, I made a quick mute class as follows:

 <?php namespace ACME\Bundle\ACMEBundle\Tools; use Symfony\Bundle\FrameworkBundle\Translation\Translator; class TranlsationsOverrides { /** * @param Translator $translator */ function __construct(Translator $translator) { $this->translator = $translator; /** * @see vendor/mopa/bootstrap-bundle/Mopa/Bundle/BootstrapBundle/Resources/views/Form/fields.html.twig:653 * https://github.com/phiamo/MopaBootstrapBundle/pull/277 */ /** @desc("(optional)") */ $translator->trans('(optional)'); } } 

Therefore, when you run php app/console translation:extract my_local it is extracted and can be translated :)

0
source

Source: https://habr.com/ru/post/1486176/


All Articles