8000 [Form] Allow to translate each language into its language in LanguageType by javiereguiluz · Pull Request #32388 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Form] Allow to translate each language into its language in LanguageType #32388

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

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
[Form] Allow to translate each language into its language in Language…
…Type
  • Loading branch information
javiereguiluz authored and fabpot committed Dec 3, 2019
commit 138200cd884c13dfeb813f7be75c8598d009b6b3
32 changes: 29 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Intl\Exception\MissingResourceException;
use Symfony\Component\Intl\Languages;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -27,19 +29,43 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
$alpha3 = $options['alpha3'];
$useAlpha3Codes = $options['alpha3'];
$choiceSelfTranslation = $options['choice_self_translation'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) {
return array_flip($alpha3 ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale));
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
if (true === $choiceSelfTranslation) {
foreach (Languages::getLanguageCodes() as $alpha2Code) {
try {
$languageCode = $useAlpha3Codes ? Languages::getAlpha3Code($alpha2Code) : $alpha2Code;
$languagesList[$languageCode] = Languages::getName($alpha2Code, $alpha2Code);
} catch (MissingResourceException $e) {
// ignore errors like "Couldn't read the indices for the locale 'meta'"
}
}
} else {
$languagesList = $useAlpha3Codes ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale);
}

return array_flip($languagesList);
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'alpha3' => false,
'choice_self_translation' => false,
]);

$resolver->setAllowedTypes('choice_self_translation', ['bool']);
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
$resolver->setAllowedTypes('alpha3', 'bool');

$resolver->setNormalizer('choice_self_translation', function (Options $options, $value) {
if (true === $value && $options['choice_translation_locale']) {
throw new LogicException('Cannot use the "choice_self_translation" and "choice_translation_locale" options at the same time. Remove one of them.');
}

return $value;
});
}

/**
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Intl\Util\IntlTestHelper;

class LanguageTypeTest extends BaseTypeTest
Expand Down Expand Up @@ -86,6 +87,52 @@ public function testChoiceTranslationLocaleAndAlpha3Option()
$this->assertNotContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices);
}

/**
* @requires extension intl
*/
public function testChoiceSelfTranslationOption()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, [
'choice_self_translation' => true,
])
->createView()->vars['choices'];

$this->assertContainsEquals(new ChoiceView('cs', 'cs', 'čeština'), $choices);
$this->assertContainsEquals(new ChoiceView('es', 'es', 'español'), $choices);
$this->assertContainsEquals(new ChoiceView('fr', 'fr', 'français'), $choices);
$this->assertContainsEquals(new ChoiceView('ta', 'ta', 'தமிழ்'), $choices);
$this->assertContainsEquals(new ChoiceView('uk', 'uk', 'українська'), $choices);
$this->assertContainsEquals(new ChoiceView('yi', 'yi', 'ייִדיש'), $choices);
$this->assertContainsEquals(new ChoiceView('zh', 'zh', '中文'), $choices);
}

/**
* @requires extension intl
*/
public function testChoiceSelfTranslationAndAlpha3Options()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, [
'alpha3' => true,
'choice_self_translation' => true,
])
->createView()->vars['choices'];

$this->assertContainsEquals(new ChoiceView('spa', 'spa', 'español'), $choices, '', false, false);
$this->assertContainsEquals(new ChoiceView('yid', 'yid', 'ייִדיש'), $choices, '', false, false);
}

public function testSelfTranslationNotAllowedWithChoiceTranslation()
{
$this->expectException(LogicException::class);

$this->factory->create(static::TESTED_TYPE, null, [
'choice_translation_locale' => 'es',
'choice_self_translation' => true,
]);
}

public function testMultipleLanguagesIsNotIncluded()
{
$choices = $this->factory->create(static::TESTED_TYPE, 'language')
Expand Down
0