Skip to content

Commit 1cd19c7

Browse files
authored
Merge pull request #4497 from BookStackApp/notification_language
Notifications: User language for notification text
2 parents 5d38ae3 + a720b37 commit 1cd19c7

File tree

16 files changed

+159
-120
lines changed

16 files changed

+159
-120
lines changed

app/Activity/Notifications/MessageParts/LinkedMailMessageLine.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace BookStack\Activity\Notifications\MessageParts;
44

55
use Illuminate\Contracts\Support\Htmlable;
6+
use Stringable;
67

78
/**
89
* A line of text with linked text included, intended for use
910
* in MailMessages. The line should have a ':link' placeholder for
1011
* where the link should be inserted within the line.
1112
*/
12-
class LinkedMailMessageLine implements Htmlable
13+
class LinkedMailMessageLine implements Htmlable, Stringable
1314
{
1415
public function __construct(
1516
protected string $url,
@@ -23,4 +24,10 @@ public function toHtml(): string
2324
$link = '<a href="' . e($this->url) . '">' . e($this->linkText) . '</a>';
2425
return str_replace(':link', $link, e($this->line));
2526
}
27+
28+
public function __toString(): string
29+
{
30+
$link = "{$this->linkText} ({$this->url})";
31+
return str_replace(':link', $link, $this->line);
32+
}
2633
}

app/Activity/Notifications/MessageParts/ListMessageLine.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace BookStack\Activity\Notifications\MessageParts;
44

55
use Illuminate\Contracts\Support\Htmlable;
6+
use Stringable;
67

78
/**
89
* A bullet point list of content, where the keys of the given list array
910
* are bolded header elements, and the values follow.
1011
*/
11-
class ListMessageLine implements Htmlable
12+
class ListMessageLine implements Htmlable, Stringable
1213
{
1314
public function __construct(
1415
protected array $list
@@ -23,4 +24,13 @@ public function toHtml(): string
2324
}
2425
return implode("<br>\n", $list);
2526
}
27+
28+
public function __toString(): string
29+
{
30+
$list = [];
31+
foreach ($this->list as $header => $content) {
32+
$list[] = $header . ' ' . $content;
33+
}
34+
return implode("\n", $list);
35+
}
2636
}

app/Activity/Notifications/Messages/BaseActivityNotification.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use BookStack\Activity\Models\Loggable;
66
use BookStack\Activity\Notifications\MessageParts\LinkedMailMessageLine;
7+
use BookStack\Notifications\MailNotification;
78
use BookStack\Users\Models\User;
89
use Illuminate\Bus\Queueable;
9-
use Illuminate\Notifications\Messages\MailMessage;
10-
use Illuminate\Notifications\Notification;
1110

12-
abstract class BaseActivityNotification extends Notification
11+
abstract class BaseActivityNotification extends MailNotification
1312
{
1413
use Queueable;
1514

@@ -19,22 +18,6 @@ public function __construct(
1918
) {
2019
}
2120

22-
/**
23-
* Get the notification's delivery channels.
24-
*
25-
* @param mixed $notifiable
26-
* @return array
27-
*/
28-
public function via($notifiable)
29-
{
30-
return ['mail'];
31-
}
32-
33-
/**
34-
* Get the mail representation of the notification.
35-
*/
36-
abstract public function toMail(mixed $notifiable): MailMessage;
37-
3821
/**
3922
* Get the array representation of the notification.
4023
*
@@ -52,12 +35,12 @@ public function toArray($notifiable)
5235
/**
5336
* Build the common reason footer line used in mail messages.
5437
*/
55-
protected function buildReasonFooterLine(): LinkedMailMessageLine
38+
protected function buildReasonFooterLine(string $language): LinkedMailMessageLine
5639
{
5740
return new LinkedMailMessageLine(
5841
url('/preferences/notifications'),
59-
trans('notifications.footer_reason'),
60-
trans('notifications.footer_reason_link'),
42+
trans('notifications.footer_reason', [], $language),
43+
trans('notifications.footer_reason_link', [], $language),
6144
);
6245
}
6346
}

app/Activity/Notifications/Messages/CommentCreationNotification.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@
55
use BookStack\Activity\Models\Comment;
66
use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
77
use BookStack\Entities\Models\Page;
8+
use BookStack\Users\Models\User;
89
use Illuminate\Notifications\Messages\MailMessage;
910

1011
class CommentCreationNotification extends BaseActivityNotification
1112
{
12-
public function toMail(mixed $notifiable): MailMessage
13+
public function toMail(User $notifiable): MailMessage
1314
{
1415
/** @var Comment $comment */
1516
$comment = $this->detail;
1617
/** @var Page $page */
1718
$page = $comment->entity;
1819

19-
return (new MailMessage())
20-
->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()]))
21-
->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')]))
20+
$language = $notifiable->getLanguage();
21+
22+
return $this->newMailMessage($language)
23+
->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()], $language))
24+
->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')], $language))
2225
->line(new ListMessageLine([
23-
trans('notifications.detail_page_name') => $page->name,
24-
trans('notifications.detail_commenter') => $this->user->name,
25-
trans('notifications.detail_comment') => strip_tags($comment->html),
26+
trans('notifications.detail_page_name', [], $language) => $page->name,
27+
trans('notifications.detail_commenter', [], $language) => $this->user->name,
28+
trans('notifications.detail_comment', [], $language) => strip_tags($comment->html),
2629
]))
27-
->action(trans('notifications.action_view_comment'), $page->getUrl('#comment' . $comment->local_id))
28-
->line($this->buildReasonFooterLine());
30+
->action(trans('notifications.action_view_comment', [], $language), $page->getUrl('#comment' . $comment->local_id))
31+
->line($this->buildReasonFooterLine($language));
2932
}
3033
}

app/Activity/Notifications/Messages/PageCreationNotification.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55
use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
66
use BookStack\Entities\Models\Page;
7+
use BookStack\Users\Models\User;
78
use Illuminate\Notifications\Messages\MailMessage;
89

910
class PageCreationNotification extends BaseActivityNotification
1011
{
11-
public function toMail(mixed $notifiable): MailMessage
12+
public function toMail(User $notifiable): MailMessage
1213
{
1314
/** @var Page $page */
1415
$page = $this->detail;
1516

16-
return (new MailMessage())
17-
->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()]))
18-
->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')]))
17+
$language = $notifiable->getLanguage();
18+
19+
return $this->newMailMessage($language)
20+
->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()], $language))
21+
->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')], $language))
1922
->line(new ListMessageLine([
20-
trans('notifications.detail_page_name') => $page->name,
21-
trans('notifications.detail_created_by') => $this->user->name,
23+
trans('notifications.detail_page_name', [], $language) => $page->name,
24+
trans('notifications.detail_created_by', [], $language) => $this->user->name,
2225
]))
23-
->action(trans('notifications.action_view_page'), $page->getUrl())
24-
->line($this->buildReasonFooterLine());
26+
->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
27+
->line($this->buildReasonFooterLine($language));
2528
}
2629
}

app/Activity/Notifications/Messages/PageUpdateNotification.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@
44

55
use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
66
use BookStack\Entities\Models\Page;
7+
use BookStack\Users\Models\User;
78
use Illuminate\Notifications\Messages\MailMessage;
89

910
class PageUpdateNotification extends BaseActivityNotification
1011
{
11-
public function toMail(mixed $notifiable): MailMessage
12+
public function toMail(User $notifiable): MailMessage
1213
{
1314
/** @var Page $page */
1415
$page = $this->detail;
1516

16-
return (new MailMessage())
17-
->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()]))
18-
->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')]))
17+
$language = $notifiable->getLanguage();
18+
19+
return $this->newMailMessage($language)
20+
->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()], $language))
21+
->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')], $language))
1922
->line(new ListMessageLine([
20-
trans('notifications.detail_page_name') => $page->name,
21-
trans('notifications.detail_updated_by') => $this->user->name,
23+
trans('notifications.detail_page_name', [], $language) => $page->name,
24+
trans('notifications.detail_updated_by', [], $language) => $this->user->name,
2225
]))
23-
->line(trans('notifications.updated_page_debounce'))
24-
->action(trans('notifications.action_view_page'), $page->getUrl())
25-
->line($this->buildReasonFooterLine());
26+
->line(trans('notifications.updated_page_debounce', [], $language))
27+
->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
28+
->line($this->buildReasonFooterLine($language));
2629
}
2730
}

app/Notifications/ConfirmEmail.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@
22

33
namespace BookStack\Notifications;
44

5+
use BookStack\Users\Models\User;
6+
use Illuminate\Notifications\Messages\MailMessage;
7+
58
class ConfirmEmail extends MailNotification
69
{
7-
public $token;
8-
9-
/**
10-
* Create a new notification instance.
11-
*
12-
* @param string $token
13-
*/
14-
public function __construct($token)
15-
{
16-
$this->token = $token;
10+
public function __construct(
11+
public string $token
12+
) {
1713
}
1814

19-
/**
20-
* Get the mail representation of the notification.
21-
*
22-
* @param mixed $notifiable
23-
*
24-
* @return \Illuminate\Notifications\Messages\MailMessage
25-
*/
26-
public function toMail($notifiable)
15+
public function toMail(User $notifiable): MailMessage
2716
{
2817
$appName = ['appName' => setting('app-name')];
2918

app/Notifications/MailNotification.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace BookStack\Notifications;
44

5+
use BookStack\Users\Models\User;
56
use Illuminate\Bus\Queueable;
67
use Illuminate\Contracts\Queue\ShouldQueue;
78
use Illuminate\Notifications\Messages\MailMessage;
89
use Illuminate\Notifications\Notification;
910

10-
class MailNotification extends Notification implements ShouldQueue
11+
abstract class MailNotification extends Notification implements ShouldQueue
1112
{
1213
use Queueable;
1314

15+
/**
16+
* Get the mail representation of the notification.
17+
*/
18+
abstract public function toMail(User $notifiable): MailMessage;
19+
1420
/**
1521
* Get the notification's channels.
1622
*
@@ -25,14 +31,14 @@ public function via($notifiable)
2531

2632
/**
2733
* Create a new mail message.
28-
*
29-
* @return MailMessage
3034
*/
31-
protected function newMailMessage()
35+
protected function newMailMessage(string $language = ''): MailMessage
3236
{
37+
$data = ['language' => $language ?: null];
38+
3339
return (new MailMessage())->view([
3440
'html' => 'vendor.notifications.email',
3541
'text' => 'vendor.notifications.email-plain',
36-
]);
42+
], $data);
3743
}
3844
}

app/Notifications/ResetPassword.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,17 @@
22

33
namespace BookStack\Notifications;
44

5+
use BookStack\Users\Models\User;
6+
use Illuminate\Notifications\Messages\MailMessage;
7+
58
class ResetPassword extends MailNotification
69
{
7-
/**
8-
* The password reset token.
9-
*
10-
* @var string
11-
*/
12-
public $token;
13-
14-
/**
15-
* Create a notification instance.
16-
*
17-
* @param string $token
18-
*/
19-
public function __construct($token)
20-
{
21-
$this->token = $token;
10+
public function __construct(
11+
public string $token
12+
) {
2213
}
2314

24-
/**
25-
* Build the mail representation of the notification.
26-
*
27-
* @return \Illuminate\Notifications\Messages\MailMessage
28-
*/
29-
public function toMail()
15+
public function toMail(User $notifiable): MailMessage
3016
{
3117
return $this->newMailMessage()
3218
->subject(trans('auth.email_reset_subject', ['appName' => setting('app-name')]))

app/Notifications/TestEmail.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
namespace BookStack\Notifications;
44

5+
use BookStack\Users\Models\User;
6+
use Illuminate\Notifications\Messages\MailMessage;
7+
58
class TestEmail extends MailNotification
69
{
7-
/**
8-
* Get the mail representation of the notification.
9-
*
10-
* @param mixed $notifiable
11-
*
12-
* @return \Illuminate\Notifications\Messages\MailMessage
13-
*/
14-
public function toMail($notifiable)
10+
public function toMail(User $notifiable): MailMessage
1511
{
1612
return $this->newMailMessage()
1713
->subject(trans('settings.maint_send_test_email_mail_subject'))

0 commit comments

Comments
 (0)