Browse Source

Translate email notification to the selected users preferred language.

pull/2/head
Nigel Banks 3 years ago
parent
commit
41355cb729
  1. 10
      dgi_fixity.module
  2. 14
      src/FixityCheckService.php
  3. 4
      src/FixityCheckServiceInterface.php

10
dgi_fixity.module

@ -78,13 +78,15 @@ function dgi_fixity_mail($key, &$message, $params) {
return; return;
} }
$options = ['langcode' => $message['langcode']];
$now = \Drupal::time()->getRequestTime(); $now = \Drupal::time()->getRequestTime();
$subject = \t('Fixity Check Report - @now', ['@now' => date(DATE_RFC7231, $now)])->render(); $subject = \t('Fixity Check Report - @now', ['@now' => date(DATE_RFC7231, $now)], $options)->render();
$body = $fixity->summary($stats); $body = $fixity->summary($stats, $options);
if ($stats['failed'] !== 0) { if ($stats['failed'] !== 0) {
$body[] = \t( $body[] = \t(
'There are failed checks which require your attention please review the current state of checks <a href="@site">here</a>.', 'There are failed checks which require your attention please review the current state of checks <a href="@site">here</a>.',
['@site' => Url::fromRoute('entity.fixity_check.collection', [], ['absolute' => TRUE])->toString()] ['@site' => Url::fromRoute('entity.fixity_check.collection', [], ['absolute' => TRUE])->toString()],
$options
)->render(); )->render();
} }
@ -130,7 +132,7 @@ function dgi_fixity_cron() {
$uid = $settings->get(SettingsForm::NOTIFY_USER); $uid = $settings->get(SettingsForm::NOTIFY_USER);
$user = User::load($uid); $user = User::load($uid);
if ($user) { if ($user) {
\Drupal::service('plugin.manager.mail')->mail('dgi_fixity', 'notify', $user->getEmail(), $user->getPreferredAdminLangcode(TRUE)); \Drupal::service('plugin.manager.mail')->mail('dgi_fixity', 'notify', $user->getEmail(), $user->getPreferredLangcode(TRUE));
} }
} }

14
src/FixityCheckService.php

@ -346,28 +346,36 @@ class FixityCheckService implements FixityCheckServiceInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function summary(array $stats): array { public function summary(array $stats, array $options = []): array {
$summary = []; $summary = [];
$summary[] = $this->formatPlural( $summary[] = $this->formatPlural(
$stats['revisions'], $stats['revisions'],
'@count check has been performed since tracking started.', '@count check has been performed since tracking started.',
'@count checks have been performed since tracking started.', '@count checks have been performed since tracking started.',
[],
$options
); );
$summary[] = $this->formatPlural( $summary[] = $this->formatPlural(
$stats['periodic']['total'], $stats['periodic']['total'],
'@count file is set to be checked periodically.', '@count file is set to be checked periodically.',
'@count files are set to be checked periodically.', '@count files are set to be checked periodically.',
[],
$options
); );
$summary[] = $this->formatPlural( $summary[] = $this->formatPlural(
$stats['periodic']['current'], $stats['periodic']['current'],
'@count periodic check is up to date.', '@count periodic check is up to date.',
'@count periodic checks are up to date.', '@count periodic checks are up to date.',
[],
$options
); );
if ($stats['periodic']['expired'] > 0) { if ($stats['periodic']['expired'] > 0) {
$summary[] = $this->formatPlural( $summary[] = $this->formatPlural(
$stats['periodic']['expired'], $stats['periodic']['expired'],
'@count periodic check is out to date.', '@count periodic check is out to date.',
'@count periodic checks are out to date.', '@count periodic checks are out to date.',
[],
$options
); );
} }
if ($stats['failed'] > 0) { if ($stats['failed'] > 0) {
@ -375,12 +383,16 @@ class FixityCheckService implements FixityCheckServiceInterface {
$stats['failed'], $stats['failed'],
'@count check has failed.', '@count check has failed.',
'@count checks have failed.', '@count checks have failed.',
[],
$options
); );
foreach ($stats['states'] as $state => $count) { foreach ($stats['states'] as $state => $count) {
$summary[] = $this->formatPlural( $summary[] = $this->formatPlural(
$count, $count,
FixityCheck::getStateProperty($state, 'singular'), FixityCheck::getStateProperty($state, 'singular'),
FixityCheck::getStateProperty($state, 'plural'), FixityCheck::getStateProperty($state, 'plural'),
[],
$options
); );
} }
} }

4
src/FixityCheckServiceInterface.php

@ -125,10 +125,12 @@ interface FixityCheckServiceInterface {
* *
* @param array $stats * @param array $stats
* The stats as returned by this service. * The stats as returned by this service.
* @param array $options
* An associative array of additional options for the TranslatableMarkup.
* *
* @return \Drupal\Core\StringTranslation\TranslatableMarkup[] * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
* A list of messages that describe the current state of the system. * A list of messages that describe the current state of the system.
*/ */
public function summary(array $stats): array; public function summary(array $stats, array $options = []): array;
} }

Loading…
Cancel
Save