Improve locale setting within user profile, using Languages symfony component

This commit is contained in:
2025-11-07 14:37:46 +01:00
parent 77311b6290
commit 29536449ae
5 changed files with 16 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
chill_main:
available_languages: [ '%env(resolve:LOCALE)%', 'en' ]
available_languages: [ '%env(resolve:LOCALE)%', 'en', 'nl' ]
available_countries: ['BE', 'FR']
notifications:
from_email: '%env(resolve:NOTIFICATION_FROM_EMAIL)%'

View File

@@ -727,10 +727,6 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter
public function setLocale(string $locale): self
{
if (!in_array($locale, ['fr', 'nl'], true)) {
throw new \InvalidArgumentException('Locale must be either "fr" or "nl"');
}
$this->locale = $locale;
return $this;

View File

@@ -13,17 +13,22 @@ namespace Chill\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Intl\Languages;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserLocaleType extends AbstractType
{
public function __construct(private readonly array $availableLanguages) {}
public function configureOptions(OptionsResolver $resolver): void
{
$choices = [];
foreach ($this->availableLanguages as $languageCode) {
$choices[Languages::getName($languageCode)] = $languageCode;
}
$resolver->setDefaults([
'choices' => [
'user.locale.choice.french' => 'fr',
'user.locale.choice.dutch' => 'nl',
],
'choices' => $choices,
'placeholder' => 'user.locale.placeholder',
'required' => true,
'label' => 'user.locale.label',

View File

@@ -12,6 +12,12 @@ services:
tags:
- { name: form.type, alias: translatable_string }
Chill\MainBundle\Form\Type\UserLocaleType:
arguments:
- "%chill_main.available_languages%"
tags:
- { name: form.type }
chill.main.form.type.select2choice:
class: Chill\MainBundle\Form\Type\Select2ChoiceType
tags:

View File

@@ -60,9 +60,6 @@ user:
label: Langue de communication
help: Langue utilisée pour les notifications par email et autres communications.
placeholder: Choisissez une langue
choice:
french: Français
dutch: Nederlands
user_group:
inactive: Inactif