Forums › Forums › OroPlatform › Task priority translation
This topic contains 14 replies, has 3 voices, and was last updated by William Radi 5 years, 10 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- May 2, 2018 at 12:32 pm #37151
Hello there!
Another translation problem:
I’m trying to create a new task, and there is a choice field called Priority, with the options High, Normal and Low:
I need to translate this options to pt_BR.
I tried to find some key, but I only discovered that it’s an entity…So, do I need to alter some table? And/or create some files? Where?
Thaks, guys! :D
- CreatorTopic
- AuthorReplies
- May 2, 2018 at 1:12 pm #37152
Currently task priority entity is not translatable, seems there is an error.
As there is no any CRUD operations for this entity I recommend you just create form type extension to this field to make choice options translatable with translatable_options = true and then translate it as regular at yml files, it will be the rouge by easiest way.
May 2, 2018 at 2:20 pm #37153Oh.. ok
Understand (half)..But, which files should I create and inside which folders and how can I make choice options translatable?
Like, where to put the translatable_options = true?
What to put inside the Build method?Can you give me an exemple code, please (of form type and yml)? :D
May 3, 2018 at 3:44 am #37154You need to create form type extension, not a form type, there is documentation about it
https://symfony.com/doc/2.8/form/create_form_type_extension.htmlPHP123456789101112131415161718192021222324252627282930namespace AppBundle\Form\Extension;use Oro\Bundle\FormBundle\Utils\FormUtils;use Symfony\Component\Form\AbstractTypeExtension;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\Form\FormEvents;class TaskTypeTypeExtension extends AbstractTypeExtension{/*** {@inheritdoc}*/public function buildForm(FormBuilderInterface $builder, array $options){$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) {$form = $event->getForm();FormUtils::replaceField($form, 'taskPriority', ['translatable_options'=> true]);});}public function getExtendedType(){return TaskType::class;}}And register it at services.yml
YAML12345services:task_type.form_type_extension:class: AppBundle\Form\Extension\TaskTypeTypeExtension;tags:- { name: form.type_extension, extended_type: Oro\Bundle\TaskBundle\Form\Type\TaskType }May 3, 2018 at 5:41 am #37155Thanks for the explanation!
Doing it, I tourn the options translatable. But to effectively translate, I need a key, like ‘oro.task.type. …”, and than, translate it in a messages.pt_BR.yml file, right?
Where do I set them and which name can I use?
Thanks again!
May 3, 2018 at 6:07 am #37156The keys are original english labels, like
Normal
- This reply was modified 5 years, 11 months ago by
Andrey Yatsenko.
May 3, 2018 at 6:54 am #37158Didn’t work =/
My form type extension is like yours, I only changed the namespace…
PHP123456789101112131415161718192021222324252627282930313233<?php// TaskBundle\Form\Extensionnamespace TaskBundle\Form\Extension;use Oro\Bundle\FormBundle\Utils\FormUtils;use Symfony\Component\Form\AbstractTypeExtension;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\Form\FormEvents;class TaskTypeTypeExtension extends AbstractTypeExtension{/*** {@inheritdoc}*/public function buildForm(FormBuilderInterface $builder, array $options){$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) {$form = $event->getForm();FormUtils::replaceField($form, 'taskPriority', ['translatable_options'=> true]);});}public function getExtendedType(){return TaskType::class;}}Service too, just changed class:
YAML123456# Resources\configservices:task_type.form_type_extension:class: TaskBundle\Form\Extension\TaskTypeTypeExtension;tags:- { name: form.type_extension, extended_type: Oro\Bundle\TaskBundle\Form\Type\TaskType }And than, I add a messages.pt_BR.yml file:
YAML1234# Resources\translationsLow: BaixaNormal: NormalHigh: AltaMay 3, 2018 at 6:57 am #37159And my localhost/task/create stil like that
May 3, 2018 at 10:02 am #37160Tried here to run
Scheme1app/console oro:translation:load --env=prodand
Scheme1app/console oro:translation:loadPut
YAML123Low: BaixaNormal: NormalHigh: AltaIn a entities.pt_BR.yml file, I cleared the cache, but nothing again..
May 4, 2018 at 7:51 am #37161I don’t know if it helps, but I noticed that these changes I made, had an effect on the datagrid…
If I try to edit the priority level directly on the datagrid (localhost/task), it pops up in portuguese:
- This reply was modified 5 years, 11 months ago by
William Radi.
May 7, 2018 at 7:22 am #37163Hello, William Radi
Hm, it’s strange. I tried to use your code and see that the options are translated.
It looks like your form type extension does not work.
You can simply check it using xdebug or just add die(); function in form type extension.Did you register your TaskBundle? Also, check that you load service definitions using DependencyInjection’s extension.
May 15, 2018 at 11:20 am #37164Hi, Viacheslav! :D
You’re right! I didn’t add my service.yml to DependencyInjection.
I added it and because of an error message I found a typo in service.yml file.I corrected it, but still not working.. I added the die() inside the buildForm method, but my form doesn’t stop.. And without the die(), it continue not translated… :(
my codes again:
Type extension:PHP12345678910111213141516171819202122232425262728293031323334<?php// Form\Extensionnamespace App\Bundle\TaskBundle\Form\Extension;use Oro\Bundle\FormBundle\Utils\FormUtils;use Symfony\Component\Form\AbstractTypeExtension;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\Form\FormEvents;class TaskTypeTypeExtension extends AbstractTypeExtension{/*** {@inheritdoc}*/public function buildForm(FormBuilderInterface $builder, array $options){$erro = "Erro!!!";die($erro);$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) {$form = $event->getForm();FormUtils::replaceField($form, 'taskPriority', ['translatable_options'=> true]);});}public function getExtendedType(){return TaskType::class;}}Dependency injection:
PHP12345678910111213141516171819202122232425262728293031323334<?php\\ DependencyInjectionnamespace App\Bundle\TaskBundle\DependencyInjection;use Symfony\Component\DependencyInjection\ContainerBuilder;use Symfony\Component\Config\FileLocator;use Symfony\Component\HttpKernel\DependencyInjection\Extension;use Symfony\Component\DependencyInjection\Loader;class AppTaskExtension extends Extension{const ALIAS = 'app_task';/*** {@inheritDoc}*/public function load(array $configs, ContainerBuilder $container){$configuration = new Configuration();$this->processConfiguration($configuration, $configs);$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));$loader->load('form.yml');$loader->load('services.yml');}/*** {@inheritDoc}*/public function getAlias(){return self::ALIAS;}}services.yml:
YAML12345services:task_type.form_type_extension:class: App\Bundle\TaskBundle\Form\Extension\TaskTypeTypeExtensiontags:- { name: form.type_extension, extended_type: Oro\Bundle\TaskBundle\Form\Type\TaskType }May 17, 2018 at 9:09 am #37165Hi William,
Just checked your code,
there are several issues with imports (“use” statements), for example, you forgot to import TaskType to TaskTypeTypeExtension everything else looks fine.Here is working solution based on your code, I just added imports and translation for “en” to Normal option.
https://gist.github.com/anyt/04c5ebd9576b58edf17f9ef0ab5591e1Here is a screenshot, how it looks like
After creating a bundle I just run
app/console oro:translation:load --env=prod
to load new translation and everything works.May 28, 2018 at 7:24 pm #37166Hi, Andrey!
After a lot of tests, I tried your code in a new instance of Oro. Without any other bundle, and it worked!
Thanks a lot for your code, time and patience! :)Well, now it’s just to find the reason.. hehe
Do you have any tips to me?
Thanks again!
May 29, 2018 at 10:11 am #37167Hi, again! lol
The problem is that I already had a form extending of Oro task… My fault!
So, I added the builder inside to my existing form and the field was translate!
Thanks!
- This reply was modified 5 years, 11 months ago by
- AuthorReplies
The forum ‘OroPlatform’ is closed to new topics and replies.