8000 feature #32388 [Form] Allow to translate each language into its langu… · symfony/symfony@9097230 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 9097230

Browse files
committed
feature #32388 [Form] Allow to translate each language into its language in LanguageType (javiereguiluz)
This PR was squashed before being merged into the 5.1-dev branch (closes #32388). Discussion ---------- [Form] Allow to translate each language into its language in LanguageType | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #31562 | License | MIT | Doc PR | I'll do it if this is approved This would allow to set this option: ```php ->add('language', LanguageType::class, [ 'choice_self_translation' => true, ]) ``` To display each language translated into its own language: ![image](https://user-images.githubusercontent.com/73419/60709908-cdf29b80-9f11-11e9-83c8-8ee939c0de3a.png) @ro0NL if this proposal is approved, could you please tell me why I must `try ... catch` with `\Exception`? * First, I need the try..catch because of a special language with the code "root" which triggers exceptions (*The resource bundle "symfony-code/src/Symfony/Component/Intl/Resources/data/languages/root.json" does not exist*) * Second, I must catch \Exception because when I try to catch the exact exception (`Symfony\Component\Intl\Exception\ResourceBundleNotFoundException`) the exception is not caught. Why? Commits ------- 138200c [Form] Allow to translate each language into its language in LanguageType
2 parents e0f6cdb + 138200c commit 9097230

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
16+
use Symfony\Component\Form\Exception\LogicException;
17+
use Symfony\Component\Intl\Exception\MissingResourceException;
1618
use Symfony\Component\Intl\Languages;
1719
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -27,19 +29,43 @@ public function configureOptions(OptionsResolver $resolver)
2729
$resolver->setDefaults([
2830
'choice_loader' => function (Options $options) {
2931
$choiceTranslationLocale = $options['choice_translation_locale'];
30-
$alpha3 = $options['alpha3'];
32+
$useAlpha3Codes = $options['alpha3'];
33+
$choiceSelfTranslation = $options['choice_self_translation'];
3134

32-
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) {
33-
return array_flip($alpha3 ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale));
35+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
36+
if (true === $choiceSelfTranslation) {
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+
}
44+
}
45+
} else {
46+
$languagesList = $useAlpha3Codes ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale);
47+
}
48+
49+
return array_flip($languagesList);
3450
});
3551
},
3652
'choice_translation_domain' => false,
3753
'choice_translation_locale' => null,
3854
'alpha3' => false,
55+
'choice_self_translation' => false,
3956
]);
4057

58+
$resolver->setAllowedTypes('choice_self_translation', ['bool']);
4159
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
4260
$resolver->setAllowedTypes('alpha3', 'bool');
61+
62+
$resolver->setNormalizer('choice_self_translation', function (Options $options, $value) {
63+
if (true === $value && $options['choice_translation_locale']) {
64+
throw new LogicException('Cannot use the "choice_self_translation" and "choice_translation_locale" options at the same time. Remove one of them.');
65+
}
66+
67+
return $value;
68+
});
4369
}
4470

4571
/**

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

Lines changed: 47 additions & 0 deletions
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
@@ -86,6 +87,52 @@ public function testChoiceTranslationLocaleAndAlpha3Option()
8687
$this->assertNotContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices);
8788
}
8889

90+
/**
91+
* @requires extension intl
92+
*/
93+
public function testChoiceSelfTranslationOption()
94+
{
95+
$choices = $this->factory
96+
->create(static::TESTED_TYPE, null, [
97+
'choice_self_translation' => true,
98+
])
99+
->createView()->vars['choices'];
100+
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);
108+
}
109+
110+
/**
111+
* @requires extension intl
112+
*/
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+
126+
public function testSelfTranslationNotAllowedWithChoiceTranslation()
127+
{
128+
$this->expectException(LogicException::class);
129+
130+
$this->factory->create(static::TESTED_TYPE, null, [
131+
'choice_translation_locale' => 'es',
132+
'choice_self_translation' => true,
133+
]);
134+
}
135+
89136
public function testMultipleLanguagesIsNotIncluded()
90137
{
91138
$choices = $this->factory->create(static::TESTED_TYPE, 'language')

0 commit comments

Comments
 (0)
0