9
9
use Kreyu \Bundle \DataTableBundle \Column \ColumnInterface ;
10
10
use Kreyu \Bundle \DataTableBundle \Column \ColumnValueView ;
11
11
use Symfony \Component \OptionsResolver \OptionsResolver ;
12
+ use Symfony \Contracts \Translation \TranslatableInterface ;
13
+ use Symfony \Contracts \Translation \TranslatorInterface ;
12
14
13
15
/**
14
16
* Represents a column with value displayed as a list.
17
19
*/
18
20
final class CollectionColumnType extends AbstractColumnType
19
21
{
22
+ public function __construct (
23
+ private ?TranslatorInterface $ translator = null ,
24
+ ) {
25
+ }
26
+
20
27
public function buildColumn (ColumnBuilderInterface $ builder , array $ options ): void
21
28
{
22
29
$ builder ->setAttribute ('prototype_factory ' , $ builder ->getColumnFactory ());
@@ -27,6 +34,7 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
27
34
$ view ->vars = array_replace ($ view ->vars , [
28
35
'separator ' => $ options ['separator ' ],
29
36
'separator_html ' => $ options ['separator_html ' ],
37
+ 'separator_translatable ' => $ options ['separator ' ] instanceof TranslatableInterface,
30
38
'children ' => $ this ->createChildrenColumnValueViews ($ view , $ column , $ options ),
31
39
]);
32
40
}
@@ -44,7 +52,19 @@ public function buildExportValueView(ColumnValueView $view, ColumnInterface $col
44
52
'separator_html ' => $ options ['separator_html ' ],
45
53
];
46
54
47
- $ view ->value = $ view ->vars ['value ' ] = implode ($ options ['export ' ]['separator ' ], array_map (
55
+ $ separator = $ options ['export ' ]['separator ' ] ?? '' ;
56
+
57
+ if ($ this ->translator && $ separator instanceof TranslatableInterface) {
58
+ $ locale = null ;
59
+
60
+ if (method_exists (TranslatableInterface::class, 'getLocale ' )) {
61
+ $ locale = $ this ->translator ->getLocale ();
62
+ }
63
+
64
+ $ separator = $ separator ->trans ($ this ->translator , $ locale );
65
+ }
66
+
67
+ $ view ->value = $ view ->vars ['value ' ] = implode ($ separator , array_map (
48
68
static fn (ColumnValueView $ view ) => $ view ->vars ['value ' ],
49
69
$ this ->createChildrenColumnValueViews ($ view , $ column , $ options ['export ' ]),
50
70
));
@@ -70,7 +90,7 @@ public function configureOptions(OptionsResolver $resolver): void
70
90
$ resolver ->define ('separator ' )
71
91
->default (', ' )
72
92
->info ('Separator to render between each item in the collection. ' )
73
- ->allowedTypes ('null ' , 'string ' )
93
+ ->allowedTypes ('null ' , 'string ' , TranslatableInterface::class )
74
94
;
75
95
76
96
/* @see https://data-table-bundle.swroblewski.pl/reference/types/column/collection#separator-html */
0 commit comments