Skip to content

Commit

Permalink
Internationalization (i18n)
Browse files Browse the repository at this point in the history
  • Loading branch information
daveearley committed Jun 17, 2024
1 parent a3e97ef commit 831164f
Show file tree
Hide file tree
Showing 79 changed files with 2,942 additions and 568 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class AttendeeDomainObjectAbstract extends \HiEvents\DomainObjects\Abst
final public const CREATED_AT = 'created_at';
final public const UPDATED_AT = 'updated_at';
final public const DELETED_AT = 'deleted_at';
final public const LOCALE = 'locale';

protected int $id;
protected int $order_id;
Expand All @@ -45,6 +46,7 @@ abstract class AttendeeDomainObjectAbstract extends \HiEvents\DomainObjects\Abst
protected string $created_at;
protected string $updated_at;
protected ?string $deleted_at = null;
protected string $locale = 'en';

public function toArray(): array
{
Expand All @@ -66,6 +68,7 @@ public function toArray(): array
'created_at' => $this->created_at ?? null,
'updated_at' => $this->updated_at ?? null,
'deleted_at' => $this->deleted_at ?? null,
'locale' => $this->locale ?? null,
];
}

Expand Down Expand Up @@ -255,4 +258,15 @@ public function getDeletedAt(): ?string
{
return $this->deleted_at;
}

public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}

public function getLocale(): string
{
return $this->locale;
}
}
14 changes: 14 additions & 0 deletions backend/app/DomainObjects/Generated/OrderDomainObjectAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class OrderDomainObjectAbstract extends \HiEvents\DomainObjects\Abstrac
final public const TAXES_AND_FEES_ROLLUP = 'taxes_and_fees_rollup';
final public const TOTAL_TAX = 'total_tax';
final public const TOTAL_FEE = 'total_fee';
final public const LOCALE = 'locale';

protected int $id;
protected int $event_id;
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class OrderDomainObjectAbstract extends \HiEvents\DomainObjects\Abstrac
protected array|string|null $taxes_and_fees_rollup = null;
protected float $total_tax = 0.0;
protected float $total_fee = 0.0;
protected string $locale = 'en';

public function toArray(): array
{
Expand Down Expand Up @@ -99,6 +101,7 @@ public function toArray(): array
'taxes_and_fees_rollup' => $this->taxes_and_fees_rollup ?? null,
'total_tax' => $this->total_tax ?? null,
'total_fee' => $this->total_fee ?? null,
'locale' => $this->locale ?? null,
];
}

Expand Down Expand Up @@ -409,4 +412,15 @@ public function getTotalFee(): float
{
return $this->total_fee;
}

public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}

public function getLocale(): string
{
return $this->locale;
}
}
14 changes: 14 additions & 0 deletions backend/app/DomainObjects/Generated/UserDomainObjectAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class UserDomainObjectAbstract extends \HiEvents\DomainObjects\Abstract
final public const LAST_NAME = 'last_name';
final public const PENDING_EMAIL = 'pending_email';
final public const TIMEZONE = 'timezone';
final public const LOCALE = 'locale';

protected int $id;
protected string $email;
Expand All @@ -35,6 +36,7 @@ abstract class UserDomainObjectAbstract extends \HiEvents\DomainObjects\Abstract
protected ?string $last_name = null;
protected ?string $pending_email = null;
protected string $timezone;
protected string $locale = 'en';

public function toArray(): array
{
Expand All @@ -51,6 +53,7 @@ public function toArray(): array
'last_name' => $this->last_name ?? null,
'pending_email' => $this->pending_email ?? null,
'timezone' => $this->timezone ?? null,
'locale' => $this->locale ?? null,
];
}

Expand Down Expand Up @@ -185,4 +188,15 @@ public function getTimezone(): string
{
return $this->timezone;
}

public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}

public function getLocale(): string
{
return $this->locale;
}
}
4 changes: 3 additions & 1 deletion backend/app/Http/Actions/Accounts/CreateAccountAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use HiEvents\Http\Request\Account\CreateAccountRequest;
use HiEvents\Http\ResponseCodes;
use HiEvents\Resources\Account\AccountResource;
use HiEvents\Services\Application\Locale\LocaleService;
use HiEvents\Services\Handlers\Account\CreateAccountHandler;
use HiEvents\Services\Handlers\Account\DTO\CreateAccountDTO;
use HiEvents\Services\Handlers\Account\Exceptions\AccountRegistrationDisabledException;
Expand All @@ -24,6 +25,7 @@ class CreateAccountAction extends BaseAuthAction
public function __construct(
private readonly CreateAccountHandler $createAccountHandler,
private readonly LoginHandler $loginHandler,
private readonly LocaleService $localeService,
)
{
}
Expand All @@ -42,7 +44,7 @@ public function __invoke(CreateAccountRequest $request): JsonResponse
'password' => $request->validated('password'),
'timezone' => $request->validated('timezone'),
'currency_code' => $request->validated('currency_code'),
'locale' => $request->getPreferredLanguage(),
'locale' => $this->localeService->getLocaleOrDefault($request->getPreferredLanguage()),
]));
} catch (EmailAlreadyExists $e) {
throw ValidationException::withMessages([
Expand Down
3 changes: 3 additions & 0 deletions backend/app/Http/Actions/Orders/CreateOrderActionPublic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use HiEvents\Http\Request\Order\CreateOrderRequest;
use HiEvents\Http\ResponseCodes;
use HiEvents\Resources\Order\OrderResourcePublic;
use HiEvents\Services\Application\Locale\LocaleService;
use HiEvents\Services\Domain\Order\OrderCreateRequestValidationService;
use HiEvents\Services\Handlers\Order\CreateOrderHandler;
use HiEvents\Services\Handlers\Order\DTO\CreateOrderPublicDTO;
Expand All @@ -22,6 +23,7 @@ public function __construct(
private readonly CreateOrderHandler $orderHandler,
private readonly OrderCreateRequestValidationService $orderCreateRequestValidationService,
private readonly CheckoutSessionManagementService $sessionIdentifierService,
private readonly LocaleService $localeService,

)
{
Expand All @@ -41,6 +43,7 @@ public function __invoke(CreateOrderRequest $request, int $eventId): JsonRespons
'promo_code' => $request->input('promo_code'),
'tickets' => TicketOrderDetailsDTO::collectionFromArray($request->input('tickets')),
'session_identifier' => $this->sessionIdentifierService->getSessionId(),
'order_locale' => $this->localeService->getLocaleOrDefault($request->getPreferredLanguage()),
])
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ public function __invoke(int $eventId, int $orderId): Response
->loadRelation(new Relationship(EventSettingDomainObject::class))
->findById($order->getEventId());

$this->mailer->to($order->getEmail())->send(new OrderSummary(
order: $order,
event: $event,
organizer: $event->getOrganizer(),
eventSettings: $event->getEventSettings(),
));
$this->mailer
->to($order->getEmail())
->locale($order->getLocale())
->send(new OrderSummary(
order: $order,
event: $event,
organizer: $event->getOrganizer(),
eventSettings: $event->getEventSettings(),
));
}

return $this->noContentResponse();
Expand Down
2 changes: 2 additions & 0 deletions backend/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HiEvents\Http;

use HiEvents\Http\Middleware\SetAccountContext;
use HiEvents\Http\Middleware\SetUserLocaleMiddleware;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
Expand Down Expand Up @@ -65,6 +66,7 @@ class Kernel extends HttpKernel
ThrottleRequests::class . ':api',
SubstituteBindings::class,
SetAccountContext::class,
SetUserLocaleMiddleware::class,
],
];

Expand Down
27 changes: 27 additions & 0 deletions backend/app/Http/Middleware/SetUserLocaleMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace HiEvents\Http\Middleware;

use Closure;
use HiEvents\DomainObjects\UserDomainObject;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;

class SetUserLocaleMiddleware
{
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
/** @var UserDomainObject $user */
$user = UserDomainObject::hydrateFromModel(Auth::user());
App::setLocale($user->getLocale());
App::setFallbackLocale(config('app.locale'));
} elseif ($request->hasHeader('Accept-Language')) {
App::setLocale($request->header('Accept-Language'));
App::setFallbackLocale(config('app.locale'));
}

return $next($request);
}
}
23 changes: 23 additions & 0 deletions backend/app/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace HiEvents;

use HiEvents\DomainObjects\Enums\BaseEnum;

enum Locale: string
{
use BaseEnum;

case EN = 'en';
case DE = 'de';
case FR = 'fr';
case ES = 'es';
case PT = 'pt';
case PT_BR = 'pt-br';
case ZH_CN = 'zh-cn';

public static function getSupportedLocales(): array
{
return self::valuesArray();
}
}
2 changes: 1 addition & 1 deletion backend/app/Mail/Order/OrderCancelled.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function envelope(): Envelope
{
return new Envelope(
replyTo: $this->eventSettings->getSupportEmail(),
subject: 'Your order has been cancelled',
subject: __('Your order has been cancelled'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/Order/OrderRefunded.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function envelope(): Envelope
{
return new Envelope(
replyTo: $this->eventSettings->getSupportEmail(),
subject: 'You\'ve received a refund',
subject: __('You\'ve received a refund'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/Order/OrderSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function envelope(): Envelope
{
return new Envelope(
replyTo: $this->eventSettings->getSupportEmail(),
subject: 'Your Order is Confirmed! 🎉',
subject: __('Your Order is Confirmed!') . ' 🎉',
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/User/ConfirmEmailChangeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(UserDomainObject $user, string $token)
public function envelope(): Envelope
{
return new Envelope(
subject: 'Confirm email change',
subject: __('Confirm email change'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/User/ForgotPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(UserDomainObject $user, string $token)
public function envelope(): Envelope
{
return new Envelope(
subject: 'Password reset',
subject: __('Password reset'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/User/ResetPasswordSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResetPasswordSuccess extends BaseMail
public function envelope(): Envelope
{
return new Envelope(
subject: 'Your password has been reset',
subject: __('Your password has been reset'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/Mail/User/UserInvited.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
public function envelope(): Envelope
{
return new Envelope(
subject: 'You\'ve been invited to join ' . $this->appName,
subject: __('You\'ve been invited to join :appName', ['appName' => $this->appName]),
);
}

Expand Down
35 changes: 35 additions & 0 deletions backend/app/Services/Application/Locale/LocaleService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace HiEvents\Services\Application\Locale;

use HiEvents\Locale;
use Illuminate\Config\Repository;

class LocaleService
{
public function __construct(
private readonly Repository $config,
)
{
}

public function getLocaleOrDefault(string $locale): string
{
$supportedLocales = Locale::getSupportedLocales();

if (in_array($locale, $supportedLocales, true)) {
return $locale;
}

$normalizedLocale = str_replace('_', '-', $locale);
$baseLanguage = explode('-', $normalizedLocale)[0];

foreach ($supportedLocales as $supportedLocale) {
if (str_starts_with($supportedLocale, $baseLanguage)) {
return $supportedLocale;
}
}

return $this->config->get('app.locale');
}
}
21 changes: 12 additions & 9 deletions backend/app/Services/Domain/Attendee/SendAttendeeTicketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ public function __construct(
}

public function send(
AttendeeDomainObject $attendee,
EventDomainObject $event,
AttendeeDomainObject $attendee,
EventDomainObject $event,
EventSettingDomainObject $eventSettings,
OrganizerDomainObject $organizer,
OrganizerDomainObject $organizer,
): void
{
$this->mailer->to($attendee->getEmail())->send(new AttendeeTicketMail(
attendee: $attendee,
event: $event,
eventSettings: $eventSettings,
organizer: $organizer,
));
$this->mailer
->to($attendee->getEmail())
->locale($attendee->getLocale())
->send(new AttendeeTicketMail(
attendee: $attendee,
event: $event,
eventSettings: $eventSettings,
organizer: $organizer,
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private function sendMessage(
string $emailAddress,
string $fullName,
SendMessageDTO $messageData,
EventDomainObject $event
EventDomainObject $event,
): void
{
$this->mailer->to(
Expand Down
Loading

0 comments on commit 831164f

Please sign in to comment.