Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Add translation_parameters option to base form type #27775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

[Form] Add translation_parameters option to base form type #27775

wants to merge 6 commits into from

Conversation

webnet-fr
Copy link
Contributor

@webnet-fr webnet-fr commented Jun 29, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes. Travis-ci isn't green because it tests the components separately. Fabbot.io requires license headers in files where they were not present before.
Fixed tickets #27698
License MIT
Doc PR symfony/symfony-docs/pull/10065

In forms it would be very nice if there is a way to provide translation parameters for label and help optons. Consider this example :

#messages.en.yml
form:
  order_list:
    id: Identifier of the order to %company%
    first: First position is to send to %address%
    second: Second position is to send to %address%
    second_help: This must be %item%
#messages.fr.yml
form:
  order_list:
    id: Identifiant de la commande pour la société %company%
    first: Première position est à envoyer à %address%
    second: Seconde position est à envoyer à %address%
    second_help: Ce doit être %item%
class OrderListType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', TextType::class, [
                'label' => 'form.order_list.id',
                'translation_parameters' => [
                    '%company%' => 'Webnet'
                ]
            ])
            ->add('list', FormType::class, [
                'translation_parameters' => [
                    '%address%' => 'Sèvres, 92310'
                ]
            ])
        ;

        // Add list's children to demonstrate translation_parameters inheritance.
        $builder->get('list')
            ->add('first', TextType::class, [
                'label' => 'form.order_list.first',
                'translation_parameters' => [
                    // "Paris, 75010" (a value particular to this field) will overwrite "Sèvres, 92310"
                    '%address%' => 'Paris, 75010' 
                ]
            ])
            ->add('second', TextType::class, [
                'label' => 'form.order_list.second',
                'help' => 'form.order_list.second_help',
                'translation_parameters' => [
                    // Inherit %address% from parent => 'Sèvres, 92310',
                    // "item" parameter is used in help message.
                    '%item%' => 'DIN5555'
                ],
            ])
        ;
    }
}

Here is a result with translation parameters :
With parameters

and without them :
With parameters

@webnet-fr webnet-fr changed the title Add translation_parameters option to base form type [Form] Add translation_parameters option to base form type Jun 29, 2018
@nicolas-grekas nicolas-grekas added this to the next milestone Jun 29, 2018
@xabbuh
Copy link
Member

xabbuh commented Jun 30, 2018

Having just one translation_parameters option could lead to hard to solve issues if you want to pass parameters with the same name to the translations for the label and the help string but with actually different values. IMO if we were to add this, we would need two distinct options.

@webnet-fr
Copy link
Contributor Author

@xabbuh that's a good point but I'd rather limit the proliferation of options. There is a very little chance that there would be the same translation parameter in label and in help that would require different values. In such cases I suggest to use slightly different translation parameters to reflect the difference in values (like %company% in label and %company_vat% in help).
Cheers.

@webnet-fr
Copy link
Contributor Author

Translation parameters were injected to labels and helps. Tests were added.

Should we inject translation parameters into buttons, attributes and choice options as well? It would be nice, isn’t it?

As for the comment of @xabbuh I would suggest a single translation_parameters for labels, helps, buttons and attributes because they share the same translation_domain. And we can add choice_translation_ parameters for choices because they have a separate choice_translation_domain.

@xabbuh
Copy link
Member

xabbuh commented Sep 23, 2018

I still think that different parameters for label and help is the way to go. The message id that is passed to the translator is already different (i.e. the label or the help message) and so we should separate the parameters.

Apart from that I like this feature.

@xabbuh
Copy link
Member

xabbuh commented Oct 1, 2018

Thanks for opening #28635. I think we can close here in favour of your other PR.

@xabbuh xabbuh closed this Oct 1, 2018
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
fabpot added a commit that referenced this pull request Feb 13, 2019
…ion_parameters and attr_translation_parameters options to base form type (webnet-fr)

This PR was squashed before being merged into the 4.3-dev branch (closes #28635).

Discussion
----------

[Form] Add label_translation_parameters, help_translation_parameters and attr_translation_parameters options to base form type

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes. Travis-ci isn't green because it tests the components separately. Fabbot.io requires license headers in files where they were not present before.
| Fixed tickets | #27698
| License       | MIT
| Doc PR        | symfony/symfony-docs#10065

Hi, this is an alternative to #27775.

`translation_parameters` is separated to `label_translation_parameters`, `help_translation_parameters`, `attr_translation_parameters`.

Commits
-------

b3f3c53 [Form] Add label_translation_parameters, help_translation_parameters and attr_translation_parameters options to base form type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants