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

Mail templates/views can now be localized #605

Merged
merged 26 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ef0743d
mail templates can now be localized
mjauvin Sep 21, 2020
4c17040
remove default locale
mjauvin Sep 21, 2020
ccb299e
no need for Mail class, we have $mailer
mjauvin Sep 22, 2020
3116e20
take plain view into account
mjauvin Sep 22, 2020
9dfa266
just use App::getLocale(), works in all cases
mjauvin Sep 23, 2020
ce0bf31
use previously saved locale if available
mjauvin Sep 23, 2020
587aabc
improve event handler name
mjauvin Sep 23, 2020
a08f83b
search more specific locale first, then country specific locale
mjauvin Sep 23, 2020
03dad82
add comments and improve code
mjauvin Sep 23, 2020
3fb1ba2
this should actually be called lang, not country
mjauvin Sep 26, 2020
eceb63c
explicitly return null
mjauvin Oct 13, 2020
00dc618
improve locale fetching code
mjauvin Oct 13, 2020
d8b5263
improve code readability
mjauvin Oct 13, 2020
a1f294a
improve code readability
mjauvin Oct 14, 2020
853f96c
fix reversed logic
mjauvin Oct 15, 2020
9ce9ffb
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
28fadbf
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
b75ef09
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
f9340a4
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
4eddbc2
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
a1b61f3
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
38c093a
Update classes/EventRegistry.php
LukeTowers Oct 15, 2020
b34daf9
be more explicit about the expected response to halt the event
mjauvin Oct 15, 2020
93a0361
fix screwup
mjauvin Oct 15, 2020
a91f120
Update Plugin.php
bennothommo Oct 16, 2020
63a0a33
mention mail templates translation in documentation
mjauvin Oct 17, 2020
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 @@ -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 @@ -263,4 +265,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;
}
mjauvin marked this conversation as resolved.
Show resolved Hide resolved

// Get the locale to use for this template
$locale = !empty($data['_current_locale']) ? $data['_current_locale'] : App::getLocale();
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved

$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;
}
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved

$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;
}
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
}


/**
* 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;
}
}