Kdyby/Translation in model

Notice: This thread is very old.
Azathoth
Member | 495
+
0
-

Hello,

I want to use Kdyby/Translation to translate e-mails. I have class BaseMessage:

<?php

namespace App\Model\Emails;

use Nette\Mail\Message;
use Kdyby\Translation;

/**
 * Description of BaseMessage
 *
 * @author Azathoth
 */
class BaseMessage extends Message{
    /** @persistent */
    public $locale;

     /** @var \Kdyby\Translation\Translator */
    protected $translator;

    public function __construct(Translation\Translator $translator) {
        parent::__construct();
        $this->translator = $translator;
    }
}
?>

and class ForgottenPassswordMail extending BaseMessage

<?php

namespace App\Model\Emails;

/**
 * Description of ForgottenPasswordMail
 *
 * @author Azathoth
 */
class ForgottenPasswordMail extends BaseMessage{



    function __construct($email, $password) {
        parent::__construct();
        \Tracy\Debugger::fireLog($this->translator);
        $subject = $this->translator->translate('text.e-mail.forgotten_password_subject');
        \Tracy\Debugger::fireLog($subject);

        $this->setFrom('goth@goth.goth')
            ->addTo($email)
            ->setSubject(
                $subject
            )
            ->setBody(
                $this->translator->translate('text.e-mail.forgotten_password_body', NULL, array('password' => $password))
            );
    }


}
?>

and here is my config.neon
php:
date.timezone: Europe/Prague

nette:
application:
errorPresenter: Error
mapping:
*: App*Module\Presenters*Presenter

session:
expiration: 14 days

services
App\Model\UserHandler
App\Model\MailHandler
App\RouterFactory

router: @App\RouterFactory::createRouter

translation:
default: en
whitelist: [en]
fallback: [en]

extensions:
translation: Kdyby\Translation\DI\TranslationExtension

And it thows exception:
Recoverable Error

Argument 1 passed to App\Model\Emails\BaseMessage::__construct() must be an instance of Kdyby\Translation\Translator, none given, called in app\model\emails\ForgottenPasswordMail.php on line 15 and defined

So I thought Kdyby/Translation is missing in config.neon services and I added one line, so services now look like this:

services
App\Model\UserHandler
App\Model\MailHandler
Kdyby\Translation\Translator
App\RouterFactory

router: @App\RouterFactory::createRouter

but it throws Service ‘62_Kdyby_Translation_Translator’: Multiple services of type Kdyby\Translation\IUserLocaleResolver found: translation.userLocaleResolver.acceptHeader, translation.userLocaleResolver.session, translation.userLocaleResolver

but I do not use Kdyby\Translation\IUserLocaleResolver service.

What should I do? I will be very grateful for answers.

jiri.pudil
Nette Blogger | 1028
+
0
-

I'd say the message is pretty self-explanatory. You call parent::__construct() in ForgottenPasswordMail without passing its argument (the translator) to it. (And one more thing, I'd rather stick with the ITranslator interface as a type-hint.)

class ForgottenPasswordMail extends BaseMessage
{

	public function __construct(Nette\Localization\ITranslator $translator, $email, $password)
	{
		parent::__construct($translator);
		// ...
	}

}
Azathoth
Member | 495
+
0
-

Oh, thanks, I totally forgot about constructor parameter…
I edited it as you wrote, but it stil does not work.
Here is what Tracy says:
…\vendor\nette\di\src\DI\ContainerBuilder.php:420
Caused by
Nette\DI\ServiceCreationException
File: …\vendor\nette\di\src\DI\ContainerBuilder.php:134

and when I look at stack trace, the constructor is not called, exception occurs in compiler.