提问人:Emilie Tossan 提问时间:11/16/2023 更新时间:11/16/2023 访问量:22
如何根据语言显示标志?交响魔法 6.0.2
How can I display flags according to language ? Symfony 6.0.2
问:
我想根据选择的语言显示表情符号。
我遵循了这个解决方案,但它仅适用于国家/地区:https://medium.com/p/f794f39e6ac9
但我想为语言做这件事,这样当我选择一种语言时,我就可以在它旁边有正确的表情符号标志。
我该怎么做?
这是我尝试过的:
更改 => 和 => 是不够的。
Countries
Languages
CountryType
LanguageType
我目前使用哪个是国家代码。
0x1F1A5
有我可以使用的语言代码吗?
表情符号类型.php
<?php
namespace App\Form\Both;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Intl\Countries;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class EmojiType extends AbstractType
{
public function getParent()
{
return CountryType::class;
}
public static function getEmojiFlag(string $countryCode): string
{
$regionalOffset = 0x1F1A5;
return mb_chr($regionalOffset + mb_ord($countryCode[0], 'UTF-8'), 'UTF-8')
. mb_chr($regionalOffset + mb_ord($countryCode[1], 'UTF-8'), 'UTF-8');
}
public function configureOptions(OptionsResolver $resolver)
{
// Define defaults options
$resolver->setDefaults([
// Dfine the list to display
'choice_loader' => function (Options $options) {
return ChoiceList::lazy($this, function () use ($options) {
$choices = [];
// Countries::getNames() return an array with the country code as key and the country name as value
// we fetch all countries code with country name translated according to the locale.
$countriesCode = Countries::getNames($options['choice_translation_locale']);
foreach ($countriesCode as $countryCode => $displayed) {
// we create an array with the country code as key and we concatenate the emoji
// and the country name as value.
$choices[$countryCode] = self::getEmojiFlag($countryCode) . ' ' . $displayed;
}
return array_flip($choices);
});
},
// define the option to define the country by the locale
'choice_translation_locale' => null,
]);
// define the allowed type
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
}
}
新语言.php
<?php
namespace App\Form\Both;
use App\Entity\Language;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class NewLanguageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('code', EmojiType::class)
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Language::class
]);
}
}
答: 暂无答案
评论