How can I display flags according to language ? Symfony 6.0.2

92 Views Asked by At

I would like to display emoji according to language selected.

I followed a this solution but it works only for countries : https://medium.com/p/f794f39e6ac9

But I would like to do it for languages so that when I select a language and I can have the right emoji flag next to it.

How can I do it ?

Here is what I have tried :

Change Countries => Languages and CountryType => LanguageType is not enough.

I currently use 0x1F1A5 which is country code.

Is there a language code I can use ?

EmojiType.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']);
    }
}

NewLanguage.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
        ]);
    }
}
0

There are 0 best solutions below