-
Notifications
You must be signed in to change notification settings - Fork 5
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
Comments
Hi, happy to know that this bundle is helpful :) Can you show me the piece of code you used in you Sonata ? |
I currently do it like this, but I don't think it's the right solution. Code for the list method
Code for the show method
I extended the sonata views for both fileds with this code :
And in the datagrid I have this
And
Thanks for your answer |
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 Hope that helps |
Thank you, it helps a lot! |
@Nils-Laurent what do you think about this PR #10 ? clear enough ? |
It is very clear. |
No need to create twig template for list field.
|
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):
Now the important about the formMapper and listMapper. This TranslationFile is on rootProject/translations/TranslationFile.en.xliff and on the content of the file is something like: Now for the listMapper: Care to remind that I don't know if this is the correct way, but it does translate the fields I want. |
@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... |
@yann-eugone can you try to be a little more specific on what you meant? |
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. |
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?
The text was updated successfully, but these errors were encountered: