translation.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 admins
  5. * ``messages`` : this catalogue is used to translate the messages 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 gets 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 field name. However a label can be defined as 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 a simple rule ::
  52. isValid => Is Valid
  53. .. note::
  54. For early adopter, you can use a specific backward compatible service to keep your current translation.
  55. The ``AdminBundle`` comes with different key label generation strategies:
  56. * ``sonata.admin.label.strategy.native`` : DEFAULT - Makes the string human readable readable - ``isValid`` => ``Is Valid``
  57. * ``sonata.admin.label.strategy.form_component`` : The default behavior from the Form Component - ``isValid`` => ``Isvalid``)
  58. * ``sonata.admin.label.strategy.underscore`` : Adds undescore to the label - ``isValid`` => ``form.label_is_valid``
  59. * ``sonata.admin.label.strategy.noop`` : does not alter the string - ``isValid`` => ``isValid``
  60. * ``sonata.admin.label.strategy.bc`` : preserves the old label generation from the early version of ``SonataAdminBundle``
  61. ``sonata.admin.label.strategy.underscore`` will be better for i18n applications and ``sonata.admin.label.strategy.native`
  62. will be better for native language based on the field name. So it is possible to start with the ``native`` strategy and then
  63. when the application needs to be translated using generic keys the configuration can be switched to the ``sonata.admin.label.strategy.underscore``.
  64. The strategy can be quickly configured when the Admin class is registered into the Container:
  65. .. code-block:: xml
  66. <service id="ekino.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  67. <tag
  68. name="sonata.admin"
  69. manager_type="orm"
  70. group="Project"
  71. label="Project"
  72. label_translator_strategy="sonata.admin.label.strategy.native"
  73. />
  74. <argument />
  75. <argument>AcmeBundle\ProjectBundle\Entity\ProjectFeed</argument>
  76. <argument />
  77. </service>
  78. .. note::
  79. In all cases the label will be used by the ``Translator``. The strategy is just a quick way to generate translatable keys.
  80. It all depends on the project's requirements.
  81. .. note::
  82. When the strategy method is called, a context (form, filter, list, show) and a type (link, label, etc ...) arguments are passed.