summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-10-06 21:03:20 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2019-10-06 21:18:42 +0200
commitfaf74150e9481ad9338eb6cc2428d02b24e9fc43 (patch)
tree185d36e48de96f9070038ce6a6e27c3fe6d8880a /src
parent24bfc292208082513fd1159cb3176e08b1686c11 (diff)
Mailer: Use users locale for translation
Diffstat (limited to 'src')
-rw-r--r--src/Helpers/Translation/TranslationServiceProvider.php6
-rw-r--r--src/Mail/EngelsystemMailer.php59
2 files changed, 57 insertions, 8 deletions
diff --git a/src/Helpers/Translation/TranslationServiceProvider.php b/src/Helpers/Translation/TranslationServiceProvider.php
index 09337dad..05e782ec 100644
--- a/src/Helpers/Translation/TranslationServiceProvider.php
+++ b/src/Helpers/Translation/TranslationServiceProvider.php
@@ -40,8 +40,10 @@ class TranslationServiceProvider extends ServiceProvider
'localeChangeCallback' => [$this, 'setLocale'],
]
);
- $this->app->instance(Translator::class, $translator);
- $this->app->instance('translator', $translator);
+ $this->app->singleton(Translator::class, function () use ($translator) {
+ return $translator;
+ });
+ $this->app->alias(Translator::class, 'translator');
}
/**
diff --git a/src/Mail/EngelsystemMailer.php b/src/Mail/EngelsystemMailer.php
index 81660681..87915d67 100644
--- a/src/Mail/EngelsystemMailer.php
+++ b/src/Mail/EngelsystemMailer.php
@@ -2,6 +2,8 @@
namespace Engelsystem\Mail;
+use Engelsystem\Helpers\Translation\Translator;
+use Engelsystem\Models\User\User;
use Engelsystem\Renderer\Renderer;
use Swift_Mailer as SwiftMailer;
@@ -10,30 +12,75 @@ class EngelsystemMailer extends Mailer
/** @var Renderer|null */
protected $view;
+ /** @var Translator|null */
+ protected $translation;
+
/** @var string */
protected $subjectPrefix = null;
/**
* @param SwiftMailer $mailer
* @param Renderer $view
+ * @param Translator $translation
*/
- public function __construct(SwiftMailer $mailer, Renderer $view = null)
+ public function __construct(SwiftMailer $mailer, Renderer $view = null, Translator $translation = null)
{
parent::__construct($mailer);
+ $this->translation = $translation;
$this->view = $view;
}
/**
+ * @param string|string[]|User $to
+ * @param string $subject
+ * @param string $template
+ * @param array $data
+ * @param string|null $locale
+ * @return int
+ */
+ public function sendViewTranslated(
+ $to,
+ string $subject,
+ string $template,
+ array $data = [],
+ ?string $locale = null
+ ): int {
+ if ($to instanceof User) {
+ $locale = $locale ?: $to->settings->language;
+ $to = $to->contact->email ? $to->contact->email : $to->email;
+ }
+
+ $activeLocale = null;
+ if (
+ $locale
+ && $this->translation
+ && isset($this->translation->getLocales()[$locale])
+ ) {
+ $activeLocale = $this->translation->getLocale();
+ $this->translation->setLocale($locale);
+ }
+
+ $subject = $this->translation ? $this->translation->translate($subject) : $subject;
+ $sentMails = $this->sendView($to, $subject, $template, $data);
+
+ if ($activeLocale) {
+ $this->translation->setLocale($activeLocale);
+ }
+
+ return $sentMails;
+ }
+
+ /**
* Send a template
*
- * @param string $to
- * @param string $subject
- * @param string $template
- * @param array $data
+ * @param string|string[] $to
+ * @param string $subject
+ * @param string $template
+ * @param array $data
* @return int
*/
- public function sendView($to, $subject, $template, $data = []): int
+ public function sendView($to, string $subject, string $template, array $data = []): int
{
$body = $this->view->render($template, $data);