translation.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. across 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. * by over-riding the ``$translationDomain`` property
  12. .. code-block:: php
  13. <?php
  14. class PageAdmin extends Admin
  15. {
  16. protected $translationDomain = 'SonataPageBundle'; // default is 'messages'
  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 ``configureFields`` method or in templates.
  31. .. code-block:: jinja
  32. {# the classical call by using the twig trans helper #}
  33. {{ 'message_create_snapshots'|trans({}, 'SonataPageBundle') }}
  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 last solution is most flexible, as no catalogue parameters are hardcoded, and is the recommended one to use.
  39. Translate field labels
  40. ----------------------
  41. The Admin bundle comes with a customized form field template. The most notable
  42. change from the original one is the use of the translation domain provided by
  43. either the Admin instance or the field description to translate labels.
  44. Overriding the translation domain
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. The translation domain (message catalog) can be overridden at either the form
  47. group or individual field level.
  48. If a translation domain is set at the group level it will cascade down to all
  49. fields within the group.
  50. Overriding the translation domain is of particular use when using
  51. :doc:`extensions <extensions>`, where the extension and the translations would
  52. be defined in one bundle, but implemented in many different Admin instances.
  53. Setting the translation domain on an individual field:
  54. .. code-block:: php
  55. $formMapper->with('form.my_group')
  56. ->add('publishable', 'checkbox', array(), array(
  57. 'translation_domain' => 'MyTranslationDomain',
  58. ))
  59. ;
  60. The following example sets the default translation domain on a form group and
  61. over-rides that setting for one of the fields:
  62. .. code-block:: php
  63. $formMapper
  64. ->with('form.my_group', array('translation_domain' => 'MyDomain'))
  65. ->add('publishable', 'checkbox', array(), array(
  66. 'translation_domain' => 'AnotherDomain',
  67. ))
  68. ->add('start_date', 'date', array(), array())
  69. ;
  70. Setting the label name
  71. ^^^^^^^^^^^^^^^^^^^^^^
  72. By default, the label is set to a sanitized version of the field name. A custom
  73. label can be defined as the third argument of the ``add`` method:
  74. .. code-block:: php
  75. <?php
  76. class PageAdmin extends Admin
  77. {
  78. public function configureFormFields(FormMapper $formMapper)
  79. {
  80. $formMapper->add(
  81. 'isValid',
  82. null,
  83. array('required' => false, 'label' => 'label.is_valid')
  84. );
  85. }
  86. }
  87. Label strategies
  88. ^^^^^^^^^^^^^^^^
  89. There is another option for rapid prototyping or to avoid spending too much time
  90. adding the ``label`` key to all option fields: **Label Strategies**. By default
  91. labels are generated by using a simple rule:
  92. isValid => Is Valid
  93. The ``AdminBundle`` comes with different key label generation strategies:
  94. * ``sonata.admin.label.strategy.native``: DEFAULT - Makes the string human
  95. readable
  96. ``isValid`` => ``Is Valid``
  97. * ``sonata.admin.label.strategy.form_component``: The default behavior from the Form
  98. Component
  99. ``isValid`` => ``Isvalid``
  100. * ``sonata.admin.label.strategy.underscore``: Changes the name into a token suitable for
  101. translation by prepending "form.label" to an underscored version of the field name
  102. ``isValid`` => ``form.label_is_valid``
  103. * ``sonata.admin.label.strategy.noop``: does not alter the
  104. string
  105. ``isValid`` => ``isValid``
  106. * ``sonata.admin.label.strategy.bc``: preserves the old label generation from the
  107. early version of ``SonataAdminBundle``
  108. ``sonata.admin.label.strategy.underscore`` will be better for i18n applications
  109. and ``sonata.admin.label.strategy.native`` will be better for native (single) language
  110. apps based on the field name. It is reasonable to start with the ``native`` strategy
  111. and then, when the application needs to be translated using generic keys, the
  112. configuration can be switched to ``underscore``.
  113. The strategy can be quickly configured when the Admin class is registered in
  114. the Container:
  115. .. code-block:: xml
  116. <service id="ekino.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  117. <tag
  118. name="sonata.admin"
  119. manager_type="orm"
  120. group="Project"
  121. label="Project"
  122. label_translator_strategy="sonata.admin.label.strategy.native"
  123. />
  124. <argument />
  125. <argument>AcmeBundle\ProjectBundle\Entity\ProjectFeed</argument>
  126. <argument />
  127. </service>
  128. .. note::
  129. In all cases the label will be used by the ``Translator``. The strategy is
  130. just a quick way to generate translatable keys. It all depends on the
  131. project's requirements.
  132. .. note::
  133. When the strategy method is called, a context (form, filter, list, show) and
  134. a type (link, label, etc ...) arguments are passed.