adding a bunch of wip: i18n stuff

This commit is contained in:
2022-11-28 12:32:48 -07:00
parent 995cc32578
commit 3f340d57fc
10 changed files with 301 additions and 53 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Middleware;
use App\Models\Language;
use Illuminate\Http\Request;
use Inertia\Middleware;
@ -41,19 +42,56 @@ class HandleInertiaRequests extends Middleware
*/
public function share(Request $request): array
{
$localeFields = ['locale', 'iso_code', 'name', 'localized_name'];
$currentLocale = $request->session()->get('locale', null);
if (is_null($currentLocale)) {
$currentLocale = Language::where(['locale' => 'en', 'iso_code' => 'en_US'])->get($localeFields)[0]->toArray();
$request->session()->put('locale', [
'locale' => $currentLocale['locale'],
'iso_code' => $currentLocale['iso_code'],
'name' => $currentLocale['name'],
'localized_name' => $currentLocale['localized_name'],
]);
}
$localeFilePath = base_path("lang/{$currentLocale['locale']}.json");
$notifications = [];
$notificationsCount = count($notifications);
$unreadNotifications = false;
if (! is_null($request->user())) {
$notifications = $request->user()->notifications;
$notificationsCount = count($notifications);
for ($i = 0; $i < $notificationsCount; $i++) {
$newData = $notifications[$i]->data;
$createdAt = carbon($notifications[$i]->created_at);
$dateFormat = 'F j';
if (!$createdAt->is(gmdate('Y'))) {
$dateFormat = 'F j, Y';
}
$newData['created_at_date'] = $createdAt->copy()->format($dateFormat);
$newData['created_at_time'] = $createdAt->copy()->format('H:i');
$notifications[$i]->data = $newData;
}
if (count($request->user()->unreadNotifications) > 0) {
$unreadNotifications = true;
}
}
return array_merge(parent::share($request), [
'appName' => config('app.name'),
$additionalData = [
'appName' => config('app.name'),
'availableLocales' => Language::get($localeFields),
'currentLocale' => $currentLocale,
'language' => translations($localeFilePath),
'notifications' => $notifications,
'unreadNotifications' => $unreadNotifications,
]);
];
if ($request->user()->email === env('ADMIN_EMAIL')) {
$additionalData['is_admin_user'] = true;
}
return array_merge(parent::share($request), $additionalData);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetLocale
{
/**
* Handle an incoming request.
*
* @package App\Http\Middleware\SetLocale
* @since 1.0.0
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (session()->has('locale')) {
app()->setLocale(session('locale')['locale']);
}
return $next($request);
}
}