Elao/PhpEnums

[SF5.3, PHP7.4] Translation

Closed this issue · 6 comments

Hi i have try to translated

My config :

elao_enum:
    # Provide the enum classes for which you want to automatically generate & register Doctrine DBAL Types:
    # https://github.com/Elao/PhpEnums#doctrine
    doctrine:
        types: {}

    translation_extractor:
        # mandatory, provides the namespace to path mappings where to search for ReadableEnum (will also search subdirectories)
        paths:
            App\Enum: '%kernel.project_dir%/src/Enum'
        domain: profile # optional, specifies the domain for translations
        filename_pattern: '*.php' # optional, specifies the filename pattern when searching in folders
        ignore: []

my gender

namespace App\Enum;

use Elao\Enum\ReadableEnum;

final class Gender extends ReadableEnum
    {
    public const MASCULINE      = 'M';
    public const FEMININE       = 'E';
    public const TRANSGENDER    = 'T';

    /**
     * @inheritDoc
     */
    public static function values(): array
    {
        return [
            self::MASCULINE    ,
            self::FEMININE     ,
            self::TRANSGENDER  ,
        ];
    }

    /**
     * @return array
     */
    public static function readables(): array
    {
        return [
            self::MASCULINE     => "enum.genders.m",
            self::FEMININE      => "enum.genders.f",
            self::TRANSGENDER   => "enum.genders.x",
        ];
    }
    }

my translation file : profile.fr.yml

enum.genders.m: masculin
enum.genders.f: féminin
enum.genders.x: 3e sexe

the render is "enum.genders.m" ???
If i just the translation in message.fr.yml all working well. But to be more readable i wanted to put translation in separate file.

thank for any help!!

Hi @xorgxx .
If I understand correctly, you'd expect your enum instance to be translated when exposed in a Twig template, right?
You probably missed a call to | trans in your template:

{{ my_enum_instance|trans }}
{# same as: #}
{{ my_enum_instance.readable|trans }}

without the trans filter, it'll only cast the instance to string using getReadable(), which returns the translation key.
There is no magic calling the translator for you here.

Oh, sorry got it:

If i just the translation in message.fr.yml all working well. But to be more readable i wanted to put translation in separate file.

since you use a profile domain (profile.fr.yml file), you should specify it when translating your instance:

{{ my_enum_instance|trans(domain="profile") }}

thank to be so fast !!!

well i have try simple as this in Symfony formType

        $builder
            ->add('gender', EnumType::class, array(
                    'enum_class' => Gender::class,
                    //                    "attr"          => array(
//                        "placeholder"   => "telemaintenance.form-user.login.placeholder",
//                    ),
//                    'label' => 'form-user.form.registration.gender.label',
//                        'translation_domain'    => 'FOSUserBundle'
                )
            )
```

i deed not change or add any think in twig.

Try this:

    $builder
        ->add('gender', EnumType::class, array(
            'enum_class' => Gender::class,
            "attr" => array(
                "placeholder"   => "telemaintenance.form-user.login.placeholder",
            ),
           'label' => 'form-user.form.registration.gender.label',
           'translation_domain'    => 'FOSUserBundle',
           'choice_translation_domain' => 'profile', # <-- Add this line
        )

See https://symfony.com/doc/current/reference/forms/types/choice.html#choice-translation-domain

Thank !!
your "patch" is working.

but still i was thinking that to add

elao_enum:
    # Provide the enum classes for which you want to automatically generate & register Doctrine DBAL Types:
    # https://github.com/Elao/PhpEnums#doctrine
    doctrine:
        types: {}

    translation_extractor:
        # mandatory, provides the namespace to path mappings where to search for ReadableEnum (will also search subdirectories)
        paths:
            App\Enum: '%kernel.project_dir%/src/Enum'
        domain: profile # optional, specifies the domain for translations
        filename_pattern: '*.php' # optional, specifies the filename pattern when searching in folders
        ignore: []

will do the job ?

The translation_extractor is a different feature allowing to hook on the Symfony Translation Extractor feature to autodetect your translation keys from your PHP code.
It does not configure the default domain to use for translating your enum, only the domain we should consider extracting the translation key to.
Actually there is no such concept allowing to register a specific domain for your objects ; you always need to be explicit (and that's fortunate)