8000 Rebased and added more tests · symfony/symfony@2012677 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2012677

Browse files
committed
Rebased and added more tests
1 parent e92e367 commit 2012677

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1616
use Symfony\Component\Form\Exception\LogicException;
17+
use Symfony\Component\Intl\Exception\MissingResourceException;
1718
use Symfony\Component\Intl\Languages;
1819
use Symfony\Component\OptionsResolver\Options;
1920
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -28,17 +29,21 @@ public function configureOptions(OptionsResolver $resolver)
2829
$resolver->setDefaults([
2930
'choice_loader' => function (Options $options) {
3031
$choiceTranslationLocale = $options['choice_translation_locale'];
31-
$alpha3 = $options['alpha3'];
32+
$useAlpha3Codes = $options['alpha3'];
3233
$choiceSelfTranslation = $options['choice_self_translation'];
3334

34-
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3, $choiceSelfTranslation) {
35+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
3536
if (true === $choiceSelfTranslation) {
36-
foreach (Languages::getLanguageCodes() as $languageCode) {
37-
$languageCodeKey = $alpha3 ? Languages::getAlpha3Code($languageCode) : $languageCode;
38-
$languagesList[$languageCodeKey] = Languages::getName($languageCode, $languageCode);
37+
foreach (Languages::getLanguageCodes() as $alpha2Code) {
38+
try {
39+
$languageCode = $useAlpha3Codes ? Languages::getAlpha3Code($alpha2Code) : $alpha2Code;
40+
$languagesList[$languageCode] = Languages::getName($alpha2Code, $alpha2Code);
41+
} catch (MissingResourceException $e) {
42+
// ignore errors like "Couldn't read the indices for the locale 'meta'"
43+
}
39 10000 44
}
4045
} else {
41-
$languagesList = Languages::getNames($choiceTranslationLocale);
46+
$languagesList = $useAlpha3Codes ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale);
4247
}
4348

4449
return array_flip($languagesList);

src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php

Lines changed: 24 additions & 10 deletions
< 8000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
15+
use Symfony\Component\Form\Exception\LogicException;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

1718
class LanguageTypeTest extends BaseTypeTest
@@ -97,22 +98,35 @@ public function testChoiceSelfTranslationOption()
9798
])
9899
->createView()->vars['choices'];
99100

100-
// Don't check objects for identity
101-
$this->assertContains(new ChoiceView('cs', 'cs', 'čeština'), $choices, '', false, false);
102-
$this->assertContains(new ChoiceView('es', 'es', 'español'), $choices, '', false, false);
103-
$this->assertContains(new ChoiceView('fr', 'fr', 'français'), $choices, '', false, false);
104-
$this->assertContains(new ChoiceView('ta', 'ta', 'தமிழ்'), $choices, '', false, false);
105-
$this->assertContains(new ChoiceView('uk', 'uk', 'українська'), $choices, '', false, false);
106-
$this->assertContains(new ChoiceView('yi', 'yi', 'ייִדיש'), $choices, '', false, false);
107-
$this->assertContains(new ChoiceView('zh', 'zh', '中文'), $choices, '', false, false);
108-
$this->assertContains(new ChoiceView('zh_Hant', 'zh_Hant', '繁體中文'), $choices, '', false, false);
101+
$this->assertContainsEquals(new ChoiceView('cs', 'cs', 'čeština'), $choices);
102+
$this->assertContainsEquals(new ChoiceView('es', 'es', 'español'), $choices);
103+
$this->assertContainsEquals(new ChoiceView('fr', 'fr', 'français'), $choices);
104+
$this->assertContainsEquals(new ChoiceView('ta', 'ta', 'தமிழ்'), $choices);
105+
$this->assertContainsEquals(new ChoiceView('uk', 'uk', 'українська'), $choices);
106+
$this->assertContainsEquals(new ChoiceView('yi', 'yi', 'ייִדיש'), $choices);
107+
$this->assertContainsEquals(new ChoiceView('zh', 'zh', '中文'), $choices);
109108
}
110109

111110
/**
112-
* @expectedException \Symfony\Component\Form\Exception\LogicException
111+
* @requires extension intl
113112
*/
113+
public function testChoiceSelfTranslationAndAlpha3Options()
114+
{
115+
$choices = $this->factory
116+
->create(static::TESTED_TYPE, null, [
117+
'alpha3' => true,
118+
'choice_self_translation' => true,
119+
])
120+
->createView()->vars['choices'];
121+
122+
$this->assertContainsEquals(new ChoiceView('spa', 'spa', 'español'), $choices, '', false, false);
123+
$this->assertContainsEquals(new ChoiceView('yid', 'yid', 'ייִדיש'), $choices, '', false, false);
124+
}
125+
114126
public function testSelfTranslationNotAllowedWithChoiceTranslation()
115127
{
128+
$this->expectException(LogicException::class);
129+
116130
$this->factory->create(static::TESTED_TYPE, null, [
117131
'choice_translation_locale' => 'es',
118132
'choice_self_translation' => true,

0 commit comments

Comments
 (0)
0