translation.rst 6.2 KB

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