Skip to content

Commit

Permalink
mail templates/views can now be localized (#605)
Browse files Browse the repository at this point in the history
Add mail templates localization.

Co-authored-by: Luke Towers <[email protected]>
Co-authored-by: Ben Thomson <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2020
1 parent 05180ee commit 470b4ab
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
79 changes: 77 additions & 2 deletions classes/EventRegistry.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php namespace RainLab\Translate\Classes;

use Str;
use App;
use Exception;
use File;
use Str;
use Cms\Classes\Page;
use Cms\Classes\Content;
use System\Classes\MailManager;
use System\Classes\PluginManager;
use RainLab\Translate\Models\Message;
use RainLab\Translate\Models\Locale as LocaleModel;
use RainLab\Translate\Classes\Translator;
use RainLab\Translate\Classes\ThemeScanner;
use Exception;

/**
* Registrant class for bootstrapping events
Expand Down Expand Up @@ -257,4 +259,77 @@ public function pruneTranslatedContentTemplates($templates)
return !Str::endsWith($template->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;
}
}

0 comments on commit 470b4ab

Please sign in to comment.