Skip to content
Closed
Changes from 1 commit
Commits
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
Next Next commit
Add CookBook documentation about Field Label Translations
  • Loading branch information
smoya committed Nov 19, 2012
commit 7cf9bf346ffc65efa5bf878bdeb5dbdccf3d68e2
110 changes: 110 additions & 0 deletions cookbook/form/form_label_translation_easily.rst
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
===================================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add one =


Translate the field labels in each form can be a tedious task,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Translating

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should remove the space between ...Extension and </...

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
Copy link
Member

Choose a reason for hiding this comment

The 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 use statements with the classnames here.

And you should align the values:

* @param FormView $view * @param FormInterface $form * @param array $options 
* @param array $options
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smoya it should be:

* @param FormView $view * @param FormInterface $form * @param array $options 

how it is now:

* @param FormView $view * @param FormInterface $form * @param array $options 
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
$view->vars['label'] = str_replace('_', '.', $view->vars['id']);
Copy link
Member

Choose a reason for hiding this comment

The 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.
Thus, you should only do it when the label option is null, to avoid replacing a label configured explicitly

}

/**
* 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">
Copy link
Member

Choose a reason for hiding this comment

The 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:

<service id="..." class="..." > 
<tag name="form.type_extension" alias="form" />
</service>

.. code-block:: php

$container
->register('acme_demo_bundle.label_translation_extension', 'Acme\DemoBundle\Form\Type\LabelTranslationExtension')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here:

 ->register( '...', '...' ) 
->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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the french word for order is Order... (same in the other three examples)


.. note::

As we see in the code, the most comfortable and clean code is YAML version.
Copy link
Member

Choose a reason for hiding this comment

The 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 book/translation article, it is irrelevant here.