Skip to content

Commit 18baddf

Browse files
authored
[BootstrapAdminUi] Use translation domain on menu (#310)
Fixes #309
2 parents 7839d32 + 3583c2c commit 18baddf

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

src/BootstrapAdminUi/templates/shared/crud/common/sidebar/menu/menu.html.twig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends 'knp_menu.html.twig' %}
1+
{% extends '@KnpMenu/menu.html.twig' %}
22

33
{% block root %}
44
<ul class="sidebar navbar-nav">
@@ -44,13 +44,15 @@
4444
{%- if classes is not empty %}
4545
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
4646
{%- endif %}
47+
4748
{% if item.hasChildren %}
4849
<li class="nav-item {% if attributes.class is defined %}{{ attributes.class }}{% endif %}">
4950
<a class="nav-link dropdown-toggle {{ is_active ? 'show' : '' }}" href="#navbar-{{ item.name }}" data-bs-toggle="dropdown" data-bs-auto-close="false" role="button" aria-expanded="{{ is_active ? 'true' : 'false' }}">
5051
<span class="nav-link-icon d-md-none d-lg-inline-block">
5152
{{ block('icon') }}
5253
</span>
53-
<span class="nav-link-title">{{ item.label|trans }}</span>
54+
55+
<span class="nav-link-title">{{ block('label') }}</span>
5456
</a>
5557
<div class="dropdown-menu {{ is_active ? 'show' : '' }}">
5658
<div class="dropdown-menu-columns">
@@ -67,7 +69,7 @@
6769
{{ block('icon') }}
6870
</span>
6971
<span class="nav-link-title">
70-
{{ item.label|trans }} {% if target == '_blank' %}{{ ux_icon('tabler:external-link', {'class': 'icon icon-sm ms-1 mb-2 opacity-75'}) }}{% endif %}
72+
{{ block('label') }} {% if target == '_blank' %}{{ ux_icon('tabler:external-link', {'class': 'icon icon-sm ms-1 mb-2 opacity-75'}) }}{% endif %}
7173
</span>
7274
</a>
7375
</li>
@@ -92,7 +94,7 @@
9294
{%- endif %}
9395

9496
<a class="dropdown-item {% if attributes.class is defined %}{{ attributes.class }}{% endif %}" href="{{ item.uri }}" target="{{ target }}">
95-
{{ item.label|trans }}{% if target == '_blank' %}{{ ux_icon('tabler:external-link', {'class': 'icon icon-sm ms-1 mb-2 opacity-75'}) }}{% endif %}
97+
{{ block('label') }}{% if target == '_blank' %}{{ ux_icon('tabler:external-link', {'class': 'icon icon-sm ms-1 mb-2 opacity-75'}) }}{% endif %}
9698
</a>
9799
{% endblock %}
98100

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace TestApplication\Sylius\BootstrapAdminUi\Menu;
15+
16+
use Knp\Menu\ItemInterface;
17+
use Sylius\AdminUi\Knp\Menu\MenuBuilderInterface;
18+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
19+
20+
#[AsDecorator(decorates: 'sylius_admin_ui.knp.menu_builder')]
21+
final class AdminMenuBuilder implements MenuBuilderInterface
22+
{
23+
public function __construct(private MenuBuilderInterface $menuBuilder)
24+
{
25+
}
26+
27+
public function createMenu(array $options): ItemInterface
28+
{
29+
$menu = $this->menuBuilder->createMenu($options);
30+
31+
$menu
32+
->addChild('dashboard', [
33+
'route' => 'sylius_admin_ui_dashboard',
34+
])
35+
->setLabel('app.ui.dashboard')
36+
->setLabelAttribute('icon', 'tabler:dashboard')
37+
;
38+
39+
$this->addLibrarySubMenu($menu);
40+
41+
return $menu;
42+
}
43+
44+
private function addLibrarySubMenu(ItemInterface $menu): void
45+
{
46+
$library = $menu
47+
->addChild('library')
48+
->setLabel('app.menu.library')
49+
->setLabelAttribute('icon', 'tabler:books')
50+
->setExtra('translation_domain', 'menu')
51+
;
52+
53+
$library->addChild('books', ['route' => 'app_book_index'])
54+
->setLabel('app.ui.books')
55+
->setLabelAttribute('icon', 'book')
56+
;
57+
}
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app:
2+
menu:
3+
library: Library
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app:
2+
ui:
3+
dashboard: Dashboard

src/BootstrapAdminUi/tests/Functional/TemplatesTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ public function testIndexTemplate(): void
2929
{
3030
$this->client->request('GET', '/books');
3131

32-
self::assertResponseIsSuccessful();
33-
self::assertSelectorTextContains('title', 'app.ui.books | Sylius');
34-
self::assertSelectorTextContains('tr.item:first-child[data-test-resource-id]', 'The Shining');
35-
self::assertSelectorTextContains('tr.item:last-child[data-test-resource-id]', 'Carrie');
32+
$this->assertResponseIsSuccessful();
33+
$this->assertSelectorTextContains('title', 'app.ui.books | Sylius');
34+
$this->assertSelectorTextContains('tr.item:first-child[data-test-resource-id]', 'The Shining');
35+
$this->assertSelectorTextContains('tr.item:last-child[data-test-resource-id]', 'Carrie');
36+
37+
// Test the translation domain on menu
38+
$this->assertAnySelectorTextSame('.nav-link-title', 'Dashboard'); // with "messages" domain
39+
$this->assertAnySelectorTextSame('.nav-link-title', 'Library'); // with "menu" domain
3640
}
3741
}

0 commit comments

Comments
 (0)