diff --git a/composer.json b/composer.json index a9a2e2e..208c158 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "symfony/security-csrf": "^5.4|^6.0", "symfony/service-contracts": "^1.1.6|^2|^3", "symfony/translation": "^5.4|^6.0", + "symfony/translation-contracts": "^2.3|^3.0", "symfony/twig-bridge": "^5.4|^6.0", "symfony/twig-bundle": "^5.4|^6.0", "symfony/validator": "^5.4|^6.0", diff --git a/src/Crud/CrudColumn.php b/src/Crud/CrudColumn.php index d836345..7b7729a 100644 --- a/src/Crud/CrudColumn.php +++ b/src/Crud/CrudColumn.php @@ -17,6 +17,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Validation; +use Symfony\Contracts\Translation\TranslatableInterface; final class CrudColumn { @@ -53,7 +54,7 @@ public function __construct(array $options) 'alias', ]); $resolver->setDefaults([ - 'label' => fn (Options $options): string => $options['id'], + 'label' => fn (Options $options): string|TranslatableInterface => $options['id'], 'sortable' => true, 'displayed_by_default' => true, 'alias_sort' => fn (Options $options): string => $options['alias'], @@ -64,7 +65,7 @@ public function __construct(array $options) new Length(max: 100, maxMessage: 'The column id "{{ value }}" is too long. It should have {{ limit }} character or less') )); $resolver->setAllowedTypes('alias', 'string'); - $resolver->setAllowedTypes('label', 'string'); + $resolver->setAllowedTypes('label', ['string', TranslatableInterface::class]); $resolver->setAllowedTypes('sortable', 'bool'); $resolver->setAllowedTypes('displayed_by_default', 'bool'); $resolver->setAllowedTypes('alias_sort', ['string', 'array']); @@ -83,7 +84,7 @@ public function getAlias(): string return $this->options['alias']; } - public function getLabel(): string + public function getLabel(): string|TranslatableInterface { return $this->options['label']; } diff --git a/src/Form/Type/DisplaySettingsType.php b/src/Form/Type/DisplaySettingsType.php index 6dff738..6af7b75 100644 --- a/src/Form/Type/DisplaySettingsType.php +++ b/src/Form/Type/DisplaySettingsType.php @@ -21,6 +21,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\Count; use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Contracts\Translation\TranslatableInterface; class DisplaySettingsType extends AbstractType { @@ -34,8 +35,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'constraints' => new NotBlank(), ]); + $columnsChoices = $options['columns_choices']; $builder->add('displayedColumns', ChoiceType::class, [ - 'choices' => array_flip($options['columns_choices']), + 'choices' => array_keys($columnsChoices), + 'choice_label' => function (string $choice, string $key, mixed $value) use ($columnsChoices): string|TranslatableInterface { + if (\array_key_exists($choice, $columnsChoices)) { + return $columnsChoices[$choice]; + } + + return $choice; + }, 'multiple' => true, 'expanded' => true, 'label' => 'display_settings.displayed_columns', diff --git a/src/Twig/CrudExtension.php b/src/Twig/CrudExtension.php index 4b563cd..abaabad 100644 --- a/src/Twig/CrudExtension.php +++ b/src/Twig/CrudExtension.php @@ -19,6 +19,7 @@ use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Contracts\Translation\TranslatableInterface; use Twig\Environment; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -330,7 +331,7 @@ public function th(Environment $environment, string $columnId, Crud $crud, array return $value; }); - $resolver->setAllowedTypes('label', ['null', 'string']); + $resolver->setAllowedTypes('label', ['null', 'string', TranslatableInterface::class]); $options = $resolver->resolve($this->buildOptions('crud_th', $options, $crud)); // If the column is not to be shown, returns empty diff --git a/tests/Functional/App/Controller/UserController.php b/tests/Functional/App/Controller/UserController.php index d56cd8c..f3e29d7 100644 --- a/tests/Functional/App/Controller/UserController.php +++ b/tests/Functional/App/Controller/UserController.php @@ -17,6 +17,7 @@ use Ecommit\CrudBundle\Crud\Crud; use Ecommit\CrudBundle\Tests\Functional\App\Entity\TestUser; use Ecommit\CrudBundle\Tests\Functional\App\Form\Searcher\UserSearcher; +use Symfony\Component\Translation\TranslatableMessage; class UserController extends AbstractCrudController { @@ -31,7 +32,7 @@ protected function getCrudOptions(): array $crudConfig = $this->createCrudConfig(static::getCrudName()) ->addColumn('username', 'u.username', 'username', ['displayed_by_default' => false]) ->addColumn('firstName', 'u.firstName', 'first_name') - ->addColumn('lastName', 'u.lastName', 'last_name') + ->addColumn('lastName', 'u.lastName', new TranslatableMessage('last_name')) ->setQueryBuilder($queryBuilder) ->setMaxPerPage([5, 10, 50], 5) ->setDefaultSort('firstName', Crud::ASC)