-
-
Couldn't load subscription status.
- Fork 5.3k
Add CookBook documentation about Field Label Translations #1938
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| .. index:: | ||
| single: Form; Translate field labels | ||
| | ||
| How to Translate field labels easily | ||
| =================================== | ||
| | ||
| Translate the field labels in each form can be a tedious task, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| especially if we use **"Keyword Messages"**. | ||
| For example, if we had two different forms with a field named "order", | ||
| the default translation keyword would be exactly the same. | ||
| On the other hand, if We need to write different translations depending the form, | ||
| we need to change the default label in one of them. | ||
| | ||
| This is why you can use a simple solution: | ||
| :doc:`Create a Form Type Extension </cookbook/form/create_form_type_extension>` that allows you | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should remove the space between | ||
| to manipulate the labels and automatically create unique keys:: | ||
| | ||
| // src/Acme/DemoBundle/Form/Extension/LabelTranslationExtension.php | ||
| namespace Acme\DemoBundle\Form\Extension; | ||
| | ||
| use Symfony\Component\Form\AbstractTypeExtension; | ||
| use Symfony\Component\Form\FormInterface; | ||
| use Symfony\Component\Form\FormView; | ||
| | ||
| class LabelTranslationExtension extends AbstractTypeExtension | ||
| { | ||
| /** | ||
| * Manipulates the label. | ||
| * | ||
| * @param \Symfony\Component\Form\FormView $view | ||
| * @param \Symfony\Component\Form\FormInterface $form | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should only use the class name and not the qualified namespace. Every good PHP documentor (i.e. phpdocumentor2 and sami) will match the And you should align the values: | ||
| * @param array $options | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still not aligned correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smoya it should be: how it is now: | ||
| */ | ||
| public function finishView(FormView $view, FormInterface $form, array $options) | ||
| { | ||
| $view->vars['label'] = str_replace('_', '.', $view->vars['id']); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bad idea IMO. The same form type will generate different translation keys depending of the place where you use it, making it difficult to reuse form types. | ||
| } | ||
| | ||
| /** | ||
| * Returns the name of the type being extended. | ||
| * | ||
| * @return string The name of the type being extended | ||
| */ | ||
| public function getExtendedType() | ||
| { | ||
| return 'form'; | ||
| } | ||
| } | ||
| | ||
| Now, declare it as a **service**: | ||
| | ||
| .. configuration-block:: | ||
| | ||
| .. code-block:: yaml | ||
| | ||
| services: | ||
| acme_demo_bundle.label_translation_extension: | ||
| class: Acme\DemoBundle\Form\Type\LabelTranslationExtension | ||
| tags: | ||
| - { name: form.type_extension, alias: form } | ||
| | ||
| .. code-block:: xml | ||
| | ||
| <service id="acme_demo_bundle.label_translation_extension" class="Acme\DemoBundle\Form\Type\LabelTranslationExtension"> | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer, it is not the standard yet, to break this into multiple lines to avoid horizontal scrolling: | ||
| <tag name="form.type_extension" alias="form" /> | ||
| </service> | ||
| | ||
| .. code-block:: php | ||
| | ||
| $container | ||
| ->register('acme_demo_bundle.label_translation_extension', 'Acme\DemoBundle\Form\Type\LabelTranslationExtension') | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here: | ||
| ->addTag('form.type_extension', array('alias' => 'form')); | ||
| | ||
| | ||
| .. configuration-block:: | ||
| | ||
| .. code-block:: xml | ||
| | ||
| <!-- messages.fr.xliff --> | ||
| <?xml version="1.0"?> | ||
| <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
| <file source-language="en" datatype="plaintext" original="file.ext"> | ||
| <body> | ||
| <trans-unit id="1"> | ||
| <source>acme.demobundle.exampletype.order</source> | ||
| <target>Order</target> | ||
| </trans-unit> | ||
| </body> | ||
| </file> | ||
| </xliff> | ||
| | ||
| .. code-block:: php | ||
| | ||
| // messages.fr.php | ||
| return array( | ||
| 'acme.demobundle.exampletype.order' => 'Order', | ||
| ); | ||
| | ||
| .. code-block:: yaml | ||
| | ||
| # messages.fr.yml | ||
| acme: | ||
| demobundle: | ||
| exampletype: | ||
| order: Order | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the french word for | ||
| | ||
| .. note:: | ||
| | ||
| As we see in the code, the most comfortable and clean code is YAML version. | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a usefull note, people can choice themselves which syntax they like and if you want to add this note somewhere you should add in in the | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add one
=