Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] localized mail templates #534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function pluginDetails()

public function register()
{
/*
* Adds language suffix to Mail templates.
*/
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) {
return EventRegistry::instance()->findTranslatedMailTemplate($mailer, $message, $view, $data, $raw, $plain);
}, 1);

/*
* Defer event with low priority to let others contribute before this registers.
*/
Expand Down
27 changes: 27 additions & 0 deletions classes/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use File;
use Cms\Classes\Page;
use Cms\Classes\Content;
use System\Classes\MailManager;
use RainLab\Translate\Models\Message;
use RainLab\Translate\Models\Locale as LocaleModel;
use RainLab\Translate\Classes\Translator;
Expand Down Expand Up @@ -201,6 +202,32 @@ public function findTranslatedContentFile($controller, $fileName)
}
}

/**
* Adds language suffixes to Mail templates.
* @return boolean
*/
public function findTranslatedMailTemplate($mailer, $message, $view, $data, $raw, $plain)
{
$translator = Translator::instance();

$locale = $translator->getLocale();
$defaultLocale = $translator->getDefaultLocale();

if ($raw !== null || $view === null || !is_string($view) || $locale === $defaultLocale) {
return;
}

$mailManager = MailManager::instance();
$templates = $mailManager->listRegisteredTemplates();

$localizedView = sprintf('%s-%s', $view, $locale);
if (!array_key_exists($localizedView, $templates)) {
return;
}

return !$mailManager->addContentToMailer($message, $localizedView, $data, false);
}

//
// Static pages
//
Expand Down