Skip to content
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

How to translate on list and show with sonata #9

Closed
Nils-Laurent opened this issue Jun 9, 2016 · 11 comments
Closed

How to translate on list and show with sonata #9

Nils-Laurent opened this issue Jun 9, 2016 · 11 comments

Comments

@Nils-Laurent
Copy link

Hi, thanks for your bundle, it helps me a lot.
I use the bundle with sonata on forms but the enum is not translated. I saw that the translator is looking in the "messages" domain, but the translation is in enum.xliff. I guess that sonata somehow overrides the translation domain. I didn't managed to find a proper solution, am I missing something?

@yann-eugone
Copy link
Collaborator

Hi, happy to know that this bundle is helpful :)

Can you show me the piece of code you used in you Sonata ?

@Nils-Laurent
Copy link
Author

I currently do it like this, but I don't think it's the right solution.
I have my admin class : CommandHistoryAdmin extends AbstractAdmin with a list and show method.

Code for the list method

    protected function configureListFields(ListMapper $list)
    {
        $list
            ->addIdentifier('id')
            ->add('command', 'string', array('template' => ':admin/custom:list_enum_command.html.twig'))
            ...

Code for the show method

    protected function configureShowFields(ShowMapper $show)
    {
        $show
            ->add('id')
            ->add('command', null, array('template' => ':admin/custom:show_enum_command.html.twig'))
            ...

I extended the sonata views for both fileds with this code :

{% block field %}
    <div>
        {{ ('enum.command.' ~ object.command)|trans({},'enum')}}
    </div>
{% endblock %}

And in the datagrid I have this

    protected function configureDatagridFilters(DatagridMapper $filter)
    {
        $filter
            ->add(
                'command',
                'doctrine_orm_choice',
                array('label' => 'filter.label_command'),
                'choice',
                array(
                    'translation_domain' => 'enum',
                    'choices' => array('enum.command.group_command' => CommandEnum::getSelectChoices()),
                    'expanded' => false,
                    'multiple' => false
                )
             ...

And CommandEnum::getSelectChoices() is a static method added :

class CommandEnum extends AbstractTranslatedEnum
...
    public static function getSelectChoices()
    {
        return [
            self::COUNT_VIEW_BY_REPORTER => 'enum.command.'.self::COUNT_VIEW_BY_REPORTER,
            self::COUNT_VIEW_BY_EVENT => 'enum.command.'.self::COUNT_VIEW_BY_EVENT,
            self::UPDATE_LIVE_PARTICIPANT => 'enum.command.'.self::UPDATE_LIVE_PARTICIPANT,
            self::COUNT_EVENT_BY_REPORTER => 'enum.command.'.self::COUNT_EVENT_BY_REPORTER,
            self::ENDED_EVENT => 'enum.command.'.self::ENDED_EVENT
        ];
    }
...

Thanks for your answer

@yann-eugone
Copy link
Collaborator

Ok, the follow code is extracted from a project where I used SonataAdmin and this bundle.

The admin class

<?php

namespace AppBundle\Admin;

use AppBundle\Enum\GenderEnum;
use EnumBundle\Form\Type\EnumType;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;

class PersonAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $list)
    {
        $list
            ->add('gender', null, [
                'template' => 'admin/person/list_gender.html.twig',
            ])
            //...
        ;
    }

    protected function configureDatagridFilters(DatagridMapper $filter)
    {
        $filter
            ->add('gender', 'doctrine_orm_choice', [
                'field_type' => EnumType::class,
                'field_options' => [
                    'enum' => GenderEnum::NAME,
                ],
            ])
            //...
        ;
    }

    protected function configureFormFields(FormMapper $form)
    {
        $form
            ->add('gender', EnumType::class, [
                'enum' => GenderEnum::NAME,
            ])
            //...
        ;
    }
}

The list template

{% extends admin.getTemplate('base_list_field') %}

{% block field %}
    {{ value|enum_label(constant('AppBundle\\Enum\\Person\\GenderEnum::NAME')) }}
{% endblock %}

You can notice that you can directly use the EnumType form type in the datagrid filter.
You can also notice that translation is done using the enum_label filter that was added in v1.2.

Hope that helps

@Nils-Laurent
Copy link
Author

Thank you, it helps a lot!
I will follow this example in my project.

@yann-eugone
Copy link
Collaborator

@Nils-Laurent what do you think about this PR #10 ? clear enough ?

@Nils-Laurent
Copy link
Author

It is very clear.
Thanks you for your efforts.

@IvanAlekseevichPopov
Copy link

IvanAlekseevichPopov commented Aug 14, 2018

No need to create twig template for list field.

<?php

class SomethingAdmin extend AbstractAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('name')
            ->add('type', 'choice', [
                'choices' => ProductEnumType::getReadableValues(), //this returns array like ['db_enum_value' => 'title.for_emum.translate']
                'catalogue' => 'admin', //this sets translation domain
            ])
}

@RenatoAlmeidaPTC
Copy link

RenatoAlmeidaPTC commented Aug 30, 2018

I also had a problem about translating some choice in sonata admin. I don't know if its the right way.

To explain i first must say i'm using symfony 4, my whole entity is based on enum (every attribute has his own enum) and of cource, the databse will only save values accordingly the enum.

To start, I used something like @IvanAlekseevichPopov strategy to get the values to the form with a ProductEnumType::getReadableValues(), but this would return something like ['xpto' => 'xpto'] (more precise, in the next code snippet):

const XPTO = 'xpto'; ... public static function getValuesToForm(){ return [selft::XPTO => 'xpto'] }

Now the important about the formMapper and listMapper.
As for the formMapper is is something like this:
$formMapper->add('attribute', ChoiceType::class, [ 'choices' => ProductEnumType::getReadableValues(), 'translation_domain' => 'TranslationFile']);

This TranslationFile is on rootProject/translations/TranslationFile.en.xliff and on the content of the file is something like:
<?xml version="1.0" encoding="UTF-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <file source-language="en" datatype="plaintext" original=""> <body> <trans-unit id="translation.key"> <source>database_value/attribute_value</source> <target>translation_to_show</target> </trans-unit> </body> </file> </xliff>

Now for the listMapper:
$listMapper ->add('attribute', 'trans', ['catalogue' => 'TranslationFile']);

Care to remind that I don't know if this is the correct way, but it does translate the fields I want.

@yann-eugone
Copy link
Collaborator

@IvanAlekseevichPopov @RenatoAlmeidaPTC using a static method from you code, you will not be able to plug in the enum process.

To be clear, the way you describe your code, I don't see anything about this bundle...

@RenatoAlmeidaPTC
Copy link

RenatoAlmeidaPTC commented Aug 30, 2018

@yann-eugone can you try to be a little more specific on what you meant?
As I told, i don't know if what I did is the correct way, just because the results are the ones I need, doens't mean its right, but for now, is the only way I have.
If you have any other way to translate a 'choice' attribute into the show/list view of the sonata admin, please, share with us :)

@yann-eugone
Copy link
Collaborator

Yes, it may work, and I think it is just fine.

I'm just saying that your solution has nothing to do with this bundle : not entering the enum system to render the list element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants