Skip to content

Commit

Permalink
* Add a type and a context to the LabelStrategyInterface
Browse files Browse the repository at this point in the history
* Convert the breadcrumb to use the LabelStrategyInterface
* Add a BC service so old translation are not broken, to use it add ``label_translator_strategy="sonata.admin.label.strategy.bc"`` when declaring the tag ``name="sonata.admin"``
  • Loading branch information
Thomas Rabaix committed Nov 15, 2011
1 parent 1997ffa commit b69b8fb
Show file tree
Hide file tree
Showing 34 changed files with 207 additions and 88 deletions.
12 changes: 6 additions & 6 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function configure()
$this->uniqid = uniqid();

if (!$this->classnameLabel) {
$this->classnameLabel = $this->urlize(substr($this->getClass(), strrpos($this->getClass(), '\\') + 1), '_');
$this->classnameLabel = substr($this->getClass(), strrpos($this->getClass(), '\\') + 1);
}

$this->baseCodeRoute = $this->getCode();
Expand Down Expand Up @@ -1710,12 +1710,12 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
}

$child = $menu->addChild(
$this->trans('breadcrumb.dashboard', array(), 'SonataAdminBundle'),
$this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
array('uri' => $this->router->generate('sonata_admin_dashboard'))
);

$child = $child->addChild(
$this->trans(sprintf('breadcrumb.link_%s_list', $this->getClassnameLabel())),
$this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
array('uri' => $this->generateUrl('list'))
);

Expand All @@ -1734,7 +1734,7 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
} elseif ($this->isChild()) {
if ($action != 'list') {
$menu = $menu->addChild(
$this->trans(sprintf('breadcrumb.link_%s_list', $this->getClassnameLabel())),
$this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
array('uri' => $this->generateUrl('list'))
);
}
Expand All @@ -1743,13 +1743,13 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
$breadcrumbs = $menu->getBreadcrumbsArray( (string) $this->getSubject());
} else {
$breadcrumbs = $menu->getBreadcrumbsArray(
$this->trans(sprintf('breadcrumb.link_%s_%s', $this->getClassnameLabel(), $action))
$this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
);
}

} else if ($action != 'list') {
$breadcrumbs = $child->getBreadcrumbsArray(
$this->trans(sprintf('breadcrumb.link_%s_%s', $this->getClassnameLabel(), $action))
$this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
);
} else {
$breadcrumbs = $child->getBreadcrumbsArray();
Expand Down
2 changes: 1 addition & 1 deletion Datagrid/ListMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function add($name, $type = null, array $fieldDescriptionOptions = array(
}

if (!$fieldDescription->getLabel()) {
$fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName()));
$fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
}

// add the field with the FormBuilder
Expand Down
35 changes: 15 additions & 20 deletions DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$groups = $groupDefaults = $admins = $classes = array();
$groupDefaults = $admins = $classes = array();

$pool = $container->getDefinition('sonata.admin.pool');

Expand Down Expand Up @@ -130,6 +130,7 @@ public function applyConfigurationFromAttribute(Definition $definition, array $a
'validator',
'security_handler',
'menu_factory',
'route_builder',
'label_translator_strategy',
);

Expand Down Expand Up @@ -163,17 +164,19 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
$addServices = isset($settings[$serviceId]) ? $settings[$serviceId] : array();

$defaultAddServices = array(
'model_manager' => sprintf('sonata.admin.manager.%s', $manager_type),
'form_contractor' => sprintf('sonata.admin.builder.%s_form', $manager_type),
'show_builder' => sprintf('sonata.admin.builder.%s_show', $manager_type),
'list_builder' => sprintf('sonata.admin.builder.%s_list', $manager_type),
'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $manager_type),
'translator' => 'translator',
'configuration_pool' => 'sonata.admin.pool',
'router' => 'router',
'validator' => 'validator',
'security_handler' => 'sonata.admin.security.handler',
'menu_factory' => 'knp_menu.factory',
'model_manager' => sprintf('sonata.admin.manager.%s', $manager_type),
'form_contractor' => sprintf('sonata.admin.builder.%s_form', $manager_type),
'show_builder' => sprintf('sonata.admin.builder.%s_show', $manager_type),
'list_builder' => sprintf('sonata.admin.builder.%s_list', $manager_type),
'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $manager_type),
'translator' => 'translator',
'configuration_pool' => 'sonata.admin.pool',
'router' => 'router',
'validator' => 'validator',
'security_handler' => 'sonata.admin.security.handler',
'menu_factory' => 'knp_menu.factory',
'route_builder' => 'sonata.admin.route.path_info',
'label_translator_strategy' => 'sonata.admin.label.strategy.native'
);

foreach ($defaultAddServices as $attr => $addServiceId) {
Expand All @@ -184,14 +187,6 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
}
}

if (!$definition->hasMethodCall('setRouteBuilder')) {
$definition->addMethodCall('setRouteBuilder', array(new Reference('sonata.admin.route.path_info')));
}

if (!$definition->hasMethodCall('setLabelTranslatorStrategy')) {
$definition->addMethodCall('setLabelTranslatorStrategy', array(new Reference('sonata.admin.label.strategy.form_component')));
}

if (isset($service['label'])) {
$label = $service['label'];
} elseif (isset($attributes['label'])) {
Expand Down
2 changes: 1 addition & 1 deletion Form/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function add($name, $type = null, array $options = array(), array $fieldD
$options = array_merge($options, $this->formContractor->getDefaultOptions($type, $fieldDescription));

if (!isset($options['label'])) {
$options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName());
$options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
}

$this->formBuilder->add($name, $type, $options);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</service>

<!-- Services used to format the label, default is sonata.admin.label.strategy.noop -->
<service id="sonata.admin.label.strategy.bc" class="Sonata\AdminBundle\Translator\BCLabelTranslatorStrategy" />
<service id="sonata.admin.label.strategy.native" class="Sonata\AdminBundle\Translator\NativeLabelTranslatorStrategy" />
<service id="sonata.admin.label.strategy.noop" class="Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy" />
<service id="sonata.admin.label.strategy.underscore" class="Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy" />
Expand Down
93 changes: 75 additions & 18 deletions Resources/doc/reference/advance.rst
Original file line number Diff line number Diff line change
@@ -1,32 +1,89 @@
Advance
=======

By default services who are injected to an admin instance are

==================== =============================================
method name Service Id
==================== =============================================
model_manager sonata.admin.manager.%manager-type%
form_contractor sonata.admin.builder.%manager-type%_form
show_builder sonata.admin.builder.%manager-type%_show
list_builder sonata.admin.builder.%manager-type%_list
datagrid_builder sonata.admin.builder.%manager-type%_datagrid
translator translator
configuration_pool sonata.admin.pool
router router
validator validator
security_handler sonata.admin.security.handler
==================== =============================================
Service Configuration
---------------------

When you create a new Admin service you can configure its dependencies, by default services who are injected are:

======================== =============================================
Dependencies Service Id
======================== =============================================
model_manager sonata.admin.manager.%manager-type%
form_contractor sonata.admin.builder.%manager-type%_form
show_builder sonata.admin.builder.%manager-type%_show
list_builder sonata.admin.builder.%manager-type%_list
datagrid_builder sonata.admin.builder.%manager-type%_datagrid
translator translator
configuration_pool sonata.admin.pool
router router
validator validator
security_handler sonata.admin.security.handler
menu_factory knp_menu.factory
router_builder sonata.admin.route.path_info
label_translator_strategy sonata.admin.label.strategy.form_component
========================= =============================================

Note: %manager-type% is replace by the manager type (orm, odm...)

You have 2 ways of defining the dependencies inside a ``services.xml``.

* With a tag attribute, less verbose::

.. code-block:: xml
<service id="acme.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
<tag
name="sonata.admin"
manager_type="orm"
group="Project"
label="Project"
label_translator_strategy="sonata.admin.label.strategy.native"
router_builder="sonata.admin.route.path_info"
/>
<argument />
<argument>AcmeBundle\ProjectBundle\Entity\Project</argument>
<argument />
</service>
* With a method call, more verbose

.. code-block:: xml
<service id="acme.project.admin.project" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
<tag
name="sonata.admin"
manager_type="orm"
group="Project"
label="Project"
/>
<argument />
<argument>AcmeBundle\ProjectBundle\Entity\Project</argument>
<argument />
<call method="setLabelTranslatorStrategy">
<argument type="service" id="sonata.admin.label.strategy.native" />
</call>
<call method="setRouterBuilder">
<argument type="service" id="sonata.admin.route.path_info" />
</call>
</service>
If you want to modify the service who are going to be injected, add the following code to your
application's config file:

.. code-block:: yaml
# app/config/config.yml
admins:
sonata_admin: #method name, you can find the list in the table above
sonata.order.admin.order: #id of the admin service's
sonata_admin: #method name, you can find the list in the table above
sonata.order.admin.order: #id of the admin service's
model_manager: sonata.order.admin.order.manager #id of the your service
Admin Extension
---------------

S
16 changes: 13 additions & 3 deletions Resources/doc/reference/translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ By default, the label is the the field name. However a label can be defined as a
There is another option for rapid prototyping or to avoid spending too much time adding the ``label`` key to all option
fields: ``Label Strategies``. By default labels are generated by using by using a simple rule ::

isValid => Isvalid
isValid => Is Valid

.. note::

For early adopter, you can use a specific backward compatible service to keep your current translation.

This is not perfect and hard to read. So in order to solve this, the ``AdminBundle`` comes with different key label generation
strategies:

* ``sonata.admin.label.strategy.native`` : DEFAULT - Make the string human readable readable - ``isValid`` => ``Is Valid``
* ``sonata.admin.label.strategy.form_component`` : The default behavior from the Form Component - ``isValid`` => ``Isvalid``)
* ``sonata.admin.label.strategy.underscore`` : Add undescore to the label - ``isValid`` => ``label_is_valid``
* ``sonata.admin.label.strategy.native`` : Make the string human readable readable - ``isValid`` => ``Is Valid``
* ``sonata.admin.label.strategy.underscore`` : Add undescore to the label - ``isValid`` => ``form.label_is_valid``
* ``sonata.admin.label.strategy.noop`` : does not alter the string - ``isValid`` => ``isValid``
* ``sonata.admin.label.strategy.bc`` : preserve the old label generation from the early version of ``SonataAdminBundle``

``sonata.admin.label.strategy.underscore`` will be better for i18n applications and ``sonata.admin.label.strategy.native`
will be better for native language based on the field name. So it is possible to start with the ``native`` strategy and then
Expand All @@ -105,3 +110,8 @@ The strategy can be quickly configured when the Admin class is registered into t

In all cases the label will be used by the ``Translator``. The strategy is just a quick way to generate translable keys
depends on the project's requirements.


.. note::

When the strategy method is called, a context (form, filter, list, show) and a type (link, label, etc ...) arguments are passed.
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.ca.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>S'ha eliminat correctament l'element.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.de.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>Element wurde erfolgreich gelöscht.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>Item has been deleted successfully.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<target>expandir/colapsar</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="label_filters">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>Les éléments ont été supprimés avec succès.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.hr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>Element je uspješno izbrisan.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.it.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<target>Elemento eliminato con successo.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
</body>
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.ja.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<target>表示</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
</body>
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.lb.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>D'Element gouf erfollegräich geläscht.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.nl.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<target>Bekijk</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
</body>
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.pl.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>Element został pomyślnie usunięty.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.pt_BR.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>O item foi apagado com sucesso.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>&#8962;</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/SonataAdminBundle.pt_PT.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<target>O item foi apagado com sucesso.</target>
</trans-unit>
<trans-unit id="link_breadcrumb_dashboard">
<source>breadcrumb.dashboard</source>
<source>breadcrumb.link_dashboard</source>
<target>Visão Geral</target>
</trans-unit>
<trans-unit id="title_delete">
Expand Down
Loading

0 comments on commit b69b8fb

Please sign in to comment.