translation.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Translation
  2. ===========
  3. There are two main catalogue names in an Admin class:
  4. * ``SonataAdminBundle`` : this catalogue is used to translate shared messages accross different admin
  5. * ``messages`` : this catalogue is used to translate the message for the current admin
  6. Ideally the ``messages`` catalogue should be changed to avoid any issues with other Admin classes.
  7. You have two options to configure the catalogue for the admin class:
  8. * one by defining a property
  9. .. code-block:: php
  10. <?php
  11. class PageAdmin extends Admin
  12. {
  13. protected $translationDomain = 'SonataPageBundle';
  14. }
  15. * or by injecting the value through the container
  16. .. code-block:: xml
  17. <service id="sonata.page.admin.page" class="Sonata\PageBundle\Admin\PageAdmin">
  18. <tag name="sonata.admin" manager_type="orm" group="sonata_page" label="page"/>
  19. <argument />
  20. <argument>Application\Sonata\PageBundle\Entity\Page</argument>
  21. <argument />
  22. <call method="setTranslationDomain">
  23. <argument>SonataPageBundle</argument>
  24. </call>
  25. </service>
  26. An admin instance always get the ``translator`` instance, so it can be used to translate messages within the
  27. ``configure*Fields`` method or in templates.
  28. .. code-block:: jinja
  29. {# the classical call by using the twig trans helper #}
  30. {% trans from 'SonataPageBundle'%}message_create_snapshots{% endtrans %}
  31. {# by using the admin trans method with hardcoded catalogue #}
  32. {{ admin.trans('message_create_snapshots', {}, 'SonataPageBundle') }}
  33. {# by using the admin trans with the configured catalogue #}
  34. {{ admin.trans('message_create_snapshots') }}
  35. The later solution is more flexible as no catalogue parameters are hardcoded.
  36. Translate field labels
  37. ----------------------
  38. The Admin bundle comes with a customized form field template. The most notable changes from the original one is the use
  39. of the translation domain provided by the Admin instance to translate label.
  40. By default, the label is the the field name. However a label can be defined as a the third argument of the ``add`` method:
  41. .. code-block:: php
  42. <?php
  43. class PageAdmin extends Admin
  44. {
  45. public function configureFormFields(FormMapper $formMapper)
  46. {
  47. $formMapper->add('isValid', null, array('required' => false, 'label' => 'label.is_valid'));
  48. }
  49. }
  50. There is another option for rapid prototyping or to avoid spending too much time adding the ``label`` key to all option
  51. fields: ``Label Strategies``. By default labels are generated by using by using a simple rule ::
  52. isValid => Isvalid
  53. This is not perfect and hard to read. So in order to solve this, the ``AdminBundle`` comes with different key label generation
  54. strategies:
  55. * ``sonata.admin.label.strategy.form_component`` : The default behavior from the Form Component - ``isValid`` => ``Isvalid``)
  56. * ``sonata.admin.label.strategy.underscore`` : Add undescore to the label - ``isValid`` => ``label_is_valid``
  57. * ``sonata.admin.label.strategy.native`` : Make the string human readable readable - ``isValid`` => ``Is Valid``
  58. * ``sonata.admin.label.strategy.noop`` : does not alter the string - ``isValid`` => ``isValid``
  59. ``sonata.admin.label.strategy.underscore`` will be better for i18n applications and ``sonata.admin.label.strategy.native`
  60. will be better for native language based on the field name. So it is possible to start with the ``native`` strategy and then
  61. when the application need to be translated using generic keys the configuration can be switched to used the ``sonata.admin.label.strategy.underscore``.
  62. The strategy can be quickly configured when the Admin class is registered into the Container:
  63. .. code-block:: xml
  64. <service id="ekino.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  65. <tag name="sonata.admin" manager_type="orm" group="Project" label="Project" label_translator_strategy="sonata.admin.label.strategy.native" />
  66. <argument />
  67. <argument>AcmeBundle\ProjectBundle\Entity\ProjectFeed</argument>
  68. <argument />
  69. </service>
  70. .. note::
  71. In all cases the label will be used by the ``Translator``. The strategy is just a quick way to generate translable keys
  72. depends on the project's requirements.