translation.rst 4.8 KB

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