translation.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 ``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 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. 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. The following example sets the translation domain on a form group:
  54. .. code-block:: php
  55. $formMapper->with('form.my_group', array(
  56. 'translation_domain' => 'MyTranslationDomain'
  57. ))
  58. ->add('publishable', 'checkbox', array(), array(
  59. // ...
  60. 'translation_domain' => 'MyTranslationDomain',
  61. ))
  62. ->add('start_date', 'date', array(), array(
  63. ));
  64. Setting the translation domain on an individual field:
  65. .. code-block:: php
  66. $formMapper->with('form.my_group')
  67. ->add('publishable', 'checkbox', array(), array(
  68. // ...
  69. 'translation_domain' => 'MyTranslationDomain',
  70. ));
  71. Setting the label name
  72. ^^^^^^^^^^^^^^^^^^^^^^
  73. By default, the label is the field name. However a label can be defined as
  74. 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('isValid', null, array('required' => false, 'label' => 'label.is_valid'));
  82. }
  83. }
  84. Label strategies
  85. ^^^^^^^^^^^^^^^^
  86. There is another option for rapid prototyping or to avoid spending too much time
  87. adding the ``label`` key to all option fields: ``Label Strategies``. By default
  88. labels are generated by using a simple rule ::
  89. isValid => Is Valid
  90. .. note::
  91. For early adopter, you can use a specific backward compatible service to
  92. keep your current translation.
  93. The ``AdminBundle`` comes with different key label generation strategies:
  94. * ``sonata.admin.label.strategy.native`` : DEFAULT - Makes the string human
  95. readable readable - ``isValid`` => ``Is Valid``
  96. * ``sonata.admin.label.strategy.form_component`` : The default behavior from the
  97. Form Component - ``isValid`` => ``Isvalid``)
  98. * ``sonata.admin.label.strategy.underscore`` : Adds undescore to the label -
  99. ``isValid`` => ``form.label_is_valid``
  100. * ``sonata.admin.label.strategy.noop`` : does not alter the string - ``isValid``
  101. => ``isValid``
  102. * ``sonata.admin.label.strategy.bc`` : preserves the old label generation from
  103. the early version of ``SonataAdminBundle``
  104. ``sonata.admin.label.strategy.underscore`` will be better for i18n applications
  105. and ``sonata.admin.label.strategy.native` will be better for native language
  106. based on the field name. So it is possible to start with the ``native`` strategy
  107. and then when the application needs to be translated using generic keys the
  108. configuration can be switched to the ``sonata.admin.label.strategy.underscore``.
  109. The strategy can be quickly configured when the Admin class is registered into
  110. the Container:
  111. .. code-block:: xml
  112. <service id="ekino.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  113. <tag
  114. name="sonata.admin"
  115. manager_type="orm"
  116. group="Project"
  117. label="Project"
  118. label_translator_strategy="sonata.admin.label.strategy.native"
  119. />
  120. <argument />
  121. <argument>AcmeBundle\ProjectBundle\Entity\ProjectFeed</argument>
  122. <argument />
  123. </service>
  124. .. note::
  125. In all cases the label will be used by the ``Translator``. The strategy is
  126. just a quick way to generate translatable keys. It all depends on the
  127. project's requirements.
  128. .. note::
  129. When the strategy method is called, a context (form, filter, list, show) and
  130. a type (link, label, etc ...) arguments are passed.