Skip to content

[WIP][Form] Allow to configure selectable locale and countries #28542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Form] Allow to configure selectable locales
  • Loading branch information
sstok committed Sep 21, 2018
commit f05a1ca98ef5c24c2023f79e721320919c1e26e8
32 changes: 32 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/ArrayInclusionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core;

/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*
* @internal
*/
final class ArrayInclusionFilter
{
private $acceptedKeys;

public function __construct(array $acceptedKeys)
{
$this->acceptedKeys = array_fill_keys($acceptedKeys, true);
}

public function __invoke(string $k)
{
return isset($this->acceptedKeys[$k]);
}
}
14 changes: 12 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\ArrayInclusionFilter;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -42,16 +43,25 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(array(
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
$supportedLocales = $options['supported_locales'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale));
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $supportedLocales) {
$locales = Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale);

if (null !== $supportedLocales) {
$locales = array_filter($locales, new ArrayInclusionFilter($supportedLocales), ARRAY_FILTER_USE_KEY);
}

return array_flip($locales);
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'supported_locales' => null,
));

$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
$resolver->setAllowedTypes('supported_locales', array('null', 'string[]'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function testLocalesAreSelectable()
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
}

public function testSupportedLocalesAreConfigurable()
{
$choices = $this->factory->create(static::TESTED_TYPE, null, ['supported_locales' => ['en', 'zh_Hant_MO']])
->createView()->vars['choices'];

$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
$this->assertNotContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false);
}

/**
* @requires extension intl
*/
Expand Down