Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb76271

Browse files
authored
[Form] Support Translatable Enum
1 parent571c7d7 commitbb76271

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

‎reference/forms/types/enum.rst‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,31 @@ these values as ``<input type="checkbox">`` or ``<input type="radio">``.
5151

5252
The label displayed in the ``<option>`` elements of the ``<select>`` is the enum
5353
name. PHP defines some strict rules for these names (e.g. they can't contain
54-
dots or spaces). If you need more flexibility for these labels,use the
55-
``choice_label``option and define a function that returns thecustomlabel::
54+
dots or spaces). If you need more flexibility for these labels,your enum can
55+
implements ``TranslatableInterface``to translate or displaycustomlabels::
5656

57-
->add('textAlign', EnumType::class, [
58-
'class' => TextAlign::class,
59-
'choice_label' => fn ($choice) => match ($choice) {
60-
TextAlign::Left => 'text_align.left.label',
61-
TextAlign::Center => 'text_align.center.label',
62-
TextAlign::Right => 'text_align.right.label',
63-
},
64-
]);
57+
// src/Config/TextAlign.php
58+
namespace App\Config;
59+
60+
enum TextAlign: string implements TranslatableInterface
61+
{
62+
case Left = 'Left aligned';
63+
case Center = 'Center aligned';
64+
case Right = 'Right aligned';
65+
66+
public function trans(TranslatorInterface $translator, string $locale = null): string
67+
{
68+
// Translate enum from name (Left, Center or Right)
69+
return $translator->trans($this->name, locale: $locale);
70+
71+
// Translate enum using custom labels
72+
return match ($this) {
73+
self::Left => $translator->trans('text_align.left.label', locale: $locale),
74+
self::Center => $translator->trans('text_align.center.label', locale: $locale),
75+
self::Right => $translator->trans('text_align.right.label', locale: $locale),
76+
};
77+
}
78+
}
6579

6680
Field Options
6781
-------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp