From 373eaa200d3cfbd48554787aecf5b688265362c8 Mon Sep 17 00:00:00 2001
From: Pavol Tuka <30590523+pavol-tk@users.noreply.github.com>
Date: Fri, 15 May 2020 09:45:16 +0200
Subject: [PATCH] Pass menu label attributes to translation
---
docs/reference/advanced_configuration.rst | 10 ++--
.../views/Menu/sonata_menu.html.twig | 52 ++++++++++++-------
2 files changed, 37 insertions(+), 25 deletions(-)
diff --git a/docs/reference/advanced_configuration.rst b/docs/reference/advanced_configuration.rst
index a3a9c80476..18ad601075 100644
--- a/docs/reference/advanced_configuration.rst
+++ b/docs/reference/advanced_configuration.rst
@@ -306,19 +306,19 @@ If you want to use the Tab Menu in a different way, you can replace the Menu Tem
Translations
^^^^^^^^^^^^
-The translation parameters and domain can be customised by using the
-``translation_domain`` and ``translation_parameters`` keys of the extra array
+The label translation parameters and domain can be customised by using the
+``label_translation_parameters`` and ``label_catalogue`` keys of the extra array
of data associated with the item, respectively::
$menuItem->setExtras([
- 'translation_parameters' => ['myparam' => 'myvalue'],
- 'translation_domain' => 'My domain',
+ 'label_translation_parameters' => ['myparam' => 'myvalue'],
+ 'label_catalogue' => 'My domain',
]);
You can also set the translation domain on the menu root, and children will
inherit it::
- $menu->setExtra('translation_domain', 'My domain');
+ $menu->setExtra('label_catalogue', 'My domain');
Filter parameters
^^^^^^^^^^^^^^^^^
diff --git a/src/Resources/views/Menu/sonata_menu.html.twig b/src/Resources/views/Menu/sonata_menu.html.twig
index 46415733a1..7afd1066ef 100644
--- a/src/Resources/views/Menu/sonata_menu.html.twig
+++ b/src/Resources/views/Menu/sonata_menu.html.twig
@@ -1,34 +1,34 @@
{% extends 'knp_menu.html.twig' %}
{% block root %}
- {%- set listAttributes = item.childrenAttributes|merge({'class': 'sidebar-menu', 'data-widget': 'tree'}) %}
- {%- set request = item.extra('request') ?: app.request %}
+ {%- set listAttributes = item.childrenAttributes|merge({'class': 'sidebar-menu', 'data-widget': 'tree'}) -%}
+ {%- set request = item.extra('request') ?: app.request -%}
{{ block('list') -}}
{% endblock %}
{% block item %}
- {%- if item.displayed %}
+ {%- if item.displayed -%}
{#- check role of the group #}
- {%- set display = item.extra('roles') is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or item.extra('roles')|filter(role => is_granted(role))|length > 0 %}
- {%- endif %}
+ {%- set display = item.extra('roles') is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or item.extra('roles')|filter(role => is_granted(role))|length > 0 -%}
+ {%- endif -%}
- {%- if item.displayed and display|default %}
- {% set options = options|merge({branch_class: 'treeview', currentClass: "active", ancestorClass: "active"}) %}
- {%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' active')|trim) %}
- {%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' treeview-menu')|trim) %}
+ {%- if item.displayed and display|default -%}
+ {%- set options = options|merge({branch_class: 'treeview', currentClass: "active", ancestorClass: "active"}) -%}
+ {%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' active')|trim) -%}
+ {%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' treeview-menu')|trim) -%}
{{ parent() }}
- {% endif %}
+ {%- endif -%}
{% endblock %}
{% block linkElement %}
{% apply spaceless %}
- {% set translation_domain = item.extra('label_catalogue', 'messages') %}
- {% if item.extra('on_top') is defined and not item.extra('on_top') %}
- {% set icon = item.extra('icon')|default(item.level > 1 ? '' : '') %}
- {% else %}
- {% set icon = item.extra('icon') %}
- {% endif %}
- {% set is_link = true %}
+ {%- set translation_domain = item.extra('label_catalogue', 'messages') -%}
+ {%- if item.extra('on_top') is defined and not item.extra('on_top') -%}
+ {%- set icon = item.extra('icon')|default(item.level > 1 ? '' : '') -%}
+ {%- else -%}
+ {%- set icon = item.extra('icon') -%}
+ {%- endif -%}
+ {%- set is_link = true -%}
{{ parent() }}
{% endapply %}
{% endblock %}
@@ -36,8 +36,8 @@
{% block spanElement %}
{% apply spaceless %}
- {% set translation_domain = item.extra('label_catalogue') %}
- {% set icon = item.extra('icon')|default('') %}
+ {%- set translation_domain = item.extra('label_catalogue', 'messages') -%}
+ {%- set icon = item.extra('icon')|default('') -%}
{{ icon|raw }}
{{ parent() }}
{%- if item.extra('keep_open') is not defined or not item.extra('keep_open') -%}
@@ -47,4 +47,16 @@
{% endapply %}
{% endblock %}
-{% block label %}{% if is_link is defined and is_link %}{{ icon|default|raw }}{% endif %}{% if options.allow_safe_labels and item.extra('safe_label', false) %}{{ item.label|raw }}{% else %}{{ item.label|trans({}, translation_domain|default('messages')) }}{% endif %}{% endblock %}
+{% block label %}
+ {% apply spaceless %}
+ {%- if is_link|default(false) -%}
+ {{ icon|default|raw }}
+ {%- endif -%}
+ {%- if options.allow_safe_labels and item.extra('safe_label', false) -%}
+ {{ item.label|raw }}
+ {%- else -%}
+ {%- set translation_domain = item.extra('label_catalogue', 'messages') -%}
+ {{ item.label|trans(item.extra('label_translation_parameters', {}), translation_domain) }}
+ {%- endif -%}
+ {% endapply %}
+{% endblock %}