From 470b4ab5382d5d84198e5299c040cc62e29595ef Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 17 Oct 2020 08:43:17 -0400 Subject: [PATCH] mail templates/views can now be localized (#605) Add mail templates localization. Co-authored-by: Luke Towers Co-authored-by: Ben Thomson --- Plugin.php | 7 ++++ README.md | 10 ++--- classes/EventRegistry.php | 79 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/Plugin.php b/Plugin.php index f0a684c5..0e07072c 100644 --- a/Plugin.php +++ b/Plugin.php @@ -37,6 +37,13 @@ public function pluginDetails() public function register() { + /* + * Load localized version of mail templates (akin to localized CMS content files) + */ + Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) { + return EventRegistry::instance()->findLocalizedMailViewContent($mailer, $message, $view, $data, $raw, $plain); + }, 1); + /* * Defer event with low priority to let others contribute before this registers. */ diff --git a/README.md b/README.md index 00a83cb8..7b34775f 100644 --- a/README.md +++ b/README.md @@ -113,13 +113,13 @@ Add the `--purge` option to clear old messages first: php artisan translate:scan --purge -## Content translation +## Content & mail template translation -This plugin activates a feature in the CMS that allows content files to use language suffixes, for example: +This plugin activates a feature in the CMS that allows content & mail template files to use language suffixes, for example: -* **welcome.htm** will contain the content in the default language. -* **welcome.ru.htm** will contain the content in Russian. -* **welcome.fr.htm** will contain the content in French. +* **welcome.htm** will contain the content or mail template in the default language. +* **welcome.ru.htm** will contain the content or mail template in Russian. +* **welcome.fr.htm** will contain the content or mail template in French. ## Model translation diff --git a/classes/EventRegistry.php b/classes/EventRegistry.php index 6459d0c1..76faed92 100644 --- a/classes/EventRegistry.php +++ b/classes/EventRegistry.php @@ -1,15 +1,17 @@ getBaseFileName(), $extensions); }); } + + /** + * Adds language suffixes to mail view files. + * @param \October\Rain\Mail\Mailer $mailer + * @param \Illuminate\Mail\Message $message + * @param string $view + * @param array $data + * @param string $raw + * @param string $plain + * @return bool|void Will return false if the translation process successfully replaced the original message with a translated version to prevent the original version from being processed. + */ + public function findLocalizedMailViewContent($mailer, $message, $view, $data, $raw, $plain) + { + // Raw content cannot be localized at this level + if (!empty($raw)) { + return; + } + + // Get the locale to use for this template + $locale = !empty($data['_current_locale']) ? $data['_current_locale'] : App::getLocale(); + + $factory = $mailer->getViewFactory(); + + if (!empty($view)) { + $view = $this->getLocalizedView($factory, $view, $locale); + } + + if (!empty($plain)) { + $plain = $this->getLocalizedView($factory, $plain, $locale); + } + + $code = $view ?: $plain; + if (empty($code)) { + return null; + } + + $plainOnly = empty($view); + + if (MailManager::instance()->addContentToMailer($message, $code, $data, $plainOnly)) { + // the caller who fired the event is expecting a FALSE response to halt the event + return false; + } + } + + + /** + * Search mail view files based on locale + * @param \October\Rain\Mail\Mailer $mailer + * @param \Illuminate\Mail\Message $message + * @param string $code + * @param string $locale + * @return string|null + */ + public function getLocalizedView($factory, $code, $locale) + { + $locale = strtolower($locale); + + $searchPaths[] = $locale; + + if (str_contains($locale, '-')) { + list($lang) = explode('-', $locale); + $searchPaths[] = $lang; + } + + foreach ($searchPaths as $path) { + $localizedView = sprintf('%s-%s', $code, $path); + + if ($factory->exists($localizedView)) { + return $localizedView; + } + } + return null; + } }