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 15 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 localize version of mail templates (akin to localized CMS content files)
bennothommo marked this conversation as resolved.
Show resolved Hide resolved
*/
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
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 string|null
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
*/
public function findLocalizedMailViewContent($mailer, $message, $view, $data, $raw, $plain)
{
// There is no need to localize raw templates
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
if (!empty($raw)) {
return null;
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
}
mjauvin marked this conversation as resolved.
Show resolved Hide resolved

$locale = !empty($data['_current_locale']) ? $data['_current_locale'] : App::getLocale();
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved

$factory = $mailer->getViewFactory();

if (isset($view)) {
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
$view = $this->getLocalizedView($factory, $view, $locale);
}

if (isset($plain)) {
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
$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);

$result = MailManager::instance()->addContentToMailer($message, $code, $data, $plainOnly);
if ($result) {
// prevent the caller who fired the event from continuing to send the mail.
return false;
}
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}