Jump to content

[SOLVED] [PS v8.1.3] CustomModuleController - problem with new module translation system.


REGE

Recommended Posts

I wrote a sample controller class for my module:

<?php
namespace MyModule\Controller;

use PrestaShopBundle\Translation\Translator;

final class CustomModuleController 
{
    /** @var Translator */
    private Translator $translator;

    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }
    
    public function foo()
    {
        $this->text = $this->translator->trans('My text to translate', [], 'Modules.Mymodule.Custommoduleclass');
    }
}

 

I would like to translate it. Everything seems fine, but the text to be translated does not appear in BackOffice->International->Translations. However, the translations from the main module file mymodule.php are all there.

Am I doing something wrong? How to translate texts from the module controller?

Edited by REGE
[SOLVED] (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

I found a solution. I replaced Translator with TranslatorInterface and it started working.

<?php
namespace MyModule\Controller;

use Symfony\Contracts\Translation\TranslatorInterface;

final class CustomModuleController 
{
    /** @var TranslatorInterface */
    private TranslatorInterface $translator;

    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }
    
    public function foo()
    {
        $this->text = $this->translator->trans('My text to translate', [], 'Modules.Mymodule.Custommoduleclass');
    }
}

 

 

Link to comment
Share on other sites

  • REGE changed the title to [SOLVED] [PS v8.1.3] CustomModuleController - problem with new module translation system.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...