translation.rst 5.6 KB

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