Skip to content

Language cleanup #4555

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

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions app/Access/Notifications/UserInviteNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public function __construct(
public function toMail(User $notifiable): MailMessage
{
$appName = ['appName' => setting('app-name')];
$language = $notifiable->getLanguage();
$locale = $notifiable->getLocale();

return $this->newMailMessage($language)
->subject(trans('auth.user_invite_email_subject', $appName, $language))
->greeting(trans('auth.user_invite_email_greeting', $appName, $language))
->line(trans('auth.user_invite_email_text', [], $language))
->action(trans('auth.user_invite_email_action', [], $language), url('/register/invite/' . $this->token));
return $this->newMailMessage($locale)
->subject($locale->trans('auth.user_invite_email_subject', $appName))
->greeting($locale->trans('auth.user_invite_email_greeting', $appName))
->line($locale->trans('auth.user_invite_email_text'))
->action($locale->trans('auth.user_invite_email_action'), url('/register/invite/' . $this->token));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BookStack\Activity\Models\Loggable;
use BookStack\Activity\Notifications\MessageParts\LinkedMailMessageLine;
use BookStack\App\MailNotification;
use BookStack\Translation\LocaleDefinition;
use BookStack\Users\Models\User;
use Illuminate\Bus\Queueable;

Expand Down Expand Up @@ -35,12 +36,12 @@ public function toArray($notifiable)
/**
* Build the common reason footer line used in mail messages.
*/
protected function buildReasonFooterLine(string $language): LinkedMailMessageLine
protected function buildReasonFooterLine(LocaleDefinition $locale): LinkedMailMessageLine
{
return new LinkedMailMessageLine(
url('/preferences/notifications'),
trans('notifications.footer_reason', [], $language),
trans('notifications.footer_reason_link', [], $language),
$locale->trans('notifications.footer_reason'),
$locale->trans('notifications.footer_reason_link'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public function toMail(User $notifiable): MailMessage
/** @var Page $page */
$page = $comment->entity;

$language = $notifiable->getLanguage();
$locale = $notifiable->getLocale();

return $this->newMailMessage($language)
->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')], $language))
return $this->newMailMessage($locale)
->subject($locale->trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()]))
->line($locale->trans('notifications.new_comment_intro', ['appName' => setting('app-name')]))
->line(new ListMessageLine([
trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_commenter', [], $language) => $this->user->name,
trans('notifications.detail_comment', [], $language) => strip_tags($comment->html),
$locale->trans('notifications.detail_page_name') => $page->name,
$locale->trans('notifications.detail_commenter') => $this->user->name,
$locale->trans('notifications.detail_comment') => strip_tags($comment->html),
]))
->action(trans('notifications.action_view_comment', [], $language), $page->getUrl('#comment' . $comment->local_id))
->line($this->buildReasonFooterLine($language));
->action($locale->trans('notifications.action_view_comment'), $page->getUrl('#comment' . $comment->local_id))
->line($this->buildReasonFooterLine($locale));
}
}
16 changes: 8 additions & 8 deletions app/Activity/Notifications/Messages/PageCreationNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public function toMail(User $notifiable): MailMessage
/** @var Page $page */
$page = $this->detail;

$language = $notifiable->getLanguage();
$locale = $notifiable->getLocale();

return $this->newMailMessage($language)
->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')], $language))
return $this->newMailMessage($locale)
->subject($locale->trans('notifications.new_page_subject', ['pageName' => $page->getShortName()]))
->line($locale->trans('notifications.new_page_intro', ['appName' => setting('app-name')], $locale))
->line(new ListMessageLine([
trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_created_by', [], $language) => $this->user->name,
$locale->trans('notifications.detail_page_name') => $page->name,
$locale->trans('notifications.detail_created_by') => $this->user->name,
]))
->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
->line($this->buildReasonFooterLine($language));
->action($locale->trans('notifications.action_view_page'), $page->getUrl())
->line($this->buildReasonFooterLine($locale));
}
}
18 changes: 9 additions & 9 deletions app/Activity/Notifications/Messages/PageUpdateNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public function toMail(User $notifiable): MailMessage
/** @var Page $page */
$page = $this->detail;

$language = $notifiable->getLanguage();
$locale = $notifiable->getLocale();

return $this->newMailMessage($language)
->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')], $language))
return $this->newMailMessage($locale)
->subject($locale->trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()]))
->line($locale->trans('notifications.updated_page_intro', ['appName' => setting('app-name')]))
->line(new ListMessageLine([
trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_updated_by', [], $language) => $this->user->name,
$locale->trans('notifications.detail_page_name') => $page->name,
$locale->trans('notifications.detail_updated_by') => $this->user->name,
]))
->line(trans('notifications.updated_page_debounce', [], $language))
->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
->line($this->buildReasonFooterLine($language));
->line($locale->trans('notifications.updated_page_debounce'))
->action($locale->trans('notifications.action_view_page'), $page->getUrl())
->line($this->buildReasonFooterLine($locale));
}
}
5 changes: 3 additions & 2 deletions app/App/MailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BookStack\App;

use BookStack\Translation\LocaleDefinition;
use BookStack\Users\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -32,9 +33,9 @@ public function via($notifiable)
/**
* Create a new mail message.
*/
protected function newMailMessage(string $language = ''): MailMessage
protected function newMailMessage(?LocaleDefinition $locale = null): MailMessage
{
$data = ['language' => $language ?: null];
$data = ['locale' => $locale ?? user()->getLocale()];

return (new MailMessage())->view([
'html' => 'vendor.notifications.email',
Expand Down
9 changes: 3 additions & 6 deletions app/Config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@
'timezone' => env('APP_TIMEZONE', 'UTC'),

// Default locale to use
// A default variant is also stored since Laravel can overwrite
// app.locale when dynamically setting the locale in-app.
'locale' => env('APP_LANG', 'en'),

// Locales available
'locales' => ['en', 'ar', 'bg', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'de_informal', 'el', 'es', 'es_AR', 'et', 'eu', 'fa', 'fr', 'he', 'hr', 'hu', 'id', 'it', 'ja', 'ka', 'ko', 'lt', 'lv', 'nl', 'nb', 'pt', 'pt_BR', 'sk', 'sl', 'sv', 'pl', 'ro', 'ru', 'tr', 'uk', 'uz', 'vi', 'zh_CN', 'zh_TW'],
'default_locale' => env('APP_LANG', 'en'),

// Application Fallback Locale
'fallback_locale' => 'en',

// Faker Locale
'faker_locale' => 'en_GB',

// Enable right-to-left text control.
'rtl' => false,

// Auto-detect the locale for public users
// For public users their locale can be guessed by headers sent by their
// browser. This is usually set by users in their browser settings.
Expand Down
39 changes: 19 additions & 20 deletions app/Entities/Tools/ExportFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

class ExportFormatter
{
protected ImageService $imageService;
protected PdfGenerator $pdfGenerator;
protected CspService $cspService;

/**
* ExportService constructor.
*/
public function __construct(ImageService $imageService, PdfGenerator $pdfGenerator, CspService $cspService)
{
$this->imageService = $imageService;
$this->pdfGenerator = $pdfGenerator;
$this->cspService = $cspService;
public function __construct(
protected ImageService $imageService,
protected PdfGenerator $pdfGenerator,
protected CspService $cspService
) {
}

/**
Expand All @@ -36,13 +29,14 @@ public function __construct(ImageService $imageService, PdfGenerator $pdfGenerat
*
* @throws Throwable
*/
public function pageToContainedHtml(Page $page)
public function pageToContainedHtml(Page $page): string
{
$page->html = (new PageContent($page))->render();
$pageHtml = view('exports.page', [
'page' => $page,
'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render();

return $this->containHtml($pageHtml);
Expand All @@ -53,7 +47,7 @@ public function pageToContainedHtml(Page $page)
*
* @throws Throwable
*/
public function chapterToContainedHtml(Chapter $chapter)
public function chapterToContainedHtml(Chapter $chapter): string
{
$pages = $chapter->getVisiblePages();
$pages->each(function ($page) {
Expand All @@ -64,6 +58,7 @@ public function chapterToContainedHtml(Chapter $chapter)
'pages' => $pages,
'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render();

return $this->containHtml($html);
Expand All @@ -74,14 +69,15 @@ public function chapterToContainedHtml(Chapter $chapter)
*
* @throws Throwable
*/
public function bookToContainedHtml(Book $book)
public function bookToContainedHtml(Book $book): string
{
$bookTree = (new BookContents($book))->getTree(false, true);
$html = view('exports.book', [
'book' => $book,
'bookChildren' => $bookTree,
'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render();

return $this->containHtml($html);
Expand All @@ -92,13 +88,14 @@ public function bookToContainedHtml(Book $book)
*
* @throws Throwable
*/
public function pageToPdf(Page $page)
public function pageToPdf(Page $page): string
{
$page->html = (new PageContent($page))->render();
$html = view('exports.page', [
'page' => $page,
'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render();

return $this->htmlToPdf($html);
Expand All @@ -109,7 +106,7 @@ public function pageToPdf(Page $page)
*
* @throws Throwable
*/
public function chapterToPdf(Chapter $chapter)
public function chapterToPdf(Chapter $chapter): string
{
$pages = $chapter->getVisiblePages();
$pages->each(function ($page) {
Expand All @@ -121,6 +118,7 @@ public function chapterToPdf(Chapter $chapter)
'pages' => $pages,
'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render();

return $this->htmlToPdf($html);
Expand All @@ -131,14 +129,15 @@ public function chapterToPdf(Chapter $chapter)
*
* @throws Throwable
*/
public function bookToPdf(Book $book)
public function bookToPdf(Book $book): string
{
$bookTree = (new BookContents($book))->getTree(false, true);
$html = view('exports.book', [
'book' => $book,
'bookChildren' => $bookTree,
'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render();

return $this->htmlToPdf($html);
Expand Down Expand Up @@ -194,7 +193,7 @@ protected function replaceIframesWithLinks(string $html): string
/** @var DOMElement $iframe */
foreach ($iframes as $iframe) {
$link = $iframe->getAttribute('src');
if (strpos($link, '//') === 0) {
if (str_starts_with($link, '//')) {
$link = 'https:' . $link;
}

Expand Down Expand Up @@ -240,7 +239,7 @@ protected function containHtml(string $htmlContent): string
foreach ($linksOutput[0] as $index => $linkMatch) {
$oldLinkString = $linkMatch;
$srcString = $linksOutput[2][$index];
if (strpos(trim($srcString), 'http') !== 0) {
if (!str_starts_with(trim($srcString), 'http')) {
$newSrcString = url($srcString);
$newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
$htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);
Expand Down
31 changes: 9 additions & 22 deletions app/Http/Middleware/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace BookStack\Http\Middleware;

use BookStack\Translation\LanguageManager;
use Carbon\Carbon;
use BookStack\Translation\LocaleManager;
use Closure;

class Localization
{
protected LanguageManager $languageManager;

public function __construct(LanguageManager $languageManager)
{
$this->languageManager = $languageManager;
public function __construct(
protected LocaleManager $localeManager
) {
}

/**
Expand All @@ -25,22 +22,12 @@ public function __construct(LanguageManager $languageManager)
*/
public function handle($request, Closure $next)
{
// Get and record the default language in the config
$defaultLang = config('app.locale');
config()->set('app.default_locale', $defaultLang);

// Get the user's language and record that in the config for use in views
$userLang = $this->languageManager->getUserLanguage($request, $defaultLang);
config()->set('app.lang', str_replace('_', '-', $this->languageManager->getIsoName($userLang)));

// Set text direction
if ($this->languageManager->isRTL($userLang)) {
config()->set('app.rtl', true);
}
// Share details of the user's locale for use in views
$userLocale = $this->localeManager->getForUser(user());
view()->share('locale', $userLocale);

app()->setLocale($userLang);
Carbon::setLocale($userLang);
$this->languageManager->setPhpDateTimeLocale($userLang);
// Set locale for system components
app()->setLocale($userLocale->appLocale());

return $next($request);
}
Expand Down
Loading