-
-
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| .. index:: | ||
| single: Form; Translate field labels | ||
| | ||
| How to Translate field labels easily | ||
| ==================================== | ||
| | ||
| Translating the field labels in each form can be a tedious task, | ||
| especially if you use **"Keyword Messages"**. | ||
| For example, if you had two different forms with a field named "order", | ||
| the default translation keyword would be exactly the same. | ||
| On the other hand, if you need to write different translations | ||
| depending the form, you 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 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 FormView $view | ||
| * @param FormInterface $form | ||
| * @param array $options | ||
| */ | ||
| public function finishView(FormView $view, FormInterface $form, array $options) | ||
| { | ||
| if (null === $options['label']) { | ||
| $view->vars['label'] = str_replace('_', '.', $view->vars['id']); | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * 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"> | ||
| <tag name="form.type_extension" alias="form" /> | ||
| </service> | ||
| | ||
| .. code-block:: php | ||
| | ||
| $container | ||
| ->register( | ||
| 'acme_demo_bundle.label_translation_extension', | ||
| 'Acme\DemoBundle\Form\Type\LabelTranslationExtension' | ||
| ) | ||
| ->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>Ordre</target> | ||
| </trans-unit> | ||
| </body> | ||
| </file> | ||
| </xliff> | ||
| | ||
| .. code-block:: php | ||
| | ||
| // messages.fr.php | ||
| return array( | ||
| 'acme.demobundle.exampletype.order' => 'Ordre', | ||
| ); | ||
| | ||
| .. code-block:: yaml | ||
| | ||
| # messages.fr.yml | ||
| acme: | ||
| demobundle: | ||
| exampletype: | ||
| order: Ordre | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
This is still not aligned correctly.
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.
@smoya it should be:
how it is now: