rboada Posted May 2, 2024 Share Posted May 2, 2024 Hello, I am developing a module with modern controllers and I have a problem with translations. I want to use the trans() function in my custom type that extends from AbsctractType, but I can't find a way to make it work. I am using the TranslatorAwareTrait trait as used by the EmployeeType class, I have even declared the service as follows: services: _defaults: public: true mycompany.module.excelstore.form.type.download_type: class: 'Mycompany\Module\ExcelStore\Form\DonwloadType'. calls: - { method: setTranslator, arguments: ['@translator'] } tags: - { name: form.type } My class: class DownloadType extends AbstractType { use TranslatorAwareTrait; public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('id_category', CategoryChoiceTreeType::class, [ 'required' => false, 'label' => 'Category', 'multiple' => false, 'disabled_values' => $this->getDisabledCategories(), ]) ->add('from', DateType::class, [ 'required' => false, 'label' => $this->trans('From', [], 'Admin.Global'), 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'class' => 'aaa' ]) ->add('to', DateType::class, [ 'required' => false, 'label' => 'To', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', ]); } } But when entering the controller I get this error: FatalThrowableError: Call to a member function trans() on null. DownloadType->trans('From', array(), 'Admin.Global') in srcPrestaShopBundleTranslationTranslatorAwareTrait.php (line 62) The line: return $this->translator->trans($id, $options, $domain); Can you help me? Thanks in advance. Link to comment Share on other sites More sharing options...
Prestashop Addict Posted May 2, 2024 Share Posted May 2, 2024 You don't have a translator object initialized in your class, use Context::getContext()->getTranslator()->trans(...); Link to comment Share on other sites More sharing options...
rboada Posted May 2, 2024 Author Share Posted May 2, 2024 I finally decided to use the Context class, but I really wanted to do it in a "cleaner" way. Still, I'm left wanting to know how to use translations without the Context class. public $translator; public function __construct() { $this->translator = \Context::getContext()->getTranslator(); } public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('id_category', CategoryChoiceTreeType::class, [ 'required' => false, 'label' => $this->translator->trans('Category', [], 'Admin.Global'), 'multiple' => false, 'disabled_values' => $this->getDisabledCategories(), ]) ->add('from', DateType::class, [ 'required' => false, 'label' => $this->translator->trans('From', [], 'Admin.Global'), 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', ]) ->add('to', DateType::class, [ 'required' => false, 'label' => $this->translator->trans('To', [], 'Admin.Global'), 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', ]); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now